From 83fb04e57f197ba10edaac3c67cb539e81003d8a Mon Sep 17 00:00:00 2001 From: Sergei Filippov Date: Sun, 19 Mar 2023 15:25:25 +0300 Subject: [PATCH] Add poll handlers --- .../Services/UpdateHandler.cs | 35 ++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/Telegram.Bot.Examples.Polling/Services/UpdateHandler.cs b/Telegram.Bot.Examples.Polling/Services/UpdateHandler.cs index 7397eb4..4317bae 100644 --- a/Telegram.Bot.Examples.Polling/Services/UpdateHandler.cs +++ b/Telegram.Bot.Examples.Polling/Services/UpdateHandler.cs @@ -11,6 +11,7 @@ public class UpdateHandler : IUpdateHandler { private readonly ITelegramBotClient _botClient; private readonly ILogger _logger; + private static readonly string[] PollAnswers = new[] { "Hello", "Bye!" }; public UpdateHandler(ITelegramBotClient botClient, ILogger logger) { @@ -27,12 +28,13 @@ public async Task HandleUpdateAsync(ITelegramBotClient _, Update update, Cancell // UpdateType.EditedChannelPost: // UpdateType.ShippingQuery: // UpdateType.PreCheckoutQuery: - // UpdateType.Poll: { Message: { } message } => BotOnMessageReceived(message, cancellationToken), { EditedMessage: { } message } => BotOnMessageReceived(message, cancellationToken), { CallbackQuery: { } callbackQuery } => BotOnCallbackQueryReceived(callbackQuery, cancellationToken), { InlineQuery: { } inlineQuery } => BotOnInlineQueryReceived(inlineQuery, cancellationToken), { ChosenInlineResult: { } chosenInlineResult } => BotOnChosenInlineResultReceived(chosenInlineResult, cancellationToken), + { Poll: { } poll} => BotOnPollReceived(poll, cancellationToken), + { PollAnswer: { } pollAnswer} => BotOnPollAnswerReceived(pollAnswer, cancellationToken), _ => UnknownUpdateHandlerAsync(update, cancellationToken) }; @@ -53,6 +55,8 @@ private async Task BotOnMessageReceived(Message message, CancellationToken cance "/photo" => SendFile(_botClient, message, cancellationToken), "/request" => RequestContactAndLocation(_botClient, message, cancellationToken), "/inline_mode" => StartInlineQuery(_botClient, message, cancellationToken), + "/poll" => SendPoll(_botClient, message, cancellationToken), + "/poll_anonymous" => SendAnonymousPoll(_botClient, message, cancellationToken), "/throw" => FailingHandler(_botClient, message, cancellationToken), _ => Usage(_botClient, message, cancellationToken) }; @@ -186,6 +190,18 @@ static async Task StartInlineQuery(ITelegramBotClient botClient, Messag cancellationToken: cancellationToken); } + static async Task SendPoll(ITelegramBotClient botClient, Message message, CancellationToken cancellationToken) + { + return await botClient + .SendPollAsync(chatId: message.Chat.Id, "Question", PollAnswers, isAnonymous: false, cancellationToken: cancellationToken); + } + + static async Task SendAnonymousPoll(ITelegramBotClient botClient, Message message, CancellationToken cancellationToken) + { + return await botClient + .SendPollAsync(chatId: message.Chat.Id, "Question", PollAnswers, cancellationToken: cancellationToken); + } + #pragma warning disable RCS1163 // Unused parameter. #pragma warning disable IDE0060 // Remove unused parameter static Task FailingHandler(ITelegramBotClient botClient, Message message, CancellationToken cancellationToken) @@ -246,6 +262,23 @@ await _botClient.SendTextMessageAsync( #endregion + private Task BotOnPollReceived(Poll poll, CancellationToken cancellationToken) + { + _logger.LogInformation($"Received Pull info: {poll.Question}"); + return Task.CompletedTask; + } + + private async Task BotOnPollAnswerReceived(PollAnswer pollAnswer, CancellationToken cancellationToken) + { + var answer = pollAnswer.OptionIds.FirstOrDefault(); + var selectedAnswer = PollAnswers[answer]; + + await _botClient.SendTextMessageAsync( + chatId: pollAnswer.User.Id, + text: $"You've chosen: {selectedAnswer} in poll!", + cancellationToken: cancellationToken); + } + #pragma warning disable IDE0060 // Remove unused parameter #pragma warning disable RCS1163 // Unused parameter. private Task UnknownUpdateHandlerAsync(Update update, CancellationToken cancellationToken)