Skip to content

Commit

Permalink
⬆️ Upgrade Dependencies and fix Telegram Library changes
Browse files Browse the repository at this point in the history
  • Loading branch information
etaxi341 committed Apr 26, 2022
1 parent d9b86de commit 367f777
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 57 deletions.
31 changes: 16 additions & 15 deletions DataManager/DataManager.csproj
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<LangVersion>latest</LangVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.1.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="3.1.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

</Project>
15 changes: 8 additions & 7 deletions Mail_Crawler/Mail_Crawler.csproj
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<LangVersion>latest</LangVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="HtmlAgilityPack" Version="1.11.23" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="HtmlAgilityPack" Version="1.11.42" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
</ItemGroup>

</Project>
101 changes: 78 additions & 23 deletions TrashmailClient_TelegramBot/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
using System.Threading.Tasks;
using Telegram.Bot;
using Telegram.Bot.Args;
using Telegram.Bot.Exceptions;
using Telegram.Bot.Extensions.Polling;
using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;
using Telegram.Bot.Types.ReplyMarkups;

namespace TrashmailClient_TelegramBot
Expand All @@ -34,11 +37,11 @@ class Program
const string refresh = "Refresh mail";
#endregion

static readonly ReplyKeyboardMarkup replyMarkup = new ReplyKeyboardMarkup(
keyboardRow: new[] { new KeyboardButton(generatemail) },
resizeKeyboard: true,
oneTimeKeyboard: false
);
static readonly ReplyKeyboardMarkup replyMarkup = new ReplyKeyboardMarkup(keyboardRow: new[] { new KeyboardButton(generatemail) })
{
ResizeKeyboard = true,
OneTimeKeyboard = false
};

static readonly InlineKeyboardMarkup recieveOptions = new InlineKeyboardMarkup(
new InlineKeyboardButton[][]
Expand Down Expand Up @@ -68,10 +71,14 @@ static async Task Main(string[] args)
botToken = args[0];
}

using CancellationTokenSource cts = new();
ReceiverOptions receiverOptions = new() { AllowedUpdates = { } };

bot = new TelegramBotClient(botToken);
bot.OnMessage += Bot_OnMessage;
bot.OnCallbackQuery += Bot_OnCallbackQuery;
bot.StartReceiving();
bot.StartReceiving(Bot_HandleUpdateAsync,
Bot_HandleErrorAsync,
receiverOptions,
cts.Token);

botUser = await bot.GetMeAsync();

Expand Down Expand Up @@ -171,7 +178,7 @@ static void CheckForMail(ActiveMails activeMail)
Thread.Sleep(1000);
}
}
catch (Telegram.Bot.Exceptions.ApiRequestException e)
catch (ApiRequestException)
{
var readmails = db.readmails.Where(r => r.mail == activeMail).ToList();
db.readmails.RemoveRange(readmails);
Expand Down Expand Up @@ -222,48 +229,94 @@ static void DeleteOldDbEntries()
db.SaveChanges();
}

private static void Bot_OnCallbackQuery(object sender, CallbackQueryEventArgs e)
public static Task Bot_HandleErrorAsync(ITelegramBotClient botClient, Exception exception, CancellationToken cancellationToken)
{
long chatID = e.CallbackQuery.Message.Chat.Id;

var ErrorMessage = exception switch
{
ApiRequestException apiRequestException => $"Telegram API Error:\n[{apiRequestException.ErrorCode}]\n{apiRequestException.Message}",
_ => exception.ToString()
};

Console.WriteLine(ErrorMessage);
return Task.CompletedTask;
}

public static async Task Bot_HandleUpdateAsync(ITelegramBotClient botClient, Update update, CancellationToken cancellationToken)
{
var handler = update.Type switch
{
// UpdateType.Unknown:
// UpdateType.ChannelPost:
// UpdateType.EditedChannelPost:
// UpdateType.ShippingQuery:
// UpdateType.PreCheckoutQuery:
// UpdateType.Poll:
UpdateType.Message => Bot_OnMessage(botClient, update.Message!),
//UpdateType.EditedMessage:
UpdateType.CallbackQuery => Bot_OnCallbackQuery(botClient, update.CallbackQuery!),
//UpdateType.InlineQuery:
//UpdateType.ChosenInlineResult:
_ => UnknownUpdateHandlerAsync(botClient, update)
};

try
{
await handler;
}
catch (Exception exception)
{
await Bot_HandleErrorAsync(botClient, exception, cancellationToken);
}
}

private static Task UnknownUpdateHandlerAsync(ITelegramBotClient botClient, Update update)
{
Console.WriteLine($"Unknown update type: {update.Type}");
return Task.CompletedTask;
}

private static Task Bot_OnCallbackQuery(object sender, CallbackQuery callback)
{
long chatID = callback.Message.Chat.Id;

DatabaseContext db = new DatabaseContext();
Subscribers sub = db.subscribers.Where(s => s.chatID == chatID).FirstOrDefault();

try
{
switch (e.CallbackQuery.Data)
switch (callback.Data)
{
case autoconfirm:
sub.mailprocess = Subscribers.MailProcess.autoconfirm;

bot.AnswerCallbackQueryAsync(
callbackQueryId: e.CallbackQuery.Id,
callbackQueryId: callback.Id,
text: "Thank you, all future mails will be automatically confirmed!"
);
break;
case show:
sub.mailprocess = Subscribers.MailProcess.read;

bot.AnswerCallbackQueryAsync(
callbackQueryId: e.CallbackQuery.Id,
callbackQueryId: callback.Id,
text: "Thank you, I will send all future mails to you!"
);
break;
case listlinks:
sub.mailprocess = Subscribers.MailProcess.readlinks;

bot.AnswerCallbackQueryAsync(
callbackQueryId: e.CallbackQuery.Id,
callbackQueryId: callback.Id,
text: "Thank you, I will filter out all the links in future mails and send them to you!"
);
break;
case refresh:
string originalMail = e.CallbackQuery.Message.Text.Split("\n")[0];
string originalMail = callback.Message.Text.Split("\n")[0];

bot.AnswerCallbackQueryAsync(
callbackQueryId: e.CallbackQuery.Id
callbackQueryId: callback.Id
);
AnswerGenerateMail(db, sub, chatID, custom, originalMail, e.CallbackQuery.Message.MessageId);
AnswerGenerateMail(db, sub, chatID, custom, originalMail, callback.Message.MessageId);
break;
}
}
Expand All @@ -273,11 +326,12 @@ private static void Bot_OnCallbackQuery(object sender, CallbackQueryEventArgs e)
}

db.SaveChanges();
return Task.CompletedTask;
}

private static void Bot_OnMessage(object sender, MessageEventArgs e)
private static Task Bot_OnMessage(object sender, Message message)
{
long chatID = e.Message.Chat.Id;
long chatID = message.Chat.Id;

DatabaseContext db = new DatabaseContext();
Subscribers sub = db.subscribers.Where(s => s.chatID == chatID).FirstOrDefault();
Expand All @@ -289,10 +343,10 @@ private static void Bot_OnMessage(object sender, MessageEventArgs e)
db.subscribers.Add(sub);
}

string command = e.Message.Text;
string command = message.Text;

if (string.IsNullOrEmpty(command))
return;
return Task.CompletedTask;

command = command.ToLower().Replace("@" + botUser.Username.ToLower(), "");

Expand Down Expand Up @@ -350,6 +404,7 @@ private static void Bot_OnMessage(object sender, MessageEventArgs e)
}

db.SaveChanges();
return Task.CompletedTask;
}

static void AnswerGenerateMail(DatabaseContext db, Subscribers sender, long chatID, string command, string customMail, int messageID = 0)
Expand Down
26 changes: 14 additions & 12 deletions TrashmailClient_TelegramBot/TrashmailClient_TelegramBot.csproj
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<LangVersion>latest</LangVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Telegram.Bot" Version="15.5.1" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Telegram.Bot" Version="17.0.0" />
<PackageReference Include="Telegram.Bot.Extensions.Polling" Version="1.0.2" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\DataManager\DataManager.csproj" />
<ProjectReference Include="..\Mail_Crawler\Mail_Crawler.csproj" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\DataManager\DataManager.csproj" />
<ProjectReference Include="..\Mail_Crawler\Mail_Crawler.csproj" />
</ItemGroup>

</Project>

0 comments on commit 367f777

Please sign in to comment.