Skip to content

Instantly share code, notes, and snippets.

@Muaath5
Last active June 6, 2021 17:04
Show Gist options
  • Save Muaath5/22d39734ac0bc5b9f9ad6b66a97e064c to your computer and use it in GitHub Desktop.
Save Muaath5/22d39734ac0bc5b9f9ad6b66a97e064c to your computer and use it in GitHub Desktop.
Simple Telegram polls bot using long-polling (wiki) in C#
using System;
using System.Threading.Tasks;
using Telegram.Bot;
using Telegram.Bot.Args;
using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;
namespace ConsoleTgBot
{
static class Program
{
static TelegramBotClient Bot = new TelegramBotClient("<bot_token>");
static ChatId PollChatID = null;
static int PollMessageID = 0;
static bool PollStarted = false;
public const string PollQuestion = "Which Telegram client[s] do you use?";
public static readonly string[] PollAnswers =
{
"Telegram desktop",
"Unigram",
"Telegram Android",
"Telegram X",
"Telegram iOS",
"Telegram macOS",
"Telegram web",
"Telegram Web K (beta)",
"Telegram Web Z (beta)",
"Other client"
};
public const bool IsAnonymous = false;
public const PollType BotPollType = PollType.Regular;
public const bool IsMultiple = true;
public static readonly DateTime? CloseDate = null;
public const int QuizTime = 60; // 1m
public const int CorrectOptionId = 0; // Same as null
public const string Explaination = "Unknown";
public static Poll LastPollData = null;
static async Task Main(string[] args)
{
Bot.OnUpdate += Bot_OnUpdate;
Bot.StartReceiving();
Console.WriteLine("Bot start receiving..");
// Test bot
User me = await Bot.GetMeAsync();
Console.WriteLine($"@{me.Username} start working..");
ReadCommand:
Console.Write("Enter a command: ");
ProcessCommand(Console.ReadLine());
goto ReadCommand;
EndBot:
Bot.StopReceiving();
Console.WriteLine("Bot end working");
}
private static async void ProcessCommand(string commandLine)
{
string[] args = commandLine.Split(' ');
string commandName = args[0];
switch (commandName)
{
case "send-poll":
if (PollStarted)
{
Console.Error.WriteLine("Stop current poll to start another poll!");
break;
}
Chat chat = null;
try
{
chat = await Bot.GetChatAsync(args[1]);
}
catch (Exception)
{
Console.Error.WriteLine($"Enter a vaild chat ID!\n {args[1]} isn\'t vaild");
break;
}
Message pollMsg = await Bot.SendPollAsync(chat, PollQuestion, PollAnswers, false, 0, null, default, IsAnonymous, BotPollType, IsMultiple, CorrectOptionId, false, Explaination, ParseMode.Html, QuizTime, CloseDate);
// Set variables
PollMessageID = pollMsg.MessageId;
PollChatID = pollMsg.Chat.Id;
LastPollData = pollMsg.Poll;
PollStarted = true;
break;
case "stop-poll":
if (PollStarted == false)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.Error.WriteLine("Start poll first to stop it!");
Console.ResetColor();
break;
}
Poll result = await Bot.StopPollAsync(PollChatID, PollMessageID);
Console.WriteLine($"Total voters count: {result.TotalVoterCount}");
for (int i = 0; i < result.Options.Length; i++)
{
Console.WriteLine($"{result.Options[i].Text}: {result.Options[i].VoterCount}");
}
PollStarted = false;
break;
}
}
private static void Bot_OnUpdate(object sender, UpdateEventArgs e)
{
if (e.Update.Poll != null)
{
Console.WriteLine();
if (e.Update.Poll.IsClosed)
{
Console.WriteLine("Poll closed");
PollStarted = false;
}
else
{
Console.WriteLine($"New anonymous answer received!");
}
}
else if (e.Update.PollAnswer != null)
{
Console.WriteLine();
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("New answer received!");
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine($"{e.Update.PollAnswer.User.FirstName} ({e.Update.PollAnswer.User.Username}) voted:");
for (int i = 0; i < e.Update.PollAnswer.OptionIds.Length; i++)
{
Console.WriteLine($" {PollAnswers[e.Update.PollAnswer.OptionIds[i]]}");
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment