From 4b4e33865e35234778ada5813b103a8aae17257b Mon Sep 17 00:00:00 2001 From: zotan Date: Wed, 7 Feb 2018 16:28:49 +0100 Subject: [PATCH] initial commit --- Program.cs | 200 ++++++++++++++++++++++++++++++++++++++++ TelegramRemindMe.csproj | 9 ++ TelegramRemindMe.sln | 16 ++++ 3 files changed, 225 insertions(+) create mode 100644 Program.cs create mode 100644 TelegramRemindMe.csproj create mode 100644 TelegramRemindMe.sln diff --git a/Program.cs b/Program.cs new file mode 100644 index 0000000..428b829 --- /dev/null +++ b/Program.cs @@ -0,0 +1,200 @@ +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); + } + } +} diff --git a/TelegramRemindMe.csproj b/TelegramRemindMe.csproj new file mode 100644 index 0000000..3a4a181 --- /dev/null +++ b/TelegramRemindMe.csproj @@ -0,0 +1,9 @@ + + + Exe + netcoreapp2.0 + + + + + \ No newline at end of file diff --git a/TelegramRemindMe.sln b/TelegramRemindMe.sln new file mode 100644 index 0000000..77b2487 --- /dev/null +++ b/TelegramRemindMe.sln @@ -0,0 +1,16 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TelegramRemindMe", "TelegramRemindMe.csproj", "{58CB8963-3F46-4C77-A188-6E26FA4DA9FE}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {58CB8963-3F46-4C77-A188-6E26FA4DA9FE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {58CB8963-3F46-4C77-A188-6E26FA4DA9FE}.Debug|Any CPU.Build.0 = Debug|Any CPU + {58CB8963-3F46-4C77-A188-6E26FA4DA9FE}.Release|Any CPU.ActiveCfg = Release|Any CPU + {58CB8963-3F46-4C77-A188-6E26FA4DA9FE}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal