diff --git a/ShadowsocksUriGenerator.Chatbot.Telegram/CLI/BotRunner.cs b/ShadowsocksUriGenerator.Chatbot.Telegram/CLI/BotRunner.cs index ba07bf2..4ef5318 100644 --- a/ShadowsocksUriGenerator.Chatbot.Telegram/CLI/BotRunner.cs +++ b/ShadowsocksUriGenerator.Chatbot.Telegram/CLI/BotRunner.cs @@ -36,15 +36,15 @@ public static async Task RunBot(string? botToken, CancellationToken cancell var bot = new TelegramBotClient(botToken); Console.WriteLine("Created Telegram bot instance with API token."); - var me = await bot.GetMeAsync(cancellationToken); + var me = await bot.GetMe(cancellationToken); if (string.IsNullOrEmpty(me.Username)) throw new Exception("Error: bot username is null or empty."); - await bot.SetMyCommandsAsync(UpdateHandler.BotCommandsPublic, null, null, cancellationToken); + await bot.SetMyCommands(UpdateHandler.BotCommandsPublic, null, null, cancellationToken); Console.WriteLine($"Registered {UpdateHandler.BotCommandsPublic.Length} bot commands for all chats."); var privateChatCommands = UpdateHandler.BotCommandsPrivate.Concat(UpdateHandler.BotCommandsPublic); - await bot.SetMyCommandsAsync(privateChatCommands, BotCommandScope.AllPrivateChats(), null, cancellationToken); + await bot.SetMyCommands(privateChatCommands, BotCommandScope.AllPrivateChats(), null, cancellationToken); Console.WriteLine($"Registered {privateChatCommands.Count()} bot commands for private chats."); Console.WriteLine($"Started Telegram bot: @{me.Username} ({me.Id})."); diff --git a/ShadowsocksUriGenerator.Chatbot.Telegram/Commands/AuthCommands.cs b/ShadowsocksUriGenerator.Chatbot.Telegram/Commands/AuthCommands.cs index 3fe7c1f..4bc1eea 100644 --- a/ShadowsocksUriGenerator.Chatbot.Telegram/Commands/AuthCommands.cs +++ b/ShadowsocksUriGenerator.Chatbot.Telegram/Commands/AuthCommands.cs @@ -21,11 +21,12 @@ public static Task StartAsync(ITelegramBotClient botClient, Message message, Bot ✈️ To get your boarding pass, please use `/link ` to link your Telegram account to your user\."; - return botClient.SendTextMessageAsync(message.Chat.Id, - replyMarkdownV2, - parseMode: ParseMode.MarkdownV2, - replyToMessageId: message.MessageId, - cancellationToken: cancellationToken); + return botClient.SendMessage( + message.Chat.Id, + replyMarkdownV2, + parseMode: ParseMode.MarkdownV2, + replyParameters: message, + cancellationToken: cancellationToken); } public static async Task LinkAsync(ITelegramBotClient botClient, Message message, string? argument, CancellationToken cancellationToken = default) @@ -101,10 +102,11 @@ public static async Task LinkAsync(ITelegramBotClient botClient, Message message } } - await botClient.SendTextMessageAsync(message.Chat.Id, - reply, - replyToMessageId: message.MessageId, - cancellationToken: cancellationToken); + _ = await botClient.SendMessage( + message.Chat.Id, + reply, + replyParameters: message, + cancellationToken: cancellationToken); } public static async Task UnlinkAsync(ITelegramBotClient botClient, Message message, CancellationToken cancellationToken = default) @@ -149,10 +151,11 @@ public static async Task UnlinkAsync(ITelegramBotClient botClient, Message messa Console.WriteLine(" Response: not linked"); } - await botClient.SendTextMessageAsync(message.Chat.Id, - reply, - replyToMessageId: message.MessageId, - cancellationToken: cancellationToken); + _ = await botClient.SendMessage( + message.Chat.Id, + reply, + replyParameters: message, + cancellationToken: cancellationToken); } } } diff --git a/ShadowsocksUriGenerator.Chatbot.Telegram/Commands/CredCommands.cs b/ShadowsocksUriGenerator.Chatbot.Telegram/Commands/CredCommands.cs index 68a7494..3332244 100644 --- a/ShadowsocksUriGenerator.Chatbot.Telegram/Commands/CredCommands.cs +++ b/ShadowsocksUriGenerator.Chatbot.Telegram/Commands/CredCommands.cs @@ -78,11 +78,12 @@ public static async Task GetSsLinksAsync(ITelegramBotClient botClient, Message m Console.WriteLine(" Response: user not linked."); } - await botClient.SendPossiblyLongTextMessageAsync(message.Chat.Id, - reply, - disableWebPagePreview: true, - replyToMessageId: message.MessageId, - cancellationToken: cancellationToken); + _ = await botClient.SendPossiblyLongTextMessageAsync( + message.Chat.Id, + reply, + linkPreviewOptions: new() { IsDisabled = true, }, + replyParameters: message, + cancellationToken: cancellationToken); } public static async Task GetOnlineConfigLinksAsync(ITelegramBotClient botClient, Message message, CancellationToken cancellationToken = default) @@ -171,12 +172,13 @@ public static async Task GetOnlineConfigLinksAsync(ITelegramBotClient botClient, Console.WriteLine(" Response: user not linked."); } - await botClient.SendPossiblyLongTextMessageAsync(message.Chat.Id, - replyMarkdownV2, - parseMode: ParseMode.MarkdownV2, - disableWebPagePreview: true, - replyToMessageId: message.MessageId, - cancellationToken: cancellationToken); + _ = await botClient.SendPossiblyLongTextMessageAsync( + message.Chat.Id, + replyMarkdownV2, + parseMode: ParseMode.MarkdownV2, + linkPreviewOptions: new() { IsDisabled = true, }, + replyParameters: message, + cancellationToken: cancellationToken); } public static async Task GetCredentialsAsync(ITelegramBotClient botClient, Message message, string? argument, CancellationToken cancellationToken = default) @@ -258,11 +260,12 @@ public static async Task GetCredentialsAsync(ITelegramBotClient botClient, Messa Console.WriteLine(" Response: user not linked."); } - await botClient.SendPossiblyLongTextMessageAsync(message.Chat.Id, - replyMarkdownV2, - parseMode: ParseMode.MarkdownV2, - replyToMessageId: message.MessageId, - cancellationToken: cancellationToken); + _ = await botClient.SendPossiblyLongTextMessageAsync( + message.Chat.Id, + replyMarkdownV2, + parseMode: ParseMode.MarkdownV2, + replyParameters: message, + cancellationToken: cancellationToken); } } } diff --git a/ShadowsocksUriGenerator.Chatbot.Telegram/Commands/DataCommands.cs b/ShadowsocksUriGenerator.Chatbot.Telegram/Commands/DataCommands.cs index 5e22a39..72a1762 100644 --- a/ShadowsocksUriGenerator.Chatbot.Telegram/Commands/DataCommands.cs +++ b/ShadowsocksUriGenerator.Chatbot.Telegram/Commands/DataCommands.cs @@ -144,11 +144,12 @@ public static async Task GetUserDataUsageAsync(ITelegramBotClient botClient, Mes Console.WriteLine(" Response: user not linked."); } - await botClient.SendPossiblyLongTextMessageAsync(message.Chat.Id, - replyMarkdownV2, - parseMode: ParseMode.MarkdownV2, - replyToMessageId: message.MessageId, - cancellationToken: cancellationToken); + _ = await botClient.SendPossiblyLongTextMessageAsync( + message.Chat.Id, + replyMarkdownV2, + parseMode: ParseMode.MarkdownV2, + replyParameters: message, + cancellationToken: cancellationToken); } public static async Task GetUserDataLimitAsync(ITelegramBotClient botClient, Message message, string? argument, CancellationToken cancellationToken = default) @@ -253,11 +254,12 @@ public static async Task GetUserDataLimitAsync(ITelegramBotClient botClient, Mes Console.WriteLine(" Response: user not linked."); } - await botClient.SendPossiblyLongTextMessageAsync(message.Chat.Id, - replyMarkdownV2, - parseMode: ParseMode.MarkdownV2, - replyToMessageId: message.MessageId, - cancellationToken: cancellationToken); + _ = await botClient.SendPossiblyLongTextMessageAsync( + message.Chat.Id, + replyMarkdownV2, + parseMode: ParseMode.MarkdownV2, + replyParameters: message, + cancellationToken: cancellationToken); } public static async Task GetGroupDataUsageAsync(ITelegramBotClient botClient, Message message, string? argument, CancellationToken cancellationToken = default) @@ -346,11 +348,12 @@ public static async Task GetGroupDataUsageAsync(ITelegramBotClient botClient, Me Console.WriteLine(" Response: user not linked."); } - await botClient.SendPossiblyLongTextMessageAsync(message.Chat.Id, - replyMarkdownV2, - parseMode: ParseMode.MarkdownV2, - replyToMessageId: message.MessageId, - cancellationToken: cancellationToken); + _ = await botClient.SendPossiblyLongTextMessageAsync( + message.Chat.Id, + replyMarkdownV2, + parseMode: ParseMode.MarkdownV2, + replyParameters: message, + cancellationToken: cancellationToken); } private static void GetGroupDataUsageCore(StringBuilder replyBuilder, KeyValuePair groupEntry) @@ -513,11 +516,12 @@ public static async Task GetGroupDataLimitAsync(ITelegramBotClient botClient, Me Console.WriteLine(" Response: user not linked."); } - await botClient.SendPossiblyLongTextMessageAsync(message.Chat.Id, - replyMarkdownV2, - parseMode: ParseMode.MarkdownV2, - replyToMessageId: message.MessageId, - cancellationToken: cancellationToken); + _ = await botClient.SendPossiblyLongTextMessageAsync( + message.Chat.Id, + replyMarkdownV2, + parseMode: ParseMode.MarkdownV2, + replyParameters: message, + cancellationToken: cancellationToken); } } } diff --git a/ShadowsocksUriGenerator.Chatbot.Telegram/Commands/ListCommands.cs b/ShadowsocksUriGenerator.Chatbot.Telegram/Commands/ListCommands.cs index c480081..2a611a8 100644 --- a/ShadowsocksUriGenerator.Chatbot.Telegram/Commands/ListCommands.cs +++ b/ShadowsocksUriGenerator.Chatbot.Telegram/Commands/ListCommands.cs @@ -76,11 +76,12 @@ public static async Task ListUsersAsync(ITelegramBotClient botClient, Message me Console.WriteLine(" Response: user not linked."); } - await botClient.SendPossiblyLongTextMessageAsync(message.Chat.Id, - replyMarkdownV2, - parseMode: ParseMode.MarkdownV2, - replyToMessageId: message.MessageId, - cancellationToken: cancellationToken); + _ = await botClient.SendPossiblyLongTextMessageAsync( + message.Chat.Id, + replyMarkdownV2, + parseMode: ParseMode.MarkdownV2, + replyParameters: message, + cancellationToken: cancellationToken); } public static async Task ListNodesAsync(ITelegramBotClient botClient, Message message, string? argument, CancellationToken cancellationToken = default) @@ -203,11 +204,12 @@ public static async Task ListNodesAsync(ITelegramBotClient botClient, Message me Console.WriteLine(" Response: user not linked."); } - await botClient.SendPossiblyLongTextMessageAsync(message.Chat.Id, - replyMarkdownV2, - parseMode: ParseMode.MarkdownV2, - replyToMessageId: message.MessageId, - cancellationToken: cancellationToken); + _ = await botClient.SendPossiblyLongTextMessageAsync( + message.Chat.Id, + replyMarkdownV2, + parseMode: ParseMode.MarkdownV2, + replyParameters: message, + cancellationToken: cancellationToken); } public static async Task ListOwnedNodesAsync(ITelegramBotClient botClient, Message message, string? argument, CancellationToken cancellationToken = default) @@ -306,11 +308,12 @@ public static async Task ListOwnedNodesAsync(ITelegramBotClient botClient, Messa Console.WriteLine(" Response: user not linked."); } - await botClient.SendPossiblyLongTextMessageAsync(message.Chat.Id, - replyMarkdownV2, - parseMode: ParseMode.MarkdownV2, - replyToMessageId: message.MessageId, - cancellationToken: cancellationToken); + _ = await botClient.SendPossiblyLongTextMessageAsync( + message.Chat.Id, + replyMarkdownV2, + parseMode: ParseMode.MarkdownV2, + replyParameters: message, + cancellationToken: cancellationToken); } public static async Task ListGroupsAsync(ITelegramBotClient botClient, Message message, CancellationToken cancellationToken = default) @@ -410,11 +413,12 @@ public static async Task ListGroupsAsync(ITelegramBotClient botClient, Message m Console.WriteLine(" Response: user not linked."); } - await botClient.SendPossiblyLongTextMessageAsync(message.Chat.Id, - replyMarkdownV2, - parseMode: ParseMode.MarkdownV2, - replyToMessageId: message.MessageId, - cancellationToken: cancellationToken); + _ = await botClient.SendPossiblyLongTextMessageAsync( + message.Chat.Id, + replyMarkdownV2, + parseMode: ParseMode.MarkdownV2, + replyParameters: message, + cancellationToken: cancellationToken); } public static async Task ListOwnedGroupsAsync(ITelegramBotClient botClient, Message message, string? argument, CancellationToken cancellationToken = default) @@ -499,11 +503,12 @@ public static async Task ListOwnedGroupsAsync(ITelegramBotClient botClient, Mess Console.WriteLine(" Response: user not linked."); } - await botClient.SendPossiblyLongTextMessageAsync(message.Chat.Id, - replyMarkdownV2, - parseMode: ParseMode.MarkdownV2, - replyToMessageId: message.MessageId, - cancellationToken: cancellationToken); + _ = await botClient.SendPossiblyLongTextMessageAsync( + message.Chat.Id, + replyMarkdownV2, + parseMode: ParseMode.MarkdownV2, + replyParameters: message, + cancellationToken: cancellationToken); } public static async Task ListGroupMembersAsync(ITelegramBotClient botClient, Message message, string? argument, CancellationToken cancellationToken = default) @@ -571,11 +576,12 @@ public static async Task ListGroupMembersAsync(ITelegramBotClient botClient, Mes Console.WriteLine(" Response: user not linked."); } - await botClient.SendPossiblyLongTextMessageAsync(message.Chat.Id, - replyMarkdownV2, - parseMode: ParseMode.MarkdownV2, - replyToMessageId: message.MessageId, - cancellationToken: cancellationToken); + _ = await botClient.SendPossiblyLongTextMessageAsync( + message.Chat.Id, + replyMarkdownV2, + parseMode: ParseMode.MarkdownV2, + replyParameters: message, + cancellationToken: cancellationToken); } } } diff --git a/ShadowsocksUriGenerator.Chatbot.Telegram/Commands/ReportCommand.cs b/ShadowsocksUriGenerator.Chatbot.Telegram/Commands/ReportCommand.cs index 97b6175..0b7ff11 100644 --- a/ShadowsocksUriGenerator.Chatbot.Telegram/Commands/ReportCommand.cs +++ b/ShadowsocksUriGenerator.Chatbot.Telegram/Commands/ReportCommand.cs @@ -100,22 +100,25 @@ public static async Task GenerateReportAsync(ITelegramBotClient botClient, Messa var (dataUsageByGroup, dataUsageByUser) = ReportHelper.GenerateDataUsageCSV(recordsByGroup, recordsByUser); Console.WriteLine(" Response: successful query."); - var sendSummaryTask = botClient.SendTextMessageAsync(message.Chat.Id, - replyMarkdownV2, - parseMode: ParseMode.MarkdownV2, - cancellationToken: cancellationToken); - - var sendDataUsageByGroup = botClient.SendTextFileFromStringAsync(message.Chat.Id, - "data-usage-by-group.csv", - dataUsageByGroup, - caption: "Data usage by group", - cancellationToken: cancellationToken); - - var sendDataUsageByUser = botClient.SendTextFileFromStringAsync(message.Chat.Id, - "data-usage-by-user.csv", - dataUsageByUser, - caption: "Data usage by user", - cancellationToken: cancellationToken); + Task sendSummaryTask = botClient.SendMessage( + message.Chat.Id, + replyMarkdownV2, + parseMode: ParseMode.MarkdownV2, + cancellationToken: cancellationToken); + + Task sendDataUsageByGroup = botClient.SendTextFileFromStringAsync( + message.Chat.Id, + "data-usage-by-group.csv", + dataUsageByGroup, + caption: "Data usage by group", + cancellationToken: cancellationToken); + + Task sendDataUsageByUser = botClient.SendTextFileFromStringAsync( + message.Chat.Id, + "data-usage-by-user.csv", + dataUsageByUser, + caption: "Data usage by user", + cancellationToken: cancellationToken); await Task.WhenAll(sendSummaryTask, sendDataUsageByGroup, sendDataUsageByUser); return; @@ -235,21 +238,24 @@ public static async Task GenerateReportAsync(ITelegramBotClient botClient, Messa replyMarkdownV2 = replyBuilder.ToString(); Console.WriteLine(" Response: successful query."); - await botClient.SendTextMessageAsync(message.Chat.Id, - replyMarkdownV2, - parseMode: ParseMode.MarkdownV2, - replyToMessageId: message.MessageId, - cancellationToken: cancellationToken); - - await botClient.SendPossiblyLongTextMessageAsync(message.Chat.Id, - byGroupSB.ToString(), - parseMode: ParseMode.MarkdownV2, - cancellationToken: cancellationToken); - - await botClient.SendPossiblyLongTextMessageAsync(message.Chat.Id, - byUserSB.ToString(), - parseMode: ParseMode.MarkdownV2, - cancellationToken: cancellationToken); + _ = await botClient.SendMessage( + message.Chat.Id, + replyMarkdownV2, + parseMode: ParseMode.MarkdownV2, + replyParameters: message, + cancellationToken: cancellationToken); + + _ = await botClient.SendPossiblyLongTextMessageAsync( + message.Chat.Id, + byGroupSB.ToString(), + parseMode: ParseMode.MarkdownV2, + cancellationToken: cancellationToken); + + _ = await botClient.SendPossiblyLongTextMessageAsync( + message.Chat.Id, + byUserSB.ToString(), + parseMode: ParseMode.MarkdownV2, + cancellationToken: cancellationToken); return; } @@ -260,11 +266,12 @@ await botClient.SendPossiblyLongTextMessageAsync(message.Chat.Id, Console.WriteLine(" Response: user not linked."); } - await botClient.SendTextMessageAsync(message.Chat.Id, - replyMarkdownV2, - parseMode: ParseMode.MarkdownV2, - replyToMessageId: message.MessageId, - cancellationToken: cancellationToken); + _ = await botClient.SendMessage( + message.Chat.Id, + replyMarkdownV2, + parseMode: ParseMode.MarkdownV2, + replyParameters: message, + cancellationToken: cancellationToken); } } } diff --git a/ShadowsocksUriGenerator.Chatbot.Telegram/ShadowsocksUriGenerator.Chatbot.Telegram.csproj b/ShadowsocksUriGenerator.Chatbot.Telegram/ShadowsocksUriGenerator.Chatbot.Telegram.csproj index 60e490c..d48cab1 100644 --- a/ShadowsocksUriGenerator.Chatbot.Telegram/ShadowsocksUriGenerator.Chatbot.Telegram.csproj +++ b/ShadowsocksUriGenerator.Chatbot.Telegram/ShadowsocksUriGenerator.Chatbot.Telegram.csproj @@ -29,7 +29,7 @@ - + diff --git a/ShadowsocksUriGenerator.Chatbot.Telegram/Utils/ChatHelper.cs b/ShadowsocksUriGenerator.Chatbot.Telegram/Utils/ChatHelper.cs index 42f7bff..79c8486 100644 --- a/ShadowsocksUriGenerator.Chatbot.Telegram/Utils/ChatHelper.cs +++ b/ShadowsocksUriGenerator.Chatbot.Telegram/Utils/ChatHelper.cs @@ -19,104 +19,106 @@ public static partial class ChatHelper /// Short messages are sent as text messages. /// Long messages are sent as text files. /// - /// - public static Task SendPossiblyLongTextMessageAsync( + /// + public static Task SendPossiblyLongTextMessageAsync( this ITelegramBotClient botClient, ChatId chatId, string text, - int? messageThreadId = null, - ParseMode? parseMode = null, - IEnumerable? entities = null, - bool? disableWebPagePreview = null, - bool? disableNotification = null, - bool? protectContent = null, - int? replyToMessageId = null, - bool? allowSendingWithoutReply = null, - IReplyMarkup? replyMarkup = null, + ParseMode parseMode = default, + ReplyParameters? replyParameters = default, + IReplyMarkup? replyMarkup = default, + LinkPreviewOptions? linkPreviewOptions = default, + int? messageThreadId = default, + IEnumerable? entities = default, + bool disableNotification = default, + bool protectContent = default, + string? messageEffectId = default, + string? businessConnectionId = default, + bool allowPaidBroadcast = default, CancellationToken cancellationToken = default) - { - if (text.Length <= 4096) - { - return botClient.SendTextMessageAsync(chatId, - text, - messageThreadId, - parseMode, - entities, - disableWebPagePreview, - disableNotification, - protectContent, - replyToMessageId, - allowSendingWithoutReply, - replyMarkup, - cancellationToken); - } - else // too large, send as file + => text.Length switch { - var filename = parseMode switch - { - ParseMode.Markdown => "long-message.md", - ParseMode.Html => "long-message.html", - ParseMode.MarkdownV2 => "long-message.md", - _ => "long-message.txt", - }; - - return botClient.SendTextFileFromStringAsync(chatId, - filename, - text, - messageThreadId, - null, - null, - parseMode, - entities, - null, - disableNotification, - protectContent, - replyToMessageId, - null, - replyMarkup, - cancellationToken); - } - } + <= 4096 => botClient.SendMessage( + chatId, + text, + parseMode, + replyParameters, + replyMarkup, + linkPreviewOptions, + messageThreadId, + entities, + disableNotification, + protectContent, + messageEffectId, + businessConnectionId, + allowPaidBroadcast, + cancellationToken), + _ => botClient.SendTextFileFromStringAsync( + chatId, + parseMode switch + { + ParseMode.Markdown => "long-message.md", + ParseMode.Html => "long-message.html", + ParseMode.MarkdownV2 => "long-message.md", + _ => "long-message.txt", + }, + text, + parseMode: parseMode, + replyParameters: replyParameters, + replyMarkup: replyMarkup, + messageThreadId: messageThreadId, + disableNotification: disableNotification, + protectContent: protectContent, + messageEffectId: messageEffectId, + businessConnectionId: businessConnectionId, + allowPaidBroadcast: allowPaidBroadcast, + cancellationToken: cancellationToken) + }; /// /// Sends a string as a text file. /// /// Filename. /// The string to send. - /// + /// public static async Task SendTextFileFromStringAsync( this ITelegramBotClient botClient, ChatId chatId, string filename, string text, - int? messageThreadId = null, - InputFile? thumbnail = null, - string? caption = null, - ParseMode? parseMode = null, - IEnumerable? captionEntities = null, - bool? disableContentTypeDetection = null, - bool? disableNotification = null, - bool? protectContent = null, - int? replyToMessageId = null, - bool? allowSendingWithoutReply = null, - IReplyMarkup? replyMarkup = null, + string? caption = default, + ParseMode parseMode = default, + ReplyParameters? replyParameters = default, + IReplyMarkup? replyMarkup = default, + InputFile? thumbnail = default, + int? messageThreadId = default, + IEnumerable? captionEntities = default, + bool disableContentTypeDetection = default, + bool disableNotification = default, + bool protectContent = default, + string? messageEffectId = default, + string? businessConnectionId = default, + bool allowPaidBroadcast = default, CancellationToken cancellationToken = default) { await using var stream = new MemoryStream(Encoding.UTF8.GetBytes(text)); - return await botClient.SendDocumentAsync(chatId, - InputFile.FromStream(stream, filename), - messageThreadId, - thumbnail, - caption, - parseMode, - captionEntities, - disableContentTypeDetection, - disableNotification, - protectContent, - replyToMessageId, - allowSendingWithoutReply, - replyMarkup, - cancellationToken); + return await botClient.SendDocument( + chatId, + InputFile.FromStream(stream, filename), + caption, + parseMode, + replyParameters, + replyMarkup, + thumbnail, + messageThreadId, + captionEntities, + disableContentTypeDetection, + disableNotification, + protectContent, + messageEffectId, + businessConnectionId, + allowPaidBroadcast, + cancellationToken); } [GeneratedRegex("[_*[\\]()~`>#+\\-=|{}.!]")]