From db970fe87de56b6c30f5b607e81cc30669cf0b71 Mon Sep 17 00:00:00 2001 From: Jetsparrow Date: Sat, 27 Apr 2024 16:23:32 +0300 Subject: [PATCH] Update project and dependencies to .NET8 --- JetHerald/JetHerald.csproj | 14 +++++------ JetHerald/Program.cs | 16 ++++++------ JetHerald/Services/JetHeraldBot.Telegram.cs | 27 +++++++++++---------- 3 files changed, 30 insertions(+), 27 deletions(-) diff --git a/JetHerald/JetHerald.csproj b/JetHerald/JetHerald.csproj index e32830e..fbbfaeb 100644 --- a/JetHerald/JetHerald.csproj +++ b/JetHerald/JetHerald.csproj @@ -1,19 +1,19 @@  - net6.0 + net8.0 en false false - - - - - - + + + + + + \ No newline at end of file diff --git a/JetHerald/Program.cs b/JetHerald/Program.cs index 60ee16c..636f513 100644 --- a/JetHerald/Program.cs +++ b/JetHerald/Program.cs @@ -13,13 +13,18 @@ using JetHerald.Middlewares; using JetHerald.Utils; using JetHerald.Authorization; using Microsoft.Extensions.Hosting; +using NLog; + + #if DEBUG -var debug = true; +var nlogConfigPath = "nlog.debug.config"; #else -var debug = false; +var nlogConfigPath = "nlog.config"; #endif -var log = NLogBuilder.ConfigureNLog(debug ? "nlog.debug.config" : "nlog.config").GetCurrentClassLogger(); +LogManager.ThrowConfigExceptions = true; +LogManager.Setup().LoadConfigurationFromFile(nlogConfigPath); +var log = LogManager.GetCurrentClassLogger(); try { @@ -127,10 +132,7 @@ try app.UseStatusCodePagesWithReExecute("/ui/{0}"); app.UseRouting(); app.UseAuthorization(); - app.UseEndpoints(endpoints => - { - endpoints.MapControllers(); - }); + app.MapControllers(); app.Run(); } catch (Exception exception) diff --git a/JetHerald/Services/JetHeraldBot.Telegram.cs b/JetHerald/Services/JetHeraldBot.Telegram.cs index aefb78b..5bf205f 100644 --- a/JetHerald/Services/JetHeraldBot.Telegram.cs +++ b/JetHerald/Services/JetHeraldBot.Telegram.cs @@ -1,16 +1,17 @@ using System.Threading; + using Telegram.Bot; -using Telegram.Bot.Types.Enums; -using Telegram.Bot.Extensions.Polling; -using Telegram.Bot.Exceptions; +using Telegram.Bot.Polling; using Telegram.Bot.Types; +using Telegram.Bot.Types.Enums; +using Telegram.Bot.Exceptions; + using JetHerald.Commands; -using Telegram.Bot.Types.ReplyMarkups; namespace JetHerald.Services; public partial class JetHeraldBot { - TelegramBotClient TelegramBot { get; set; } + TelegramBotClient Client { get; set; } Telegram.Bot.Types.User Me { get; set; } ChatCommandRouter Commands; CancellationTokenSource TelegramBotShutdownToken { get; } = new(); @@ -19,16 +20,16 @@ public partial class JetHeraldBot if (string.IsNullOrWhiteSpace(TelegramConfig.ApiKey)) return; - TelegramBot = new TelegramBotClient(TelegramConfig.ApiKey); - Me = await TelegramBot.GetMeAsync(); + Client = new TelegramBotClient(TelegramConfig.ApiKey); + Me = await Client.GetMeAsync(); Commands = new ChatCommandRouter(Me.Username, Log); - Commands.Add(new SubscribeCommand(Db, TelegramBot), "subscribe", "sub"); - Commands.Add(new UnsubscribeCommand(Db, TelegramBot), "unsubscribe", "unsub"); - Commands.Add(new ListCommand(Db, TelegramBot), "list"); + Commands.Add(new SubscribeCommand(Db, Client), "subscribe", "sub"); + Commands.Add(new UnsubscribeCommand(Db, Client), "unsubscribe", "unsub"); + Commands.Add(new ListCommand(Db, Client), "list"); var receiverOptions = new ReceiverOptions { AllowedUpdates = new[] { UpdateType.Message } }; - TelegramBot.StartReceiving( + Client.StartReceiving( HandleUpdateAsync, HandleErrorAsync, receiverOptions, @@ -44,7 +45,7 @@ public partial class JetHeraldBot Task SendMessageToTelegramChannel(NamespacedId chat, string formatted) { var id = long.Parse(chat.Id); - return TelegramBot.SendTextMessageAsync(id, formatted); + return Client.SendTextMessageAsync(id, formatted); } Task HandleErrorAsync(ITelegramBotClient botClient, Exception exception, CancellationToken cancellationToken) @@ -67,7 +68,7 @@ public partial class JetHeraldBot { var reply = await Commands.Execute(sender, update); if (reply != null) - await TelegramBot.SendTextMessageAsync( + await Client.SendTextMessageAsync( chatId: msg.Chat.Id, text: reply, replyToMessageId: msg.MessageId);