diff --git a/Examples/.editorconfig b/Examples/.editorconfig index d3500d0..d91f42c 100644 --- a/Examples/.editorconfig +++ b/Examples/.editorconfig @@ -6,7 +6,7 @@ root = true # All Files [*] charset = utf-8 -end_of_line = lf +#end_of_line = lf indent_style = space indent_size = 4 insert_final_newline = true diff --git a/Examples/1/ExampleBot.cs b/Examples/1/ExampleBot.cs index 95b3495..171d515 100644 --- a/Examples/1/ExampleBot.cs +++ b/Examples/1/ExampleBot.cs @@ -13,12 +13,12 @@ internal class ExampleBot private async Task BookExamples() { // ANCHOR: example-bot -var botClient = new TelegramBotClient("{YOUR_ACCESS_TOKEN_HERE}"); +using CancellationTokenSource cts = new(); -using CancellationTokenSource cts = new (); +var botClient = new TelegramBotClient("{YOUR_ACCESS_TOKEN_HERE}", cancellationToken: cts.Token); // StartReceiving does not block the caller thread. Receiving is done on the ThreadPool. - ReceiverOptions receiverOptions = new () + var receiverOptions = new ReceiverOptions() { AllowedUpdates = Array.Empty() // receive all update types except ChatMember related updates }; @@ -26,8 +26,7 @@ private async Task BookExamples() botClient.StartReceiving( updateHandler: HandleUpdateAsync, pollingErrorHandler: HandlePollingErrorAsync, - receiverOptions: receiverOptions, - cancellationToken: cts.Token + receiverOptions: receiverOptions ); var me = await botClient.GetMeAsync(); @@ -52,10 +51,7 @@ async Task HandleUpdateAsync(ITelegramBotClient botClient, Update update, Cancel Console.WriteLine($"Received a '{messageText}' message in chat {chatId}."); // Echo received message text - Message sentMessage = await botClient.SendTextMessageAsync( - chatId: chatId, - text: "You said:\n" + messageText, - cancellationToken: cancellationToken); + Message sentMessage = await botClient.SendTextMessageAsync(chatId, "You said:\n" + messageText); } Task HandlePollingErrorAsync(ITelegramBotClient botClient, Exception exception, CancellationToken cancellationToken) diff --git a/Examples/2/ReplyMarkup.cs b/Examples/2/ReplyMarkup.cs index 41bf9ca..7b474da 100644 --- a/Examples/2/ReplyMarkup.cs +++ b/Examples/2/ReplyMarkup.cs @@ -10,138 +10,104 @@ namespace BookExamples.Chapter2; internal class ReplyMarkup { - private readonly ITelegramBotClient botClient = new TelegramBotClient("{YOUR_ACCESS_TOKEN_HERE}"); - private readonly ChatId chatId = 12345; - private readonly CancellationToken cancellationToken = new CancellationTokenSource().Token; + public readonly ITelegramBotClient botClient = new TelegramBotClient("{YOUR_ACCESS_TOKEN_HERE}"); + public readonly ChatId chatId = 12345; private async Task SingleRowMarkup() { // ANCHOR: single-row -ReplyKeyboardMarkup replyKeyboardMarkup = new(new[] +var buttons = new KeyboardButton[] { - new KeyboardButton[] { "Help me", "Call me ☎️" }, -}) -{ - ResizeKeyboard = true + "Help me", "Call me ☎️", }; -Message sentMessage = await botClient.SendTextMessageAsync( - chatId: chatId, - text: "Choose a response", - replyMarkup: replyKeyboardMarkup, - cancellationToken: cancellationToken); +var sent = await botClient.SendTextMessageAsync(chatId, "Choose a response", + replyMarkup: new ReplyKeyboardMarkup(buttons) { ResizeKeyboard = true }); // ANCHOR_END: single-row } private async Task MultipleRowMarkup() { // ANCHOR: multiple-row -ReplyKeyboardMarkup replyKeyboardMarkup = new(new[] +var buttons = new KeyboardButton[][] { new KeyboardButton[] { "Help me" }, - new KeyboardButton[] { "Call me ☎️" }, -}) -{ - ResizeKeyboard = true + new KeyboardButton[] { "Call me ☎️", "Write me ✉️" }, }; -Message sentMessage = await botClient.SendTextMessageAsync( - chatId: chatId, - text: "Choose a response", - replyMarkup: replyKeyboardMarkup, - cancellationToken: cancellationToken); +var sent = await botClient.SendTextMessageAsync(chatId, "Choose a response", + replyMarkup: new ReplyKeyboardMarkup(buttons) { ResizeKeyboard = true }); // ANCHOR_END: multiple-row } private async Task RequestInfo() { // ANCHOR: request-info -ReplyKeyboardMarkup replyKeyboardMarkup = new(new[] +var buttons = new[] { KeyboardButton.WithRequestLocation("Share Location"), KeyboardButton.WithRequestContact("Share Contact"), -}); +}; -Message sentMessage = await botClient.SendTextMessageAsync( - chatId: chatId, - text: "Who or Where are you?", - replyMarkup: replyKeyboardMarkup, - cancellationToken: cancellationToken); +var sent = await botClient.SendTextMessageAsync(chatId, "Who or Where are you?", + replyMarkup: new ReplyKeyboardMarkup(buttons)); // ANCHOR_END: request-info } private async Task RemoveKeyboard() { // ANCHOR: remove-keyboard -Message sentMessage = await botClient.SendTextMessageAsync( - chatId: chatId, - text: "Removing keyboard", - replyMarkup: new ReplyKeyboardRemove(), - cancellationToken: cancellationToken); +var sent = await botClient.SendTextMessageAsync(chatId, "Removing keyboard", + replyMarkup: new ReplyKeyboardRemove()); // ANCHOR_END: remove-keyboard } private async Task CallbackButtons() { // ANCHOR: callback-buttons -InlineKeyboardMarkup inlineKeyboard = new(new[] +var buttons = new InlineKeyboardButton[][] { - // first row - new [] + new[] // first row { - InlineKeyboardButton.WithCallbackData(text: "1.1", callbackData: "11"), - InlineKeyboardButton.WithCallbackData(text: "1.2", callbackData: "12"), + InlineKeyboardButton.WithCallbackData("1.1", "11"), + InlineKeyboardButton.WithCallbackData("1.2", "12"), }, - // second row - new [] + new[] // second row { - InlineKeyboardButton.WithCallbackData(text: "2.1", callbackData: "21"), - InlineKeyboardButton.WithCallbackData(text: "2.2", callbackData: "22"), + InlineKeyboardButton.WithCallbackData("2.1", "21"), + InlineKeyboardButton.WithCallbackData("2.2", "22"), }, -}); +}; -Message sentMessage = await botClient.SendTextMessageAsync( - chatId: chatId, - text: "A message with an inline keyboard markup", - replyMarkup: inlineKeyboard, - cancellationToken: cancellationToken); +var sent = await botClient.SendTextMessageAsync(chatId, "A message with an inline keyboard markup", + replyMarkup: new InlineKeyboardMarkup(buttons)); // ANCHOR_END: callback-buttons } private async Task UrlButtons() { // ANCHOR: url-buttons -InlineKeyboardMarkup inlineKeyboard = new(new[] +var buttons = new[] { - InlineKeyboardButton.WithUrl( - text: "Link to the Repository", - url: "https://github.com/TelegramBots/Telegram.Bot") -}); - -Message sentMessage = await botClient.SendTextMessageAsync( - chatId: chatId, - text: "A message with an inline keyboard markup", - replyMarkup: inlineKeyboard, - cancellationToken: cancellationToken); + InlineKeyboardButton.WithUrl("Repositoy Link", "https://github.com/TelegramBots/Telegram.Bot") +}; + +var sent = await botClient.SendTextMessageAsync(chatId, "A message with an inline keyboard markup", + replyMarkup: new InlineKeyboardMarkup(buttons)); // ANCHOR_END: url-buttons } private async Task SwitchToInline() { // ANCHOR: switch-to-inline -InlineKeyboardMarkup inlineKeyboard = new(new[] +var buttons = new[] { - InlineKeyboardButton.WithSwitchInlineQuery( - text: "switch_inline_query"), - InlineKeyboardButton.WithSwitchInlineQueryCurrentChat( - text: "switch_inline_query_current_chat"), -}); - -Message sentMessage = await botClient.SendTextMessageAsync( - chatId: chatId, - text: "A message with an inline keyboard markup", - replyMarkup: inlineKeyboard, - cancellationToken: cancellationToken); + InlineKeyboardButton.WithSwitchInlineQuery("switch_inline_query"), + InlineKeyboardButton.WithSwitchInlineQueryCurrentChat("switch_inline_query_current_chat"), +}; + +var sent = await botClient.SendTextMessageAsync(chatId, "A message with an inline keyboard markup", + replyMarkup: new InlineKeyboardMarkup(buttons)); // ANCHOR_END: switch-to-inline } } diff --git a/Examples/2/SendMessage.cs b/Examples/2/SendMessage.cs index c66a8c8..0d3bf51 100644 --- a/Examples/2/SendMessage.cs +++ b/Examples/2/SendMessage.cs @@ -7,69 +7,48 @@ namespace Examples.Chapter2; internal class SendMessage { - private readonly ITelegramBotClient botClient = new TelegramBotClient("{YOUR_ACCESS_TOKEN_HERE}"); - private readonly CancellationToken cancellationToken = new CancellationTokenSource().Token; - private readonly ChatId chatId = 12345; - private readonly Update update = new (); + public readonly ITelegramBotClient botClient = new TelegramBotClient("{YOUR_ACCESS_TOKEN_HERE}"); + public readonly ChatId chatId = 12345; + public readonly Update update = new (); private async Task SendTextMessage() { // ANCHOR: text-message -Message message = await botClient.SendTextMessageAsync( - chatId: chatId, - text: "Hello, World!", - cancellationToken: cancellationToken); +var message = await botClient.SendTextMessageAsync(chatId, "Hello, World!"); // ANCHOR_END: text-message } private async Task SendStickerMessage() { // ANCHOR: sticker-message -Message message = await botClient.SendStickerAsync( - chatId: chatId, - sticker: InputFile.FromUri("https://github.com/TelegramBots/book/raw/master/src/docs/sticker-dali.webp"), - cancellationToken: cancellationToken); +var message = await botClient.SendStickerAsync(chatId, "https://telegrambots.github.io/book/docs/sticker-dali.webp"); // ANCHOR_END: sticker-message } private async Task SendVideoMessage() { // ANCHOR: video-message -Message message = await botClient.SendVideoAsync( - chatId: chatId, - video: InputFile.FromUri("https://github.com/TelegramBots/book/raw/master/src/docs/video-bulb.mp4"), - cancellationToken: cancellationToken); +var message = await botClient.SendVideoAsync(chatId, "https://telegrambots.github.io/book/docs/video-hawk.mp4"); // ANCHOR_END: video-message } private async Task SendMediaGroup() { // ANCHOR: send-media-group -Message[] messages = await botClient.SendMediaGroupAsync( - chatId: chatId, - media: new IAlbumInputMedia[] +var messages = await botClient.SendMediaGroupAsync(chatId, new IAlbumInputMedia[] { - new InputMediaPhoto( - InputFile.FromUri("https://cdn.pixabay.com/photo/2017/06/20/19/22/fuchs-2424369_640.jpg")), - new InputMediaPhoto( - InputFile.FromUri("https://cdn.pixabay.com/photo/2017/04/11/21/34/giraffe-2222908_640.jpg")), - }, - cancellationToken: cancellationToken); + new InputMediaPhoto("https://cdn.pixabay.com/photo/2017/06/20/19/22/fuchs-2424369_640.jpg"), + new InputMediaPhoto("https://cdn.pixabay.com/photo/2017/04/11/21/34/giraffe-2222908_640.jpg"), + }); // ANCHOR_END: send-media-group } private async Task SendAudio() { // ANCHOR: send-audio -Message message = await botClient.SendAudioAsync( - chatId: chatId, - audio: InputFile.FromUri("https://github.com/TelegramBots/book/raw/master/src/docs/audio-guitar.mp3"), - /* - performer: "Joel Thomas Hunger", - title: "Fun Guitar and Ukulele", - duration: 91, // in seconds - */ - cancellationToken: cancellationToken); +var message = await botClient.SendAudioAsync(chatId, "https://telegrambots.github.io/book/docs/audio-guitar.mp3" + // , performer: "Joel Thomas Hunger", title: "Fun Guitar and Ukulele", duration: 91 // optional + ); // ANCHOR_END: send-audio } @@ -77,137 +56,94 @@ private async Task SendVoice() { // ANCHOR: send-voice await using Stream stream = System.IO.File.OpenRead("/path/to/voice-nfl_commentary.ogg"); -Message message = await botClient.SendVoiceAsync( - chatId: chatId, - voice: InputFile.FromStream(stream), - duration: 36, - cancellationToken: cancellationToken); +var message = await botClient.SendVoiceAsync(chatId, stream, duration: 36); // ANCHOR_END: send-voice } private async Task SendDocument() { // ANCHOR: send-document -Message message = await botClient.SendDocumentAsync( - chatId: chatId, - document: InputFile.FromUri("https://github.com/TelegramBots/book/raw/master/src/docs/photo-ara.jpg"), - caption: "Ara bird. Source: Pixabay", - parseMode: ParseMode.Html, - cancellationToken: cancellationToken); +var message = await botClient.SendDocumentAsync(chatId, "https://telegrambots.github.io/book/docs/photo-ara.jpg", + caption: "Ara bird. Source: Pixabay", parseMode: ParseMode.Html); // ANCHOR_END: send-document } private async Task SendAnimation() { // ANCHOR: send-animation -Message message = await botClient.SendAnimationAsync( - chatId: chatId, - animation: InputFile.FromUri("https://raw.githubusercontent.com/TelegramBots/book/master/src/docs/video-waves.mp4"), - caption: "Waves", - cancellationToken: cancellationToken); +var message = await botClient.SendAnimationAsync(chatId, "https://telegrambots.github.io/book/docs/video-waves.mp4", + caption: "Waves"); // ANCHOR_END: send-animation } private async Task SendPoll() { // ANCHOR: send-poll -Message pollMessage = await botClient.SendPollAsync( - chatId: "@channel_name", - question: "Did you ever hear the tragedy of Darth Plagueis The Wise?", - options: new[] + +var pollMessage = await botClient.SendPollAsync("@channel_name", + "Did you ever hear the tragedy of Darth Plagueis The Wise?", + new InputPollOption[] { "Yes for the hundredth time!", "No, who`s that?" - }, - cancellationToken: cancellationToken); + }); // ANCHOR_END: send-poll // ANCHOR: stop-poll -Poll poll = await botClient.StopPollAsync( - chatId: pollMessage.Chat.Id, - messageId: pollMessage.MessageId, - cancellationToken: cancellationToken); +Poll poll = await botClient.StopPollAsync(pollMessage.Chat.Id, pollMessage.MessageId); // ANCHOR_END: stop-poll } private async Task SendContact() { // ANCHOR: send-contact -Message message = await botClient.SendContactAsync( - chatId: chatId, - phoneNumber: "+1234567890", - firstName: "Han", - lastName: "Solo", - cancellationToken: cancellationToken); +var message = await botClient.SendContactAsync(chatId, phoneNumber: "+1234567890", firstName: "Han", lastName: "Solo"); // ANCHOR_END: send-contact } private async Task SendvCard() { // ANCHOR: send-vCard -Message message = await botClient.SendContactAsync( - chatId: chatId, - phoneNumber: "+1234567890", - firstName: "Han", - vCard: "BEGIN:VCARD\n" + +var message = await botClient.SendContactAsync(chatId, phoneNumber: "+1234567890", firstName: "Han", + vcard: "BEGIN:VCARD\n" + "VERSION:3.0\n" + "N:Solo;Han\n" + "ORG:Scruffy-looking nerf herder\n" + "TEL;TYPE=voice,work,pref:+1234567890\n" + "EMAIL:hansolo@mfalcon.com\n" + - "END:VCARD", - cancellationToken: cancellationToken); + "END:VCARD"); // ANCHOR_END: send-vCard } private async Task SendVenue() { // ANCHOR: send-venue -Message message = await botClient.SendVenueAsync( - chatId: chatId, - latitude: 50.0840172f, - longitude: 14.418288f, - title: "Man Hanging out", - address: "Husova, 110 00 Staré Město, Czechia", - cancellationToken: cancellationToken); +var message = await botClient.SendVenueAsync(chatId, latitude: 50.0840172f, longitude: 14.418288f, + title: "Man Hanging out", address: "Husova, 110 00 Staré Město, Czechia"); // ANCHOR_END: send-venue } private async Task SendLocation() { // ANCHOR: send-location -Message message = await botClient.SendLocationAsync( - chatId: chatId, - latitude: 33.747252f, - longitude: -112.633853f, - cancellationToken: cancellationToken); +var message = await botClient.SendLocationAsync(chatId, latitude: 33.747252f, longitude: -112.633853f); // ANCHOR_END: send-location } private async Task SendPhoto() { // ANCHOR: send-photo -Message message = await botClient.SendPhotoAsync( - chatId: chatId, - photo: InputFile.FromUri("https://github.com/TelegramBots/book/raw/master/src/docs/photo-ara.jpg"), - caption: "Ara bird. Source: Pixabay", - parseMode: ParseMode.Html, - cancellationToken: cancellationToken); +var message = await botClient.SendPhotoAsync(chatId, "https://telegrambots.github.io/book/docs/photo-ara.jpg", + caption: "Ara bird. Source: Pixabay", parseMode: ParseMode.Html); // ANCHOR_END: send-photo } private async Task SendSticker() { // ANCHOR: send-sticker -Message message1 = await botClient.SendStickerAsync( - chatId: chatId, - sticker: InputFile.FromUri("https://github.com/TelegramBots/book/raw/master/src/docs/sticker-fred.webp"), - cancellationToken: cancellationToken); +var message1 = await botClient.SendStickerAsync(chatId, "https://telegrambots.github.io/book/docs/sticker-fred.webp"); -Message message2 = await botClient.SendStickerAsync( - chatId: chatId, - sticker: InputFile.FromFileId(message1.Sticker!.FileId), - cancellationToken: cancellationToken); +var message2 = await botClient.SendStickerAsync(chatId, message1.Sticker!.FileId); // ANCHOR_END: send-sticker } @@ -217,17 +153,12 @@ private async Task SendText() return; // ANCHOR: send-text -Message message = await botClient.SendTextMessageAsync( - chatId: chatId, - text: "Trying *all the parameters* of `sendMessage` method", +var message = await botClient.SendTextMessageAsync(chatId, "Trying more parameters for `sendMessage` method", parseMode: ParseMode.MarkdownV2, - disableNotification: true, - replyToMessageId: update.Message.MessageId, + protectContent: true, + replyParameters: update.Message.MessageId, replyMarkup: new InlineKeyboardMarkup( - InlineKeyboardButton.WithUrl( - text: "Check sendMessage method", - url: "https://core.telegram.org/bots/api#sendmessage")), - cancellationToken: cancellationToken); + InlineKeyboardButton.WithUrl("Check sendMessage method", "https://core.telegram.org/bots/api#sendmessage"))); // ANCHOR_END: send-text if (message is not {From: { }, @@ -250,12 +181,8 @@ private async Task SendText() private async Task SendVideo() { // ANCHOR: send-video -Message message = await botClient.SendVideoAsync( - chatId: chatId, - video: InputFile.FromUri("https://raw.githubusercontent.com/TelegramBots/book/master/src/docs/video-countdown.mp4"), - thumbnail: InputFile.FromUri("https://raw.githubusercontent.com/TelegramBots/book/master/src/2/docs/thumb-clock.jpg"), - supportsStreaming: true, - cancellationToken: cancellationToken); +var message = await botClient.SendVideoAsync(chatId, "https://telegrambots.github.io/book/docs/video-countdown.mp4", + thumbnail: "https://telegrambots.github.io/book/2/docs/thumb-clock.jpg", supportsStreaming: true); // ANCHOR_END: send-video } @@ -264,12 +191,8 @@ private async Task SendVideoNote() // ANCHOR: send-video-note await using Stream stream = System.IO.File.OpenRead("/path/to/video-waves.mp4"); -Message message = await botClient.SendVideoNoteAsync( - chatId: chatId, - videoNote: InputFile.FromStream(stream), - duration: 47, - length: 360, // value of width/height - cancellationToken: cancellationToken); +var message = await botClient.SendVideoNoteAsync(chatId, stream, + duration: 47, length: 360); // value of width/height // ANCHOR_END: send-video-note } } diff --git a/Examples/3/Files.cs b/Examples/3/Files.cs index 26999ae..b62af29 100644 --- a/Examples/3/Files.cs +++ b/Examples/3/Files.cs @@ -5,10 +5,9 @@ namespace Examples.Chapter3; internal class Files { - private readonly ITelegramBotClient botClient = new TelegramBotClient("{YOUR_ACCESS_TOKEN_HERE}"); - private readonly CancellationToken cancellationToken = new CancellationTokenSource().Token; - private readonly ChatId chatId = 12345; - private readonly Update update = new(); + public readonly ITelegramBotClient botClient = new TelegramBotClient("{YOUR_ACCESS_TOKEN_HERE}"); + public readonly ChatId chatId = 12345; + public readonly Update update = new(); private async Task GetFile() { @@ -35,10 +34,7 @@ async Task DownloadFile() const string destinationFilePath = "../downloaded.file"; await using Stream fileStream = System.IO.File.Create(destinationFilePath); -await botClient.DownloadFileAsync( - filePath: filePath, - destination: fileStream, - cancellationToken: cancellationToken); +await botClient.DownloadFileAsync(filePath, fileStream); // ANCHOR_END: download-file } @@ -48,10 +44,7 @@ async Task GetInfoAndDownloadFile() const string destinationFilePath = "../downloaded.file"; await using Stream fileStream = System.IO.File.Create(destinationFilePath); -var file = await botClient.GetInfoAndDownloadFileAsync( - fileId: fileId, - destination: fileStream, - cancellationToken: cancellationToken); +var file = await botClient.GetInfoAndDownloadFileAsync(fileId, fileStream); // ANCHOR_END: get-and-download-file } } @@ -60,9 +53,7 @@ private async Task UploadLocalFile() { // ANCHOR: upload-local-file await using Stream stream = System.IO.File.OpenRead("../hamlet.pdf"); -Message message = await botClient.SendDocumentAsync( - chatId: chatId, - document: InputFile.FromStream(stream: stream, fileName: "hamlet.pdf"), +var message = await botClient.SendDocumentAsync(chatId, document: InputFile.FromStream(stream, "hamlet.pdf"), caption: "The Tragedy of Hamlet,\nPrince of Denmark"); // ANCHOR_END: upload-local-file } @@ -76,18 +67,14 @@ private async Task UploadByFileId() // ANCHOR: upload-by-file_id var fileId = update.Message.Photo.Last().FileId; -Message message = await botClient.SendPhotoAsync( - chatId: chatId, - photo: InputFile.FromFileId(fileId)); +var message = await botClient.SendPhotoAsync(chatId, fileId); // ANCHOR_END: upload-by-file_id } private async Task UploadByURL() { // ANCHOR: upload-by-url -Message message = await botClient.SendPhotoAsync( - chatId: chatId, - photo: InputFile.FromUri("https://cdn.pixabay.com/photo/2017/04/11/21/34/giraffe-2222908_640.jpg")); +var message = await botClient.SendPhotoAsync(chatId, "https://cdn.pixabay.com/photo/2017/04/11/21/34/giraffe-2222908_640.jpg"); // ANCHOR_END: upload-by-url } } diff --git a/Examples/3/Inline.cs b/Examples/3/Inline.cs index 9878b39..16f1a20 100644 --- a/Examples/3/Inline.cs +++ b/Examples/3/Inline.cs @@ -10,7 +10,7 @@ namespace BookExamples.Chapter3; internal class Inline { - private readonly ITelegramBotClient botClient = new TelegramBotClient("{YOUR_ACCESS_TOKEN_HERE}"); + public readonly ITelegramBotClient botClient = new TelegramBotClient("{YOUR_ACCESS_TOKEN_HERE}"); // ANCHOR: arrays private readonly string[] sites = { "Google", "Github", "Telegram", "Wikipedia" }; private readonly string[] siteDescriptions = @@ -45,11 +45,15 @@ async Task HandleUpdateAsync(ITelegramBotClient bot, Update update, Cancellation try { // ANCHOR: switch-statement -await (update.Type switch +switch (update.Type) { - UpdateType.InlineQuery => BotOnInlineQueryReceived(bot, update.InlineQuery!), - UpdateType.ChosenInlineResult => BotOnChosenInlineResultReceived(bot, update.ChosenInlineResult!), -}); + case UpdateType.InlineQuery: + await OnInlineQueryReceived(bot, update.InlineQuery!); + break; + case UpdateType.ChosenInlineResult: + await OnChosenInlineResultReceived(bot, update.ChosenInlineResult!); + break; +}; // ANCHOR_END: switch-statement } catch (Exception ex) @@ -59,7 +63,7 @@ async Task HandleUpdateAsync(ITelegramBotClient bot, Update update, Cancellation } // ANCHOR: on-inline-query-received -async Task BotOnInlineQueryReceived(ITelegramBotClient bot, InlineQuery inlineQuery) +async Task OnInlineQueryReceived(ITelegramBotClient bot, InlineQuery inlineQuery) { var results = new List(); @@ -79,7 +83,7 @@ async Task BotOnInlineQueryReceived(ITelegramBotClient bot, InlineQuery inlineQu // ANCHOR_END: on-inline-query-received // ANCHOR: on-chosen-inline-result-received -Task BotOnChosenInlineResultReceived(ITelegramBotClient bot, ChosenInlineResult chosenInlineResult) +Task OnChosenInlineResultReceived(ITelegramBotClient bot, ChosenInlineResult chosenInlineResult) { if (uint.TryParse(chosenInlineResult.ResultId, out var resultId) // check if a result id is parsable and introduce variable && resultId < sites.Length) diff --git a/Examples/Examples.csproj b/Examples/Examples.csproj index 0b5f726..fad05c3 100644 --- a/Examples/Examples.csproj +++ b/Examples/Examples.csproj @@ -7,7 +7,7 @@ - + diff --git a/Examples/nuget.config b/Examples/nuget.config new file mode 100644 index 0000000..f71e80c --- /dev/null +++ b/Examples/nuget.config @@ -0,0 +1,7 @@ + + + + + + + diff --git a/book.toml b/book.toml index 0716219..0b5c7d0 100644 --- a/book.toml +++ b/book.toml @@ -7,4 +7,5 @@ site-url = "https://telegrambots.github.io/book/" language = "en" [output.html] -git-repository-url = "https://github.com/TelegramBots/book" \ No newline at end of file +git-repository-url = "https://github.com/TelegramBots/book" +additional-css = ["theme/custom.css"] diff --git a/src/2/reply-markup.md b/src/2/reply-markup.md index 1636f3d..9e8c4d6 100644 --- a/src/2/reply-markup.md +++ b/src/2/reply-markup.md @@ -34,6 +34,8 @@ A [`ReplyKeyboardMarkup`] with two rows of buttons: {{#include ../../Examples/2/ReplyMarkup.cs:multiple-row}} ``` +You can use `new List>` instead of `KeyboardButton[][]` if you prefer to build the list dynamically. + ### Request information [`ReplyKeyboardMarkup`] containing buttons for contact and location requests using helper methods `KeyboardButton.WithRequestLocation` and `KeyboardButton.WithRequestContact`: @@ -70,6 +72,8 @@ When a user presses a [callback button], no messages are sent to the chat. Inste {{#include ../../Examples/2/ReplyMarkup.cs:callback-buttons}} ``` +You can use `new List>` instead of `InlineKeyboardButton[][]` if you prefer to build the list dynamically. + ### URL buttons Buttons of this type have a small arrow icon to help the user understand that tapping on a [URL button] will open an external link. In this example we use `InlineKeyboardButton.WithUrl` helper method to create a button with a text and url. diff --git a/src/2/send-msg/audio-voice-msg.md b/src/2/send-msg/audio-voice-msg.md index a324070..2db6e8a 100644 --- a/src/2/send-msg/audio-voice-msg.md +++ b/src/2/send-msg/audio-voice-msg.md @@ -56,4 +56,4 @@ A value is passed for `duration` because Telegram can't figure that out from a f A voice message is returned from the method. Inspect the `message.Voice` property to learn more. -[NFL Commentary voice file]: https://raw.githubusercontent.com/TelegramBots/book/master/src/docs/voice-nfl_commentary.ogg +[NFL Commentary voice file]: https://telegrambots.github.io/book/docs/voice-nfl_commentary.ogg diff --git a/src/2/send-msg/text-msg.md b/src/2/send-msg/text-msg.md index e1b7719..7639385 100644 --- a/src/2/send-msg/text-msg.md +++ b/src/2/send-msg/text-msg.md @@ -25,10 +25,10 @@ text message and returns the message object sent. `text` is written in [MarkDown format] and `parseMode` indicates that. You can also write in HTML or plain text. -By passing `disableNotification` we tell Telegram client on user's device not to show/sound a notification. +By passing `protectContent` we prevent the message (and eventual media) to be copiable/forwardable elsewhere. It's a good idea to make it clear to a user the reason why the bot is sending this message and that's why we pass the user's -message id for `replyToMessageId`. +message id for `replyParameters`. You have the option of specifying a `replyMarkup` when sending messages. Reply markups are explained in details later in this book. diff --git a/src/2/send-msg/video-video_note-msg.md b/src/2/send-msg/video-video_note-msg.md index a2dc355..673c727 100644 --- a/src/2/send-msg/video-video_note-msg.md +++ b/src/2/send-msg/video-video_note-msg.md @@ -45,4 +45,4 @@ Download the [Sea Waves video] to your disk for this example. ![vide note screenshot](../docs/shot-video_note.jpg) -[Sea Waves video]: https://raw.githubusercontent.com/TelegramBots/book/master/src/docs/video-waves.mp4 +[Sea Waves video]: https://telegrambots.github.io/book/docs/video-waves.mp4 diff --git a/src/Migration-Guide-to-Version-21.x.md b/src/Migration-Guide-to-Version-21.x.md index 67c494e..6119b4b 100644 --- a/src/Migration-Guide-to-Version-21.x.md +++ b/src/Migration-Guide-to-Version-21.x.md @@ -2,7 +2,7 @@ Important notes: - Don't bother about version 20, migrate directly to version 21.* -- You won't find this version on Nuget: [See this guide to install it in your programs](https://telegrambots.github.io/book/index.html#-obtain-latest-versions). +- You won't find this version on Nuget: [See this guide to install it in your programs](https://telegrambots.github.io/book/index.html#-installation). - Version 21.1 supports [Bot API 7.5](https://core.telegram.org/bots/api-changelog#june-18-2024) _(including [Telegram Stars payments](#payments-with-telegram-stars))_ - Library is now based on System.Text.Json and doesn't depend on NewtonsoftJson anymore. _([See below](#webhooks-with-systemtextjson))_ @@ -86,3 +86,22 @@ To make it work in your ASP.NET projects, you'll need to: `services.ConfigureTelegramBot(opt => opt.JsonSerializerOptions);` - or if you use minimal APIs, use this line instead: `builder.Services.ConfigureTelegramBot(opt => opt.SerializerOptions);` + +## Global cancellation token + +You can now specify a global `CancellationToken` directly in TelegramBotClient constructor. + +This way, you won't need to pass a cancellationToken to every method call after that +(if you just need one single cancellation token for stopping your bot) + +## InputPollOption in SendPollAsync + +SendPollAsync now expect an array of InputPollOption instead of string. + +But we added an implicit conversion from string to InputPollOption, so the change is minimal: +```csharp +// before: +await botClient.SendPollAsync(chatId, "question", new[] { "answer1", "answer2" }); +// after: +await botClient.SendPollAsync(chatId, "question", new InputPollOption[] { "answer1", "answer2" }); +``` \ No newline at end of file diff --git a/src/README.md b/src/README.md index 389c4aa..435dfd1 100644 --- a/src/README.md +++ b/src/README.md @@ -22,7 +22,7 @@ chatbot in .NET. There are also many concrete examples written in C#. The guides here can even be useful to bot developers using other languages/platforms as it shows best practices in developing Telegram chatbots with examples. -## ⚠️ Obtain latest versions +## 🧩 Installation Latest versions of the library are not yet available on Nuget․org due to false-positive malware detection. We are working with Nuget/ESET teams to resolve this issue. In the mean time, it's available on our [special nuget feed](https://nuget.voids.site/packages/Telegram.Bot): `https://nuget.voids.site/v3/index.json` diff --git a/theme/custom.css b/theme/custom.css new file mode 100644 index 0000000..3f1d4cd --- /dev/null +++ b/theme/custom.css @@ -0,0 +1,3 @@ +:root { + --content-max-width: 900px; +}