Update project and dependencies to .NET8

This commit is contained in:
Jetsparrow 2024-04-27 16:23:32 +03:00
parent b938e37fdd
commit db970fe87d
3 changed files with 30 additions and 27 deletions

View File

@ -1,19 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk.Web"> <Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net6.0</TargetFramework> <TargetFramework>net8.0</TargetFramework>
<SatelliteResourceLanguages>en</SatelliteResourceLanguages> <SatelliteResourceLanguages>en</SatelliteResourceLanguages>
<ProduceReferenceAssembly>false</ProduceReferenceAssembly> <ProduceReferenceAssembly>false</ProduceReferenceAssembly>
<PreserveCompilationContext>false</PreserveCompilationContext> <PreserveCompilationContext>false</PreserveCompilationContext>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Dapper.Transaction" Version="2.0.35.2" /> <PackageReference Include="Dapper.Transaction" Version="2.1.28" />
<PackageReference Include="DSharpPlus" Version="4.2.0" /> <PackageReference Include="DSharpPlus" Version="4.4.6" />
<PackageReference Include="DSharpPlus.CommandsNext" Version="4.2.0" /> <PackageReference Include="DSharpPlus.CommandsNext" Version="4.4.6" />
<PackageReference Include="MySql.Data" Version="8.0.28" /> <PackageReference Include="MySql.Data" Version="8.3.0" />
<PackageReference Include="NLog.Web.AspNetCore" Version="5.0.0-rc2" /> <PackageReference Include="NLog.Web.AspNetCore" Version="5.3.8" />
<PackageReference Include="Telegram.Bot.Extensions.Polling" Version="1.0.2" /> <PackageReference Include="Telegram.Bot" Version="19.0.0" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -13,13 +13,18 @@ using JetHerald.Middlewares;
using JetHerald.Utils; using JetHerald.Utils;
using JetHerald.Authorization; using JetHerald.Authorization;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using NLog;
#if DEBUG #if DEBUG
var debug = true; var nlogConfigPath = "nlog.debug.config";
#else #else
var debug = false; var nlogConfigPath = "nlog.config";
#endif #endif
var log = NLogBuilder.ConfigureNLog(debug ? "nlog.debug.config" : "nlog.config").GetCurrentClassLogger(); LogManager.ThrowConfigExceptions = true;
LogManager.Setup().LoadConfigurationFromFile(nlogConfigPath);
var log = LogManager.GetCurrentClassLogger();
try try
{ {
@ -127,10 +132,7 @@ try
app.UseStatusCodePagesWithReExecute("/ui/{0}"); app.UseStatusCodePagesWithReExecute("/ui/{0}");
app.UseRouting(); app.UseRouting();
app.UseAuthorization(); app.UseAuthorization();
app.UseEndpoints(endpoints => app.MapControllers();
{
endpoints.MapControllers();
});
app.Run(); app.Run();
} }
catch (Exception exception) catch (Exception exception)

View File

@ -1,16 +1,17 @@
using System.Threading; using System.Threading;
using Telegram.Bot; using Telegram.Bot;
using Telegram.Bot.Types.Enums; using Telegram.Bot.Polling;
using Telegram.Bot.Extensions.Polling;
using Telegram.Bot.Exceptions;
using Telegram.Bot.Types; using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;
using Telegram.Bot.Exceptions;
using JetHerald.Commands; using JetHerald.Commands;
using Telegram.Bot.Types.ReplyMarkups;
namespace JetHerald.Services; namespace JetHerald.Services;
public partial class JetHeraldBot public partial class JetHeraldBot
{ {
TelegramBotClient TelegramBot { get; set; } TelegramBotClient Client { get; set; }
Telegram.Bot.Types.User Me { get; set; } Telegram.Bot.Types.User Me { get; set; }
ChatCommandRouter Commands; ChatCommandRouter Commands;
CancellationTokenSource TelegramBotShutdownToken { get; } = new(); CancellationTokenSource TelegramBotShutdownToken { get; } = new();
@ -19,16 +20,16 @@ public partial class JetHeraldBot
if (string.IsNullOrWhiteSpace(TelegramConfig.ApiKey)) if (string.IsNullOrWhiteSpace(TelegramConfig.ApiKey))
return; return;
TelegramBot = new TelegramBotClient(TelegramConfig.ApiKey); Client = new TelegramBotClient(TelegramConfig.ApiKey);
Me = await TelegramBot.GetMeAsync(); Me = await Client.GetMeAsync();
Commands = new ChatCommandRouter(Me.Username, Log); Commands = new ChatCommandRouter(Me.Username, Log);
Commands.Add(new SubscribeCommand(Db, TelegramBot), "subscribe", "sub"); Commands.Add(new SubscribeCommand(Db, Client), "subscribe", "sub");
Commands.Add(new UnsubscribeCommand(Db, TelegramBot), "unsubscribe", "unsub"); Commands.Add(new UnsubscribeCommand(Db, Client), "unsubscribe", "unsub");
Commands.Add(new ListCommand(Db, TelegramBot), "list"); Commands.Add(new ListCommand(Db, Client), "list");
var receiverOptions = new ReceiverOptions { AllowedUpdates = new[] { UpdateType.Message } }; var receiverOptions = new ReceiverOptions { AllowedUpdates = new[] { UpdateType.Message } };
TelegramBot.StartReceiving( Client.StartReceiving(
HandleUpdateAsync, HandleUpdateAsync,
HandleErrorAsync, HandleErrorAsync,
receiverOptions, receiverOptions,
@ -44,7 +45,7 @@ public partial class JetHeraldBot
Task SendMessageToTelegramChannel(NamespacedId chat, string formatted) Task SendMessageToTelegramChannel(NamespacedId chat, string formatted)
{ {
var id = long.Parse(chat.Id); var id = long.Parse(chat.Id);
return TelegramBot.SendTextMessageAsync(id, formatted); return Client.SendTextMessageAsync(id, formatted);
} }
Task HandleErrorAsync(ITelegramBotClient botClient, Exception exception, CancellationToken cancellationToken) Task HandleErrorAsync(ITelegramBotClient botClient, Exception exception, CancellationToken cancellationToken)
@ -67,7 +68,7 @@ public partial class JetHeraldBot
{ {
var reply = await Commands.Execute(sender, update); var reply = await Commands.Execute(sender, update);
if (reply != null) if (reply != null)
await TelegramBot.SendTextMessageAsync( await Client.SendTextMessageAsync(
chatId: msg.Chat.Id, chatId: msg.Chat.Id,
text: reply, text: reply,
replyToMessageId: msg.MessageId); replyToMessageId: msg.MessageId);