Skip to content

Commit

Permalink
✨ Add "Refresh mail" Button, fix "one minute left" timer
Browse files Browse the repository at this point in the history
  • Loading branch information
etaxi341 committed Feb 18, 2021
1 parent dc56b58 commit 6a6fc4f
Showing 1 changed file with 111 additions and 71 deletions.
182 changes: 111 additions & 71 deletions TrashmailClient_TelegramBot/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -28,22 +29,29 @@ 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) },
new [] { InlineKeyboardButton.WithCallbackData(show) },
new [] { InlineKeyboardButton.WithCallbackData(listlinks) }
});

static readonly InlineKeyboardMarkup refreshOptions = new InlineKeyboardMarkup(
new InlineKeyboardButton[][]
{
new [] { InlineKeyboardButton.WithCallbackData(refresh) }
});

static void Main(string[] args)
{
string botToken;
Expand All @@ -57,7 +65,7 @@ static void Main(string[] args)
{
botToken = args[0];
}

bot = new TelegramBotClient(botToken);
bot.OnMessage += Bot_OnMessage;
bot.OnCallbackQuery += Bot_OnCallbackQuery;
Expand Down Expand Up @@ -102,7 +110,8 @@ static void CheckForMail(ActiveMails activeMail)
messageId: activeMail.messageID,
text: activeMail.address + Environment.NewLine + suffix
);
} catch { }
}
catch { }

try
{
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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 <user@domain>" + 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 <user@domain>" + 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;
}
}
}

0 comments on commit 6a6fc4f

Please sign in to comment.