using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using Telegram.Bot; using Telegram.Bot.Args; using Telegram.Bot.Types; using Telegram.Bot.Types.Enums; using Telegram.Bot.Types.InlineQueryResults; using Telegram.Bot.Types.ReplyMarkups; namespace TelegramRemindMe { public static class Program { //TODO: multiuser uwu private static bool isAddingTask = false; private static List tasks = new List(); private static readonly TelegramBotClient Bot = new TelegramBotClient("511835815:AAHewCgiXep58wF6tDWIW133LhfqjCbb8c4"); public static void Main(string[] args) { Bot.OnMessage += BotOnMessageReceived; Bot.OnMessageEdited += BotOnMessageReceived; Bot.OnCallbackQuery += BotOnCallbackQueryReceived; Bot.OnInlineQuery += BotOnInlineQueryReceived; Bot.OnInlineResultChosen += BotOnChosenInlineResultReceived; Bot.OnReceiveError += BotOnReceiveError; var me = Bot.GetMeAsync().Result; Console.Title = me.Username; Bot.StartReceiving(); Console.WriteLine($"Start listening for @{me.Username}"); Console.ReadLine(); Bot.StopReceiving(); } private static async void BotOnMessageReceived(object sender, MessageEventArgs messageEventArgs) { var message = messageEventArgs.Message; if (message == null || message.Type != MessageType.Text) return; //IReplyMarkup keyboard = new ReplyKeyboardRemove(); var actionKeyboard = new InlineKeyboardMarkup(new[] { new [] { InlineKeyboardButton.WithCallbackData("Add a new task"), InlineKeyboardButton.WithCallbackData("List tasks"), }, new [] { InlineKeyboardButton.WithCallbackData("Add a new reminder"), InlineKeyboardButton.WithCallbackData("List reminders"), } }); switch (message.Text.Split(' ').First()) { case "/start": case "/do": case "/actions": case "/help": await Bot.SendChatActionAsync(message.Chat.Id, ChatAction.Typing); //await Task.Delay(500); await Bot.SendTextMessageAsync( message.Chat.Id, "What do you want me to do?", replyMarkup: actionKeyboard); break; case "/echo": await Bot.SendChatActionAsync(message.Chat.Id, ChatAction.RecordVideoNote); await Task.Delay(500); await Bot.SendChatActionAsync(message.Chat.Id, ChatAction.UploadDocument); await Task.Delay(500); await Bot.SendChatActionAsync(message.Chat.Id, ChatAction.RecordAudio); await Task.Delay(500); await Bot.SendChatActionAsync(message.Chat.Id, ChatAction.Typing); await Task.Delay(500); await Bot.SendTextMessageAsync( message.Chat.Id, "MEOW" + message.Text.Substring(5)); break; default: if (isAddingTask) { tasks.Add(message.Text); await Bot.SendTextMessageAsync( message.Chat.Id, "Added. Anything else?", replyMarkup: actionKeyboard); } break; } } private static async void BotOnCallbackQueryReceived(object sender, CallbackQueryEventArgs callbackQueryEventArgs) { Console.WriteLine($"Received action request: {callbackQueryEventArgs.CallbackQuery.Data}"); switch (callbackQueryEventArgs.CallbackQuery.Data) { case "Add a new task": await Bot.EditMessageTextAsync( new ChatId(callbackQueryEventArgs.CallbackQuery.Message.Chat.Id), callbackQueryEventArgs.CallbackQuery.Message.MessageId, "Which task should I add?" ); isAddingTask = true; break; case "List tasks": var sb = new StringBuilder(); sb.AppendLine("Your tasks:"); foreach (var task in tasks) { sb.Append("[ ] "); sb.AppendLine(task); } await Bot.EditMessageTextAsync( new ChatId(callbackQueryEventArgs.CallbackQuery.Message.Chat.Id), callbackQueryEventArgs.CallbackQuery.Message.MessageId, sb.ToString() ); break; default: Console.WriteLine("Wat (Unknown callback action)"); await Bot.AnswerCallbackQueryAsync( callbackQueryEventArgs.CallbackQuery.Id, "Wat"); break; } //await Bot.AnswerCallbackQueryAsync( // callbackQueryEventArgs.CallbackQuery.Id, // $"Received {callbackQueryEventArgs.CallbackQuery.Data}"); } private static async void BotOnInlineQueryReceived(object sender, InlineQueryEventArgs inlineQueryEventArgs) { Console.WriteLine($"Received inline query from: {inlineQueryEventArgs.InlineQuery.From.Id}"); InlineQueryResultBase[] results = { new InlineQueryResultLocation { Id = "1", Latitude = 40.7058316f, // displayed result Longitude = -74.2581888f, Title = "New York", InputMessageContent = new InputLocationMessageContent // message if result is selected { Latitude = 40.7058316f, Longitude = -74.2581888f, } }, new InlineQueryResultLocation { Id = "2", Longitude = 52.507629f, // displayed result Latitude = 13.1449577f, Title = "Berlin", InputMessageContent = new InputLocationMessageContent // message if result is selected { Longitude = 52.507629f, Latitude = 13.1449577f } } }; await Bot.AnswerInlineQueryAsync( inlineQueryEventArgs.InlineQuery.Id, results, isPersonal: true, cacheTime: 0); } private static void BotOnChosenInlineResultReceived(object sender, ChosenInlineResultEventArgs chosenInlineResultEventArgs) { Console.WriteLine($"Received inline result: {chosenInlineResultEventArgs.ChosenInlineResult.ResultId}"); } private static void BotOnReceiveError(object sender, ReceiveErrorEventArgs receiveErrorEventArgs) { Console.WriteLine("Received error: {0} — {1}", receiveErrorEventArgs.ApiRequestException.ErrorCode, receiveErrorEventArgs.ApiRequestException.Message); } } }