Code cleanup

This commit is contained in:
Laura Hausmann 2023-01-08 23:02:18 +01:00
parent b9c43550aa
commit ada850dc05
Signed by: zotan
GPG key ID: D044E84C5BE01605

View file

@ -4,17 +4,18 @@ using Telegram.Bot.Types.Enums;
var token = Environment.GetEnvironmentVariable("TELEGRAM_BOT_TOKEN")!;
var bot = new TelegramBotClient(token);
var me = await bot.GetMeAsync();
using var cts = new CancellationTokenSource();
var bot = new TelegramBotClient(token);
var me = await bot.GetMeAsync();
await bot.SetMyCommandsAsync(new[] {
new BotCommand { Command = "help", Description = "Lists all commands" },
new BotCommand { Command = "coinflip", Description = "Flips a coin" },
new BotCommand { Command = "shouldi", Description = "Tells you if you should or shouldn't do something" },
new BotCommand { Command = "yesno", Description = "Answes binary questions" },
new BotCommand { Command = "makedecision", Description = "Makes a decision. Expects a list of comma separated options." },
new BotCommand { Command = "choose", Description = "Chooses an item. Expects a list of comma separated options." },
new BotCommand { Command = "help", Description = "Lists all commands" },
});
var iBotCommands = new List<string> {
@ -39,10 +40,10 @@ Task PollingErrorHandler(ITelegramBotClient ibot, Exception ex, CancellationToke
async Task HandleUpdateAsync(ITelegramBotClient ibot, Update update, CancellationToken ct) {
try {
await (update.Type switch {
UpdateType.Message => BotOnMessageReceived(ibot, update.Message!),
_ => Task.CompletedTask
});
if (update.Type != UpdateType.Message)
return;
await BotOnMessageReceived(ibot, update.Message!);
}
catch (Exception ex) {
Console.WriteLine($"Exception while handling {update.Type}: {ex}");
@ -116,7 +117,7 @@ async Task BotOnMessageReceived(ITelegramBotClient botClient, Message message) {
await botClient.SendTextMessageAsync(message.Chat.Id, RandomOption(options).Trim(), replyToMessageId: message.MessageId);
break;
case "help":
case "start":
case "start":
await botClient.SendTextMessageAsync(message.Chat.Id, """
Heya, I am a bot that can help you make decisions! Here's a list of things I can do: