Skip to content

Commit

Permalink
⤴️ Update Telegram.Bot to v22
Browse files Browse the repository at this point in the history
  • Loading branch information
database64128 committed Nov 5, 2024
1 parent de507ff commit 2cc23cb
Show file tree
Hide file tree
Showing 8 changed files with 223 additions and 198 deletions.
6 changes: 3 additions & 3 deletions ShadowsocksUriGenerator.Chatbot.Telegram/CLI/BotRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ public static async Task<int> 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}).");
Expand Down
29 changes: 16 additions & 13 deletions ShadowsocksUriGenerator.Chatbot.Telegram/Commands/AuthCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ public static Task StartAsync(ITelegramBotClient botClient, Message message, Bot
✈️ To get your boarding pass, please use `/link <UUID>` 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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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);
}
}
}
35 changes: 19 additions & 16 deletions ShadowsocksUriGenerator.Chatbot.Telegram/Commands/CredCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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);
}
}
}
44 changes: 24 additions & 20 deletions ShadowsocksUriGenerator.Chatbot.Telegram/Commands/DataCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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<string, Group> groupEntry)
Expand Down Expand Up @@ -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);
}
}
}
66 changes: 36 additions & 30 deletions ShadowsocksUriGenerator.Chatbot.Telegram/Commands/ListCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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);
}
}
}
Loading

0 comments on commit 2cc23cb

Please sign in to comment.