WIP: implemented registration and EULA accept, WIP on auth issue
This commit is contained in:
@@ -1,17 +1,19 @@
|
||||
using JOBot.TClient.Commands;
|
||||
using JOBot.TClient.Commands.Buttons;
|
||||
using JOBot.TClient.Services;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace JOBot.TClient.Core.HostedServices;
|
||||
|
||||
using Telegram.Bot;
|
||||
using Telegram.Bot.Types;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
|
||||
namespace JOBot.TClient.Core.HostedServices;
|
||||
|
||||
public sealed class BotBackgroundService(
|
||||
ITelegramBotClient botClient,
|
||||
IServiceProvider services,
|
||||
ILogger<BotBackgroundService> logger)
|
||||
ILogger<BotBackgroundService> logger,
|
||||
UserService userService)
|
||||
: BackgroundService
|
||||
{
|
||||
protected override Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
@@ -20,7 +22,7 @@ public sealed class BotBackgroundService(
|
||||
updateHandler: HandleUpdateAsync,
|
||||
errorHandler: HandleErrorAsync,
|
||||
cancellationToken: stoppingToken);
|
||||
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
@@ -29,11 +31,34 @@ public sealed class BotBackgroundService(
|
||||
using var scope = services.CreateScope();
|
||||
var commands = new Dictionary<string, ITelegramCommand>
|
||||
{
|
||||
["/start"] = scope.ServiceProvider.GetRequiredService<StartCommand>()
|
||||
//Commands
|
||||
["/start"] = scope.ServiceProvider.GetRequiredService<StartCommand>(),
|
||||
["/menu"] = scope.ServiceProvider.GetRequiredService<StartCommand>(),
|
||||
|
||||
//Buttons
|
||||
[ButtonResource.EULAAgrement] = scope.ServiceProvider.GetRequiredService<EulaAgreementButtonCommand>(),
|
||||
};
|
||||
|
||||
if (update.Message?.Text is { } text && commands.TryGetValue(text, out var command))
|
||||
await command.ExecuteAsync(update, ct);
|
||||
if (update.Message?.Text is { } text && update.Message?.From != null)
|
||||
{
|
||||
var user = await userService.GetUser(update, ct); //Check user for existance
|
||||
|
||||
if (user == null)
|
||||
{
|
||||
await commands["/start"].ExecuteAsync(update, ct);
|
||||
return;
|
||||
}
|
||||
|
||||
if (commands.TryGetValue(text, out var command))
|
||||
{
|
||||
if (command is IAuthorizedTelegramCommand authorizedTelegramCommand)
|
||||
await authorizedTelegramCommand.ExecuteAsync(update, user, ct);
|
||||
else await command.ExecuteAsync(update, ct); //А если вызвать /start когда зареган? То-то и оно, но всё равно надо подумать
|
||||
return;
|
||||
}
|
||||
|
||||
await bot.SendMessage(update.Message.From.Id, TextResource.CommandNotFound, cancellationToken: ct);
|
||||
}
|
||||
}
|
||||
|
||||
private Task HandleErrorAsync(ITelegramBotClient bot, Exception ex, CancellationToken ct)
|
||||
|
||||
Reference in New Issue
Block a user