From e5b59e21eb28c281cc6185b14ded9100257cd1e5 Mon Sep 17 00:00:00 2001 From: Wizou <11647984+wiz0u@users.noreply.github.com> Date: Mon, 4 Nov 2024 01:02:06 +0100 Subject: [PATCH] Update Examples for v22.0.0 --- .../Abstract/ReceiverServiceBase.cs | 2 +- Console.Advanced/Console.Advanced.csproj | 2 +- Console.Advanced/README.md | 2 +- Console.Advanced/Services/UpdateHandler.cs | 32 +++++++++---------- Console/Console.csproj | 2 +- Console/Program.cs | 31 +++++++++--------- FSharp.Example/FSharp.Example.fsproj | 2 +- .../Services/Internal/UpdateHandlerFuncs.fs | 24 +++++++------- FSharp.Example/Services/ReceiverService.fs | 2 +- InlineQueries/InlineQueries.csproj | 2 +- InlineQueries/Program.cs | 4 +-- MiniApp/Cafe.cs | 6 ++-- MiniApp/MiniApp.csproj | 2 +- MiniApp/Program.cs | 10 +++--- .../lambda-bot/UpdateService.cs | 8 ++--- .../lambda-bot/lambda-bot.csproj | 2 +- ...reFunctions.IsolatedProcess.Webhook.csproj | 2 +- .../UpdateService.cs | 2 +- .../AzureFunctions.Webhook.csproj | 4 +-- .../AzureFunctions.Webhook/UpdateService.cs | 2 +- .../Controllers/BotController.cs | 2 +- Webhook.Controllers/Services/UpdateHandler.cs | 32 +++++++++---------- .../Webhook.Controllers.csproj | 2 +- Webhook.MinimalAPIs/Program.cs | 4 +-- .../Webhook.MinimalAPIs.csproj | 2 +- 25 files changed, 93 insertions(+), 92 deletions(-) diff --git a/Console.Advanced/Abstract/ReceiverServiceBase.cs b/Console.Advanced/Abstract/ReceiverServiceBase.cs index 25ffccd7..3a053a7f 100644 --- a/Console.Advanced/Abstract/ReceiverServiceBase.cs +++ b/Console.Advanced/Abstract/ReceiverServiceBase.cs @@ -39,7 +39,7 @@ public async Task ReceiveAsync(CancellationToken stoppingToken) DropPendingUpdates = true, }; - var me = await _botClient.GetMeAsync(stoppingToken); + var me = await _botClient.GetMe(stoppingToken); _logger.LogInformation("Start receiving updates for {BotName}", me.Username ?? "My Awesome Bot"); // Start receiving updates diff --git a/Console.Advanced/Console.Advanced.csproj b/Console.Advanced/Console.Advanced.csproj index 88c81aab..74f9ca7e 100644 --- a/Console.Advanced/Console.Advanced.csproj +++ b/Console.Advanced/Console.Advanced.csproj @@ -11,7 +11,7 @@ - + diff --git a/Console.Advanced/README.md b/Console.Advanced/README.md index aec10128..79c26586 100644 --- a/Console.Advanced/README.md +++ b/Console.Advanced/README.md @@ -27,7 +27,7 @@ Make sure that your .csproj contains these items (versions may vary): ```xml - + ``` diff --git a/Console.Advanced/Services/UpdateHandler.cs b/Console.Advanced/Services/UpdateHandler.cs index ea2f3502..aeb8c453 100644 --- a/Console.Advanced/Services/UpdateHandler.cs +++ b/Console.Advanced/Services/UpdateHandler.cs @@ -59,7 +59,7 @@ private async Task OnMessage(Message msg) "/throw" => FailingHandler(msg), _ => Usage(msg) }); - logger.LogInformation("The message was sent with id: {SentMessageId}", sentMessage.MessageId); + logger.LogInformation("The message was sent with id: {SentMessageId}", sentMessage.Id); } async Task Usage(Message msg) @@ -76,15 +76,15 @@ async Task Usage(Message msg) /poll_anonymous - send an anonymous poll /throw - what happens if handler fails """; - return await bot.SendTextMessageAsync(msg.Chat, usage, parseMode: ParseMode.Html, replyMarkup: new ReplyKeyboardRemove()); + return await bot.SendMessage(msg.Chat, usage, parseMode: ParseMode.Html, replyMarkup: new ReplyKeyboardRemove()); } async Task SendPhoto(Message msg) { - await bot.SendChatActionAsync(msg.Chat, ChatAction.UploadPhoto); + await bot.SendChatAction(msg.Chat, ChatAction.UploadPhoto); await Task.Delay(2000); // simulate a long task await using var fileStream = new FileStream("Files/bot.gif", FileMode.Open, FileAccess.Read); - return await bot.SendPhotoAsync(msg.Chat, fileStream, caption: "Read https://telegrambots.github.io/book/"); + return await bot.SendPhoto(msg.Chat, fileStream, caption: "Read https://telegrambots.github.io/book/"); } // Send inline keyboard. You can process responses in OnCallbackQuery handler @@ -95,7 +95,7 @@ async Task SendInlineKeyboard(Message msg) .AddNewRow() .AddButton("WithCallbackData", "CallbackData") .AddButton(InlineKeyboardButton.WithUrl("WithUrl", "https://github.com/TelegramBots/Telegram.Bot")); - return await bot.SendTextMessageAsync(msg.Chat, "Inline buttons:", replyMarkup: inlineMarkup); + return await bot.SendMessage(msg.Chat, "Inline buttons:", replyMarkup: inlineMarkup); } async Task SendReplyKeyboard(Message msg) @@ -103,12 +103,12 @@ async Task SendReplyKeyboard(Message msg) var replyMarkup = new ReplyKeyboardMarkup(true) .AddNewRow("1.1", "1.2", "1.3") .AddNewRow().AddButton("2.1").AddButton("2.2"); - return await bot.SendTextMessageAsync(msg.Chat, "Keyboard buttons:", replyMarkup: replyMarkup); + return await bot.SendMessage(msg.Chat, "Keyboard buttons:", replyMarkup: replyMarkup); } async Task RemoveKeyboard(Message msg) { - return await bot.SendTextMessageAsync(msg.Chat, "Removing keyboard", replyMarkup: new ReplyKeyboardRemove()); + return await bot.SendMessage(msg.Chat, "Removing keyboard", replyMarkup: new ReplyKeyboardRemove()); } async Task RequestContactAndLocation(Message msg) @@ -116,24 +116,24 @@ async Task RequestContactAndLocation(Message msg) var replyMarkup = new ReplyKeyboardMarkup(true) .AddButton(KeyboardButton.WithRequestLocation("Location")) .AddButton(KeyboardButton.WithRequestContact("Contact")); - return await bot.SendTextMessageAsync(msg.Chat, "Who or Where are you?", replyMarkup: replyMarkup); + return await bot.SendMessage(msg.Chat, "Who or Where are you?", replyMarkup: replyMarkup); } async Task StartInlineQuery(Message msg) { var button = InlineKeyboardButton.WithSwitchInlineQueryCurrentChat("Inline Mode"); - return await bot.SendTextMessageAsync(msg.Chat, "Press the button to start Inline Query\n\n" + + return await bot.SendMessage(msg.Chat, "Press the button to start Inline Query\n\n" + "(Make sure you enabled Inline Mode in @BotFather)", replyMarkup: new InlineKeyboardMarkup(button)); } async Task SendPoll(Message msg) { - return await bot.SendPollAsync(msg.Chat, "Question", PollOptions, isAnonymous: false); + return await bot.SendPoll(msg.Chat, "Question", PollOptions, isAnonymous: false); } async Task SendAnonymousPoll(Message msg) { - return await bot.SendPollAsync(chatId: msg.Chat, "Question", PollOptions); + return await bot.SendPoll(chatId: msg.Chat, "Question", PollOptions); } static Task FailingHandler(Message msg) @@ -145,8 +145,8 @@ static Task FailingHandler(Message msg) private async Task OnCallbackQuery(CallbackQuery callbackQuery) { logger.LogInformation("Received inline keyboard callback from: {CallbackQueryId}", callbackQuery.Id); - await bot.AnswerCallbackQueryAsync(callbackQuery.Id, $"Received {callbackQuery.Data}"); - await bot.SendTextMessageAsync(callbackQuery.Message!.Chat, $"Received {callbackQuery.Data}"); + await bot.AnswerCallbackQuery(callbackQuery.Id, $"Received {callbackQuery.Data}"); + await bot.SendMessage(callbackQuery.Message!.Chat, $"Received {callbackQuery.Data}"); } #region Inline Mode @@ -159,13 +159,13 @@ private async Task OnInlineQuery(InlineQuery inlineQuery) new InlineQueryResultArticle("1", "Telegram.Bot", new InputTextMessageContent("hello")), new InlineQueryResultArticle("2", "is the best", new InputTextMessageContent("world")) ]; - await bot.AnswerInlineQueryAsync(inlineQuery.Id, results, cacheTime: 0, isPersonal: true); + await bot.AnswerInlineQuery(inlineQuery.Id, results, cacheTime: 0, isPersonal: true); } private async Task OnChosenInlineResult(ChosenInlineResult chosenInlineResult) { logger.LogInformation("Received inline result: {ChosenInlineResultId}", chosenInlineResult.ResultId); - await bot.SendTextMessageAsync(chosenInlineResult.From.Id, $"You chose result with Id: {chosenInlineResult.ResultId}"); + await bot.SendMessage(chosenInlineResult.From.Id, $"You chose result with Id: {chosenInlineResult.ResultId}"); } #endregion @@ -181,7 +181,7 @@ private async Task OnPollAnswer(PollAnswer pollAnswer) var answer = pollAnswer.OptionIds.FirstOrDefault(); var selectedOption = PollOptions[answer]; if (pollAnswer.User != null) - await bot.SendTextMessageAsync(pollAnswer.User.Id, $"You've chosen: {selectedOption.Text} in poll"); + await bot.SendMessage(pollAnswer.User.Id, $"You've chosen: {selectedOption.Text} in poll"); } private Task UnknownUpdateHandlerAsync(Update update) diff --git a/Console/Console.csproj b/Console/Console.csproj index 140723d4..101901a0 100644 --- a/Console/Console.csproj +++ b/Console/Console.csproj @@ -8,7 +8,7 @@ - + diff --git a/Console/Program.cs b/Console/Program.cs index 62879fb5..e0abbc5c 100644 --- a/Console/Program.cs +++ b/Console/Program.cs @@ -9,8 +9,9 @@ using var cts = new CancellationTokenSource(); var bot = new TelegramBotClient(token, cancellationToken: cts.Token); -var me = await bot.GetMeAsync(); -await bot.DropPendingUpdatesAsync(); +var me = await bot.GetMe(); +await bot.DeleteWebhook(); // you may comment this line if you find it unnecessary +await bot.DropPendingUpdates(); // you may comment this line if you find it unnecessary bot.OnError += OnError; bot.OnMessage += OnMessage; bot.OnUpdate += OnUpdate; @@ -58,7 +59,7 @@ async Task OnCommand(string command, string args, Message msg) switch (command) { case "/start": - await bot.SendTextMessageAsync(msg.Chat, """ + await bot.SendMessage(msg.Chat, """ Bot menu: /photo [url] - send a photo (optionally from an url) /inline_buttons - send inline buttons @@ -71,13 +72,13 @@ await bot.SendTextMessageAsync(msg.Chat, """ break; case "/photo": if (args.StartsWith("http")) - await bot.SendPhotoAsync(msg.Chat, args, caption: "Source: " + args); + await bot.SendPhoto(msg.Chat, args, caption: "Source: " + args); else { - await bot.SendChatActionAsync(msg.Chat, ChatAction.UploadPhoto); + await bot.SendChatAction(msg.Chat, ChatAction.UploadPhoto); await Task.Delay(2000); // simulate a long task await using var fileStream = new FileStream("bot.gif", FileMode.Open, FileAccess.Read); - await bot.SendPhotoAsync(msg.Chat, fileStream, caption: "Read https://telegrambots.github.io/book/"); + await bot.SendPhoto(msg.Chat, fileStream, caption: "Read https://telegrambots.github.io/book/"); } break; case "/inline_buttons": @@ -86,22 +87,22 @@ await bot.SendTextMessageAsync(msg.Chat, """ .AddNewRow() .AddButton("WithCallbackData", "CallbackData") .AddButton(InlineKeyboardButton.WithUrl("WithUrl", "https://github.com/TelegramBots/Telegram.Bot")); - await bot.SendTextMessageAsync(msg.Chat, "Inline buttons:", replyMarkup: inlineMarkup); + await bot.SendMessage(msg.Chat, "Inline buttons:", replyMarkup: inlineMarkup); break; case "/keyboard": - var replyMarkup = new ReplyKeyboardMarkup(true) + var replyMarkup = new ReplyKeyboardMarkup() .AddNewRow("1.1", "1.2", "1.3") .AddNewRow().AddButton("2.1").AddButton("2.2"); - await bot.SendTextMessageAsync(msg.Chat, "Keyboard buttons:", replyMarkup: replyMarkup); + await bot.SendMessage(msg.Chat, "Keyboard buttons:", replyMarkup: replyMarkup); break; case "/remove": - await bot.SendTextMessageAsync(msg.Chat, "Removing keyboard", replyMarkup: new ReplyKeyboardRemove()); + await bot.SendMessage(msg.Chat, "Removing keyboard", replyMarkup: new ReplyKeyboardRemove()); break; case "/poll": - await bot.SendPollAsync(msg.Chat, "Question", ["Option 0", "Option 1", "Option 2"], isAnonymous: false, allowsMultipleAnswers: true); + await bot.SendPoll(msg.Chat, "Question", ["Option 0", "Option 1", "Option 2"], isAnonymous: false, allowsMultipleAnswers: true); break; case "/reaction": - await bot.SetMessageReactionAsync(msg.Chat, msg.MessageId, ["❤"], false); + await bot.SetMessageReaction(msg.Chat, msg.Id, ["❤"], false); break; } } @@ -118,12 +119,12 @@ async Task OnUpdate(Update update) async Task OnCallbackQuery(CallbackQuery callbackQuery) { - await bot.AnswerCallbackQueryAsync(callbackQuery.Id, $"You selected {callbackQuery.Data}"); - await bot.SendTextMessageAsync(callbackQuery.Message!.Chat, $"Received callback from inline button {callbackQuery.Data}"); + await bot.AnswerCallbackQuery(callbackQuery.Id, $"You selected {callbackQuery.Data}"); + await bot.SendMessage(callbackQuery.Message!.Chat, $"Received callback from inline button {callbackQuery.Data}"); } async Task OnPollAnswer(PollAnswer pollAnswer) { if (pollAnswer.User != null) - await bot.SendTextMessageAsync(pollAnswer.User.Id, $"You voted for option(s) id [{string.Join(',', pollAnswer.OptionIds)}]"); + await bot.SendMessage(pollAnswer.User.Id, $"You voted for option(s) id [{string.Join(',', pollAnswer.OptionIds)}]"); } diff --git a/FSharp.Example/FSharp.Example.fsproj b/FSharp.Example/FSharp.Example.fsproj index 19c1749b..80b57412 100644 --- a/FSharp.Example/FSharp.Example.fsproj +++ b/FSharp.Example/FSharp.Example.fsproj @@ -20,6 +20,6 @@ - + diff --git a/FSharp.Example/Services/Internal/UpdateHandlerFuncs.fs b/FSharp.Example/Services/Internal/UpdateHandlerFuncs.fs index 74c79147..646f40b9 100644 --- a/FSharp.Example/Services/Internal/UpdateHandlerFuncs.fs +++ b/FSharp.Example/Services/Internal/UpdateHandlerFuncs.fs @@ -27,7 +27,7 @@ open FSharp.Example.Util module UpdateHandlerFuncs = module private BotTextMessages = let sendInlineKeyboard (botClient:ITelegramBotClient) (logger: ILogger) (cts: CancellationToken) (message:Message) = - botClient.SendChatActionAsync( + botClient.SendChatAction( chatId = message.Chat.Id, action = ChatAction.Typing, cancellationToken = cts) |> Async.AwaitTask |> ignore @@ -46,7 +46,7 @@ module UpdateHandlerFuncs = } } - botClient.SendTextMessageAsync( + botClient.SendMessage( chatId = message.Chat.Id, text = "Choose", replyMarkup = InlineKeyboardMarkup(inlineKeyboard), @@ -64,7 +64,7 @@ module UpdateHandlerFuncs = }, ResizeKeyboard = true) - botClient.SendTextMessageAsync( + botClient.SendMessage( chatId = message.Chat.Id, text = "Choose", replyMarkup = replyKeyboardMarkup, @@ -72,7 +72,7 @@ module UpdateHandlerFuncs = |> Async.AwaitTask |> Async.Ignore let removeKeyboard (botClient:ITelegramBotClient) (logger: ILogger) (cts: CancellationToken) (message:Message) = - botClient.SendTextMessageAsync( + botClient.SendMessage( chatId = message.Chat.Id, text = "Removing keyboard", replyMarkup = ReplyKeyboardRemove(), @@ -80,7 +80,7 @@ module UpdateHandlerFuncs = |> Async.AwaitTask |> Async.Ignore let sendFile (botClient:ITelegramBotClient) (logger: ILogger) (cts: CancellationToken) (message:Message) = - botClient.SendChatActionAsync( + botClient.SendChatAction( message.Chat.Id, ChatAction.UploadPhoto, cancellationToken = cts) @@ -94,7 +94,7 @@ module UpdateHandlerFuncs = InputFileStream(new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read), fileName) - botClient.SendPhotoAsync( + botClient.SendPhoto( chatId = message.Chat.Id, photo = inputStream, caption = "Nice Picture", @@ -109,7 +109,7 @@ module UpdateHandlerFuncs = }, ResizeKeyboard = true) - botClient.SendTextMessageAsync( + botClient.SendMessage( chatId = message.Chat.Id, text = "Who or Where are you?", replyMarkup = requestReplyKeyboard, @@ -120,7 +120,7 @@ module UpdateHandlerFuncs = let inlineKeyboard = InlineKeyboardMarkup (InlineKeyboardButton.WithSwitchInlineQueryCurrentChat("Inline Mode")) - botClient.SendTextMessageAsync( + botClient.SendMessage( chatId = message.Chat.Id, text = "Press the button to start Inline Query", replyMarkup = inlineKeyboard, @@ -141,7 +141,7 @@ module UpdateHandlerFuncs = "/inline_mode - send keyboard with inline query\n" + "/throw - Simulate a failed bot message handler\n" - botClient.SendTextMessageAsync( + botClient.SendMessage( chatId = message.Chat.Id, text = usage, replyMarkup = ReplyKeyboardRemove(), @@ -169,9 +169,9 @@ module UpdateHandlerFuncs = | _ -> async { return () } let botOnCallbackQueryReceived (botClient:ITelegramBotClient) (logger: ILogger) (cts: CancellationToken) (query:CallbackQuery) = - botClient.AnswerCallbackQueryAsync(query.Id, $"Received {query.Data}") |> Async.AwaitTask |> ignore + botClient.AnswerCallbackQuery(query.Id, $"Received {query.Data}") |> Async.AwaitTask |> ignore - botClient.SendTextMessageAsync( + botClient.SendMessage( chatId = query.Message.Chat.Id, text = $"Received {query.Data}", cancellationToken = cts) |> Async.AwaitTask |> Async.Ignore @@ -188,7 +188,7 @@ module UpdateHandlerFuncs = inputMessageContent = InputTextMessageContent("hello")) } - botClient.AnswerInlineQueryAsync(inlinequery.Id, + botClient.AnswerInlineQuery(inlinequery.Id, results |> Seq.cast, isPersonal = true, cacheTime = 0, diff --git a/FSharp.Example/Services/ReceiverService.fs b/FSharp.Example/Services/ReceiverService.fs index 8a641e3f..9b555cf7 100644 --- a/FSharp.Example/Services/ReceiverService.fs +++ b/FSharp.Example/Services/ReceiverService.fs @@ -32,7 +32,7 @@ type ReceiverService<'T when 'T :> IUpdateHandler>(botClient: ITelegramBotClient ) try - let! me = botClient.GetMeAsync(cts) |> Async.AwaitTask + let! me = botClient.GetMe(cts) |> Async.AwaitTask let username = match me.Username with | null -> "My Awesome Bot" diff --git a/InlineQueries/InlineQueries.csproj b/InlineQueries/InlineQueries.csproj index c72ad0e0..5ae89355 100644 --- a/InlineQueries/InlineQueries.csproj +++ b/InlineQueries/InlineQueries.csproj @@ -8,7 +8,7 @@ - + diff --git a/InlineQueries/Program.cs b/InlineQueries/Program.cs index 42fb4339..5fb43d74 100644 --- a/InlineQueries/Program.cs +++ b/InlineQueries/Program.cs @@ -24,7 +24,7 @@ // start the bot using var cts = new CancellationTokenSource(); var bot = new TelegramBotClient(token, cancellationToken: cts.Token); -var me = await bot.GetMeAsync(); +var me = await bot.GetMe(); bot.OnUpdate += OnUpdate; Console.WriteLine($"Start listening for @{me.Username}"); @@ -73,7 +73,7 @@ async Task OnInlineQuery(ITelegramBotClient botClient, InlineQuery inlineQuery) }); index++; } - await botClient.AnswerInlineQueryAsync(inlineQuery.Id, results); + await botClient.AnswerInlineQuery(inlineQuery.Id, results); } // for this method to be called, you need to enable "Inline feedback" in BotFather (100% if you want to know all your users choices) diff --git a/MiniApp/Cafe.cs b/MiniApp/Cafe.cs index a1cd4332..6aab4e4e 100644 --- a/MiniApp/Cafe.cs +++ b/MiniApp/Cafe.cs @@ -2,7 +2,7 @@ using Telegram.Bot; using Telegram.Bot.Types.Payments; -#pragma warning disable CA1305, IDE0059, IDE1006 +#pragma warning disable CA1305, CA1812, CA1852, IDE0059, IDE1006 internal static class Cafe { @@ -28,8 +28,8 @@ internal static async Task OnCafeApi(TelegramBotClient bot, IConfigurati return new { ok = true, - invoice_url = await bot.CreateInvoiceLinkAsync("Order #12345678", "Perfect lunch from Durger King", - payload.ToString(), config["PaymentProviderToken"], "USD", prices, needName: true, needPhoneNumber: true, needShippingAddress: true) + invoice_url = await bot.CreateInvoiceLink("Order #12345678", "Perfect lunch from Durger King", + payload.ToString(), "USD", prices, config["PaymentProviderToken"], needName: true, needPhoneNumber: true, needShippingAddress: true) }; } diff --git a/MiniApp/MiniApp.csproj b/MiniApp/MiniApp.csproj index 2fc95f9a..6e58394f 100644 --- a/MiniApp/MiniApp.csproj +++ b/MiniApp/MiniApp.csproj @@ -8,7 +8,7 @@ - + diff --git a/MiniApp/Program.cs b/MiniApp/Program.cs index b86f40cd..55c1109b 100644 --- a/MiniApp/Program.cs +++ b/MiniApp/Program.cs @@ -36,7 +36,7 @@ app.MapRazorPages(); // Bot entrypoints: -app.MapGet("/bot/setWebhook", async (TelegramBotClient bot) => { await bot.SetWebhookAsync(webappUrl + "/bot"); return $"Webhook set to {webappUrl}/bot"; }); +app.MapGet("/bot/setWebhook", async (TelegramBotClient bot) => { await bot.SetWebhook(webappUrl + "/bot"); return $"Webhook set to {webappUrl}/bot"; }); app.MapPost("/bot", OnUpdate); // WebAppDemo & DurgerKing backend: (their AJAX don't use XSRF antiforgery but we validate Telegram Data anyway) app.MapPost("/cafe/api", Cafe.OnCafeApi).DisableAntiforgery(); @@ -50,15 +50,15 @@ async void OnUpdate(TelegramBotClient bot, Update update, ILogger logge switch (update) { case { Message.Text: "/start" }: - await bot.SendTextMessageAsync(update.Message.Chat, "Let's get started\n\nTry our Razor Webapp or order your perfect lunch! 🍟", + await bot.SendMessage(update.Message.Chat, "Let's get started\n\nTry our Razor Webapp or order your perfect lunch! 🍟", parseMode: Telegram.Bot.Types.Enums.ParseMode.Html, replyMarkup: (InlineKeyboardMarkup)InlineKeyboardButton.WithWebApp("Launch Mini-App", webappUrl)); break; case { PreCheckoutQuery: { } pcq }: - await bot.AnswerPreCheckoutQueryAsync(pcq.Id, Cafe.OnPreCheckoutQuery(pcq)); + await bot.AnswerPreCheckoutQuery(pcq.Id, Cafe.OnPreCheckoutQuery(pcq)); break; case { Message.SuccessfulPayment: { } sp }: - await bot.SendTextMessageAsync(update.Message.Chat, "Thank you for your 'payment'! Don't worry, your imaginary credit card was not charged. Your order is not on the way."); + await bot.SendMessage(update.Message.Chat, "Thank you for your 'payment'! Don't worry, your imaginary credit card was not charged. Your order is not on the way."); break; case { Message.WebAppData: { } wad }: logger.LogInformation("Received WebAppData: {Data} | button {ButtonText}", wad.Data, wad.ButtonText); @@ -77,7 +77,7 @@ async Task OnDemoApi(TelegramBotClient bot, IConfiguration config, [From case "sendMessage": string? msg_id = form["msg_id"], with_webview = form["with_webview"]; var user = JsonSerializer.Deserialize(query["user"], JsonBotAPI.Options)!; - await bot.SendTextMessageAsync(user.Id, "Hello, World!", + await bot.SendMessage(user.Id, "Hello, World!", replyMarkup: with_webview == "0" ? new ReplyKeyboardRemove() : new ReplyKeyboardMarkup(true).AddButton(KeyboardButton.WithWebApp("Open WebApp", webappUrl + "/demo"))); return new { response = new { ok = true } }; diff --git a/Serverless/AwsLambda.Webhook/lambda-bot/UpdateService.cs b/Serverless/AwsLambda.Webhook/lambda-bot/UpdateService.cs index 0abccc86..f67e3001 100644 --- a/Serverless/AwsLambda.Webhook/lambda-bot/UpdateService.cs +++ b/Serverless/AwsLambda.Webhook/lambda-bot/UpdateService.cs @@ -34,21 +34,21 @@ public async Task EchoAsync(Update update) { case MessageType.Text: // Echo each Message - await botClient.SendTextMessageAsync(message.Chat.Id, message.Text!); + await botClient.SendMessage(message.Chat.Id, message.Text!); break; case MessageType.Photo: // Download Photo string fileId = message.Photo![^1].FileId; - File file = await botClient.GetFileAsync(fileId); + File file = await botClient.GetFile(fileId); string filename = file.FileId + "." + file.FilePath!.Split('.').Last(); await using (FileStream saveImageStream = System.IO.File.Open(filename, FileMode.Create)) { - await botClient.DownloadFileAsync(file.FilePath, saveImageStream); + await botClient.DownloadFile(file.FilePath, saveImageStream); } - await botClient.SendTextMessageAsync(message.Chat.Id, "Thx for the Pics"); + await botClient.SendMessage(message.Chat.Id, "Thx for the Pics"); break; } } diff --git a/Serverless/AwsLambda.Webhook/lambda-bot/lambda-bot.csproj b/Serverless/AwsLambda.Webhook/lambda-bot/lambda-bot.csproj index 8c9269a4..d0ea06bd 100644 --- a/Serverless/AwsLambda.Webhook/lambda-bot/lambda-bot.csproj +++ b/Serverless/AwsLambda.Webhook/lambda-bot/lambda-bot.csproj @@ -14,6 +14,6 @@ - + \ No newline at end of file diff --git a/Serverless/AzureFunctions.IsolatedProcess.Webhook/AzureFunctions.IsolatedProcess.Webhook.csproj b/Serverless/AzureFunctions.IsolatedProcess.Webhook/AzureFunctions.IsolatedProcess.Webhook.csproj index 1bd7ad09..e36477a4 100644 --- a/Serverless/AzureFunctions.IsolatedProcess.Webhook/AzureFunctions.IsolatedProcess.Webhook.csproj +++ b/Serverless/AzureFunctions.IsolatedProcess.Webhook/AzureFunctions.IsolatedProcess.Webhook.csproj @@ -10,7 +10,7 @@ - + diff --git a/Serverless/AzureFunctions.IsolatedProcess.Webhook/UpdateService.cs b/Serverless/AzureFunctions.IsolatedProcess.Webhook/UpdateService.cs index f6be2815..7a842063 100644 --- a/Serverless/AzureFunctions.IsolatedProcess.Webhook/UpdateService.cs +++ b/Serverless/AzureFunctions.IsolatedProcess.Webhook/UpdateService.cs @@ -24,7 +24,7 @@ public async Task EchoAsync(Update update) if (!(update.Message is { } message)) return; _logger.LogInformation("Received Message from {ChatId}", message.Chat.Id); - await _botClient.SendTextMessageAsync( + await _botClient.SendMessage( chatId: message.Chat.Id, text: $"Echo : {message.Text}"); } diff --git a/Serverless/AzureFunctions.Webhook/AzureFunctions.Webhook.csproj b/Serverless/AzureFunctions.Webhook/AzureFunctions.Webhook.csproj index ae8c2baa..802d6c2d 100644 --- a/Serverless/AzureFunctions.Webhook/AzureFunctions.Webhook.csproj +++ b/Serverless/AzureFunctions.Webhook/AzureFunctions.Webhook.csproj @@ -7,10 +7,10 @@ - + - + diff --git a/Serverless/AzureFunctions.Webhook/UpdateService.cs b/Serverless/AzureFunctions.Webhook/UpdateService.cs index 04198bb6..100e865d 100644 --- a/Serverless/AzureFunctions.Webhook/UpdateService.cs +++ b/Serverless/AzureFunctions.Webhook/UpdateService.cs @@ -24,7 +24,7 @@ public async Task EchoAsync(Update update) if (!(update.Message is { } message)) return; _logger.LogInformation("Received Message from {0}", message.Chat.Id); - await _botClient.SendTextMessageAsync( + await _botClient.SendMessage( chatId: message.Chat.Id, text: $"Echo : {message.Text}"); } diff --git a/Webhook.Controllers/Controllers/BotController.cs b/Webhook.Controllers/Controllers/BotController.cs index fbb5bcab..244b673c 100644 --- a/Webhook.Controllers/Controllers/BotController.cs +++ b/Webhook.Controllers/Controllers/BotController.cs @@ -14,7 +14,7 @@ public class BotController(IOptions Config) : ControllerBase public async Task SetWebHook([FromServices] ITelegramBotClient bot, CancellationToken ct) { var webhookUrl = Config.Value.BotWebhookUrl.AbsoluteUri; - await bot.SetWebhookAsync(webhookUrl, allowedUpdates: [], secretToken: Config.Value.SecretToken, cancellationToken: ct); + await bot.SetWebhook(webhookUrl, allowedUpdates: [], secretToken: Config.Value.SecretToken, cancellationToken: ct); return $"Webhook set to {webhookUrl}"; } diff --git a/Webhook.Controllers/Services/UpdateHandler.cs b/Webhook.Controllers/Services/UpdateHandler.cs index d1c58f61..8a9e2eea 100644 --- a/Webhook.Controllers/Services/UpdateHandler.cs +++ b/Webhook.Controllers/Services/UpdateHandler.cs @@ -59,7 +59,7 @@ private async Task OnMessage(Message msg) "/throw" => FailingHandler(msg), _ => Usage(msg) }); - logger.LogInformation("The message was sent with id: {SentMessageId}", sentMessage.MessageId); + logger.LogInformation("The message was sent with id: {SentMessageId}", sentMessage.Id); } async Task Usage(Message msg) @@ -76,15 +76,15 @@ async Task Usage(Message msg) /poll_anonymous - send an anonymous poll /throw - what happens if handler fails """; - return await bot.SendTextMessageAsync(msg.Chat, usage, parseMode: ParseMode.Html, replyMarkup: new ReplyKeyboardRemove()); + return await bot.SendMessage(msg.Chat, usage, parseMode: ParseMode.Html, replyMarkup: new ReplyKeyboardRemove()); } async Task SendPhoto(Message msg) { - await bot.SendChatActionAsync(msg.Chat, ChatAction.UploadPhoto); + await bot.SendChatAction(msg.Chat, ChatAction.UploadPhoto); await Task.Delay(2000); // simulate a long task await using var fileStream = new FileStream("Files/bot.gif", FileMode.Open, FileAccess.Read); - return await bot.SendPhotoAsync(msg.Chat, fileStream, caption: "Read https://telegrambots.github.io/book/"); + return await bot.SendPhoto(msg.Chat, fileStream, caption: "Read https://telegrambots.github.io/book/"); } // Send inline keyboard. You can process responses in OnCallbackQuery handler @@ -95,7 +95,7 @@ async Task SendInlineKeyboard(Message msg) .AddNewRow() .AddButton("WithCallbackData", "CallbackData") .AddButton(InlineKeyboardButton.WithUrl("WithUrl", "https://github.com/TelegramBots/Telegram.Bot")); - return await bot.SendTextMessageAsync(msg.Chat, "Inline buttons:", replyMarkup: inlineMarkup); + return await bot.SendMessage(msg.Chat, "Inline buttons:", replyMarkup: inlineMarkup); } async Task SendReplyKeyboard(Message msg) @@ -103,12 +103,12 @@ async Task SendReplyKeyboard(Message msg) var replyMarkup = new ReplyKeyboardMarkup(true) .AddNewRow("1.1", "1.2", "1.3") .AddNewRow().AddButton("2.1").AddButton("2.2"); - return await bot.SendTextMessageAsync(msg.Chat, "Keyboard buttons:", replyMarkup: replyMarkup); + return await bot.SendMessage(msg.Chat, "Keyboard buttons:", replyMarkup: replyMarkup); } async Task RemoveKeyboard(Message msg) { - return await bot.SendTextMessageAsync(msg.Chat, "Removing keyboard", replyMarkup: new ReplyKeyboardRemove()); + return await bot.SendMessage(msg.Chat, "Removing keyboard", replyMarkup: new ReplyKeyboardRemove()); } async Task RequestContactAndLocation(Message msg) @@ -116,24 +116,24 @@ async Task RequestContactAndLocation(Message msg) var replyMarkup = new ReplyKeyboardMarkup(true) .AddButton(KeyboardButton.WithRequestLocation("Location")) .AddButton(KeyboardButton.WithRequestContact("Contact")); - return await bot.SendTextMessageAsync(msg.Chat, "Who or Where are you?", replyMarkup: replyMarkup); + return await bot.SendMessage(msg.Chat, "Who or Where are you?", replyMarkup: replyMarkup); } async Task StartInlineQuery(Message msg) { var button = InlineKeyboardButton.WithSwitchInlineQueryCurrentChat("Inline Mode"); - return await bot.SendTextMessageAsync(msg.Chat, "Press the button to start Inline Query\n\n" + + return await bot.SendMessage(msg.Chat, "Press the button to start Inline Query\n\n" + "(Make sure you enabled Inline Mode in @BotFather)", replyMarkup: new InlineKeyboardMarkup(button)); } async Task SendPoll(Message msg) { - return await bot.SendPollAsync(msg.Chat, "Question", PollOptions, isAnonymous: false); + return await bot.SendPoll(msg.Chat, "Question", PollOptions, isAnonymous: false); } async Task SendAnonymousPoll(Message msg) { - return await bot.SendPollAsync(chatId: msg.Chat, "Question", PollOptions); + return await bot.SendPoll(chatId: msg.Chat, "Question", PollOptions); } static Task FailingHandler(Message msg) @@ -145,8 +145,8 @@ static Task FailingHandler(Message msg) private async Task OnCallbackQuery(CallbackQuery callbackQuery) { logger.LogInformation("Received inline keyboard callback from: {CallbackQueryId}", callbackQuery.Id); - await bot.AnswerCallbackQueryAsync(callbackQuery.Id, $"Received {callbackQuery.Data}"); - await bot.SendTextMessageAsync(callbackQuery.Message!.Chat, $"Received {callbackQuery.Data}"); + await bot.AnswerCallbackQuery(callbackQuery.Id, $"Received {callbackQuery.Data}"); + await bot.SendMessage(callbackQuery.Message!.Chat, $"Received {callbackQuery.Data}"); } #region Inline Mode @@ -159,13 +159,13 @@ private async Task OnInlineQuery(InlineQuery inlineQuery) new InlineQueryResultArticle("1", "Telegram.Bot", new InputTextMessageContent("hello")), new InlineQueryResultArticle("2", "is the best", new InputTextMessageContent("world")) ]; - await bot.AnswerInlineQueryAsync(inlineQuery.Id, results, cacheTime: 0, isPersonal: true); + await bot.AnswerInlineQuery(inlineQuery.Id, results, cacheTime: 0, isPersonal: true); } private async Task OnChosenInlineResult(ChosenInlineResult chosenInlineResult) { logger.LogInformation("Received inline result: {ChosenInlineResultId}", chosenInlineResult.ResultId); - await bot.SendTextMessageAsync(chosenInlineResult.From.Id, $"You chose result with Id: {chosenInlineResult.ResultId}"); + await bot.SendMessage(chosenInlineResult.From.Id, $"You chose result with Id: {chosenInlineResult.ResultId}"); } #endregion @@ -181,7 +181,7 @@ private async Task OnPollAnswer(PollAnswer pollAnswer) var answer = pollAnswer.OptionIds.FirstOrDefault(); var selectedOption = PollOptions[answer]; if (pollAnswer.User != null) - await bot.SendTextMessageAsync(pollAnswer.User.Id, $"You've chosen: {selectedOption.Text} in poll"); + await bot.SendMessage(pollAnswer.User.Id, $"You've chosen: {selectedOption.Text} in poll"); } private Task UnknownUpdateHandlerAsync(Update update) diff --git a/Webhook.Controllers/Webhook.Controllers.csproj b/Webhook.Controllers/Webhook.Controllers.csproj index 70d05b0c..f9c1e100 100644 --- a/Webhook.Controllers/Webhook.Controllers.csproj +++ b/Webhook.Controllers/Webhook.Controllers.csproj @@ -8,7 +8,7 @@ - + diff --git a/Webhook.MinimalAPIs/Program.cs b/Webhook.MinimalAPIs/Program.cs index ce1553ed..38a04c7b 100644 --- a/Webhook.MinimalAPIs/Program.cs +++ b/Webhook.MinimalAPIs/Program.cs @@ -10,7 +10,7 @@ var app = builder.Build(); app.UseHttpsRedirection(); -app.MapGet("/bot/setWebhook", async (TelegramBotClient bot) => { await bot.SetWebhookAsync(webhookUrl); return $"Webhook set to {webhookUrl}"; }); +app.MapGet("/bot/setWebhook", async (TelegramBotClient bot) => { await bot.SetWebhook(webhookUrl); return $"Webhook set to {webhookUrl}"; }); app.MapPost("/bot", OnUpdate); app.Run(); @@ -21,5 +21,5 @@ async void OnUpdate(TelegramBotClient bot, Update update) var msg = update.Message; Console.WriteLine($"Received message '{msg.Text}' in {msg.Chat}"); // let's echo back received text in the chat - await bot.SendTextMessageAsync(msg.Chat, $"{msg.From} said: {msg.Text}"); + await bot.SendMessage(msg.Chat, $"{msg.From} said: {msg.Text}"); } diff --git a/Webhook.MinimalAPIs/Webhook.MinimalAPIs.csproj b/Webhook.MinimalAPIs/Webhook.MinimalAPIs.csproj index 3972f4a4..e1779d57 100644 --- a/Webhook.MinimalAPIs/Webhook.MinimalAPIs.csproj +++ b/Webhook.MinimalAPIs/Webhook.MinimalAPIs.csproj @@ -8,7 +8,7 @@ - +