From 6a6fc4ffff6f4ecc0ecd4a2ae5eb70369d3868c6 Mon Sep 17 00:00:00 2001 From: Maximilian Engel Date: Thu, 18 Feb 2021 20:31:48 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20"Refresh=20mail"=20Button,=20?= =?UTF-8?q?fix=20"one=20minute=20left"=20timer?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TrashmailClient_TelegramBot/Program.cs | 182 +++++++++++++++---------- 1 file changed, 111 insertions(+), 71 deletions(-) diff --git a/TrashmailClient_TelegramBot/Program.cs b/TrashmailClient_TelegramBot/Program.cs index 264101c..5c1f9ae 100644 --- a/TrashmailClient_TelegramBot/Program.cs +++ b/TrashmailClient_TelegramBot/Program.cs @@ -10,6 +10,7 @@ using System.Threading; using Telegram.Bot; using Telegram.Bot.Args; +using Telegram.Bot.Types; using Telegram.Bot.Types.ReplyMarkups; namespace TrashmailClient_TelegramBot @@ -28,15 +29,16 @@ class Program const string show = "Show"; const string listlinks = "Show only links"; const string generatemail = "Generate new mail"; + const string refresh = "Refresh mail"; #endregion static readonly ReplyKeyboardMarkup replyMarkup = new ReplyKeyboardMarkup( keyboardRow: new[] { new KeyboardButton(generatemail) }, resizeKeyboard: true, - oneTimeKeyboard: true + oneTimeKeyboard: false ); - static readonly InlineKeyboardMarkup replyMarkupOptions = new InlineKeyboardMarkup( + static readonly InlineKeyboardMarkup recieveOptions = new InlineKeyboardMarkup( new InlineKeyboardButton[][] { new [] { InlineKeyboardButton.WithCallbackData(autoconfirm) }, @@ -44,6 +46,12 @@ class Program new [] { InlineKeyboardButton.WithCallbackData(listlinks) } }); + static readonly InlineKeyboardMarkup refreshOptions = new InlineKeyboardMarkup( + new InlineKeyboardButton[][] + { + new [] { InlineKeyboardButton.WithCallbackData(refresh) } + }); + static void Main(string[] args) { string botToken; @@ -57,7 +65,7 @@ static void Main(string[] args) { botToken = args[0]; } - + bot = new TelegramBotClient(botToken); bot.OnMessage += Bot_OnMessage; bot.OnCallbackQuery += Bot_OnCallbackQuery; @@ -102,7 +110,8 @@ static void CheckForMail(ActiveMails activeMail) messageId: activeMail.messageID, text: activeMail.address + Environment.NewLine + suffix ); - } catch { } + } + catch { } try { @@ -173,12 +182,18 @@ static void CheckForMail(ActiveMails activeMail) static void DeleteOldDbEntries() { DatabaseContext db = new DatabaseContext(); - DateTime ago15Minutes = DateTime.Now.AddMinutes(-15); - ActiveMails[] activeMails = db.activemails.Where(a => a.endDate < ago15Minutes).ToArray(); - ReadMails[] readMails = db.readmails.Include("mail").Where(a => a.mail.endDate < ago15Minutes).ToArray(); + ActiveMails[] activeMails = db.activemails.Include("subscriber").Where(a => a.endDate < DateTime.Now).ToArray(); + ReadMails[] readMails = db.readmails.Include("mail").Where(a => a.mail.endDate < DateTime.Now).ToArray(); - foreach(ActiveMails activeMail in activeMails) + foreach (ActiveMails activeMail in activeMails) { + bot.EditMessageTextAsync( + chatId: activeMail.subscriber.chatID, + messageId: activeMail.messageID, + text: activeMail.address + Environment.NewLine + "This mail has run out of time", + replyMarkup: refreshOptions + ); + IMailService mailServer = MailService.Create(activeMail.address); if (mailServer == null) @@ -233,6 +248,10 @@ private static void Bot_OnCallbackQuery(object sender, CallbackQueryEventArgs e) replyMarkup: replyMarkup ); break; + case refresh: + string originalMail = e.CallbackQuery.Message.Text.Split("\n")[0]; + AnswerGenerateMail(db, sub, chatID, custom, originalMail, e.CallbackQuery.Message.MessageId); + break; } db.SaveChanges(); @@ -290,88 +309,109 @@ private static void Bot_OnMessage(object sender, MessageEventArgs e) bot.SendTextMessageAsync( chatId: chatID, text: "What should I do with mails?", - replyMarkup: replyMarkupOptions + replyMarkup: recieveOptions ); break; case generate: case generatemail: case custom: + string customMail = ""; + if (commandParameters != null && commandParameters.Count > 0) + customMail = commandParameters[0]; + AnswerGenerateMail(db, sub, chatID, command, customMail); + break; + } + + db.SaveChanges(); + } - var currentlyActiveMails = db.activemails.Where(a => a.endDate > DateTime.Now && a.subscriber == sub).ToArray(); + static void AnswerGenerateMail(DatabaseContext db, Subscribers sender, long chatID, string command, string customMail, int messageID = 0) + { + var currentlyActiveMails = db.activemails.Where(a => a.endDate > DateTime.Now && a.subscriber == sender).ToArray(); - string generatedMail = ""; - if (command == custom) + string generatedMail = ""; + if (command == custom) + { + if (!string.IsNullOrEmpty(customMail)) + { + generatedMail = customMail; + } + else + { + string botText = "This is not how you use this command." + Environment.NewLine + "Try this:" + Environment.NewLine + Environment.NewLine + "/custom " + Environment.NewLine + Environment.NewLine + "Valid domains are:" + Environment.NewLine; + + string exampleDomain = ""; + foreach (var service in MailService.mailProviders.Keys) { - if (commandParameters.Count > 0) - { - generatedMail = commandParameters[0]; - } - else + foreach (string domain in MailService.mailProviders[service]) { - string botText = "This is not how you use this command." + Environment.NewLine + "Try this:" + Environment.NewLine + Environment.NewLine + "/custom " + Environment.NewLine + Environment.NewLine + "Valid domains are:" + Environment.NewLine; - - string exampleDomain = ""; - foreach(var service in MailService.mailProviders.Keys) - { - foreach(string domain in MailService.mailProviders[service]) - { - if (string.IsNullOrEmpty(exampleDomain)) - exampleDomain = domain; - botText += domain + Environment.NewLine; - } - } - - botText += "For example:" + Environment.NewLine + "abc123@" + exampleDomain; - - - bot.SendTextMessageAsync( - chatId: chatID, - text: botText, - replyMarkup: replyMarkup - ); - break; + if (string.IsNullOrEmpty(exampleDomain)) + exampleDomain = domain; + botText += domain + Environment.NewLine; } } - else - { - generatedMail = MailService.GenerateMail(); - } - if (currentlyActiveMails.Length >= 5) - { - bot.SendTextMessageAsync( - chatId: chatID, - text: "You have generated too many mails. Try again later.", - replyMarkup: replyMarkup - ); - break; - } + botText += "For example:" + Environment.NewLine + "abc123@" + exampleDomain; - if (MailService.Create(generatedMail) == null) - { - bot.SendTextMessageAsync( - chatId: chatID, - text: generatedMail + " is not a valid address!" - ); - break; - } - var result = bot.SendTextMessageAsync( + bot.SendTextMessageAsync( chatId: chatID, - text: generatedMail + Environment.NewLine + "This mail will be valid for another 15 minutes" - ).Result; + text: botText, + replyMarkup: replyMarkup + ); + return; + } + } + else + { + generatedMail = MailService.GenerateMail(); + } - ActiveMails activeMail = new ActiveMails(); - activeMail.address = generatedMail; - activeMail.subscriber = sub; - activeMail.messageID = result.MessageId; - activeMail.endDate = DateTime.Now.AddMinutes(15); + if (currentlyActiveMails.Length >= 5) + { + bot.SendTextMessageAsync( + chatId: chatID, + text: "You have generated too many mails. Try again later.", + replyMarkup: replyMarkup + ); + return; + } - db.activemails.Add(activeMail); - break; + if (MailService.Create(generatedMail) == null) + { + bot.SendTextMessageAsync( + chatId: chatID, + text: generatedMail + " is not a valid address!" + ); + return; } - db.SaveChanges(); + Message result = null; + + if (messageID == 0) + { + result = bot.SendTextMessageAsync( + chatId: chatID, + text: generatedMail + Environment.NewLine + "This mail will be valid for another 15 minutes" + ).Result; + } + else + { + result = bot.EditMessageTextAsync( + chatId: chatID, + messageId: messageID, + text: generatedMail + Environment.NewLine + "This mail will be valid for another 15 minutes" + ).Result; + } + + ActiveMails activeMail = new ActiveMails(); + activeMail.address = generatedMail; + activeMail.subscriber = sender; + activeMail.messageID = result.MessageId; + activeMail.endDate = DateTime.Now.AddMinutes(15); + + db.activemails.Add(activeMail); + return; } } }