Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add poll handlers #442

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 34 additions & 1 deletion Telegram.Bot.Examples.Polling/Services/UpdateHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class UpdateHandler : IUpdateHandler
{
private readonly ITelegramBotClient _botClient;
private readonly ILogger<UpdateHandler> _logger;
private static readonly string[] PollAnswers = new[] { "Hello", "Bye!" };

public UpdateHandler(ITelegramBotClient botClient, ILogger<UpdateHandler> logger)
{
Expand All @@ -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)
};

Expand All @@ -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)
};
Expand Down Expand Up @@ -186,6 +190,18 @@ static async Task<Message> StartInlineQuery(ITelegramBotClient botClient, Messag
cancellationToken: cancellationToken);
}

static async Task<Message> SendPoll(ITelegramBotClient botClient, Message message, CancellationToken cancellationToken)
{
return await botClient
.SendPollAsync(chatId: message.Chat.Id, "Question", PollAnswers, isAnonymous: false, cancellationToken: cancellationToken);
}

static async Task<Message> 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<Message> FailingHandler(ITelegramBotClient botClient, Message message, CancellationToken cancellationToken)
Expand Down Expand Up @@ -246,6 +262,23 @@ private async Task BotOnChosenInlineResultReceived(ChosenInlineResult chosenInli

#endregion

private Task BotOnPollReceived(Poll poll, CancellationToken cancellationToken)
{
_logger.LogInformation($"Received Pull info: {poll.Question}");
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_logger.LogInformation($"Received Poll 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)
Expand Down