diff --git a/JOBot.TClient/Commands/Buttons/EulaAgreementButtonCommand.cs b/JOBot.TClient/Commands/Buttons/EulaAgreementButtonCommand.cs index 3ff584b..4f1c6d3 100644 --- a/JOBot.TClient/Commands/Buttons/EulaAgreementButtonCommand.cs +++ b/JOBot.TClient/Commands/Buttons/EulaAgreementButtonCommand.cs @@ -1,17 +1,20 @@ using JOBot.Proto; +using JOBot.TClient.Infrastructure.Attributes.Authorization; using JOBot.TClient.Services; +using JOBot.TClient.Statements; using Telegram.Bot.Types; using Telegram.Bot.Types.Enums; namespace JOBot.TClient.Commands.Buttons; -public class EulaAgreementButtonCommand(PrepareUserService prepareUserService) : IAuthorizedTelegramCommand +[AcceptNotPrepared] +public class EulaAgreementButtonCommand(PrepareUserState prepareUserState) : IAuthorizedTelegramCommand { public async Task ExecuteAsync(Update update, GetUserResponse user, CancellationToken ct) { if (update.Type != UpdateType.Message || update.Message?.From == null) return; - await prepareUserService.AcceptEula(update, ct); + await prepareUserState.AcceptEula(update, ct); } } \ No newline at end of file diff --git a/JOBot.TClient/Commands/InfoCommand.cs b/JOBot.TClient/Commands/InfoCommand.cs new file mode 100644 index 0000000..084fb2a --- /dev/null +++ b/JOBot.TClient/Commands/InfoCommand.cs @@ -0,0 +1,14 @@ +using Telegram.Bot; +using Telegram.Bot.Types; + +namespace JOBot.TClient.Commands; + +public class InfoCommand(ITelegramBotClient bot) : ITelegramCommand +{ + public async Task ExecuteAsync(Update update, CancellationToken ct) + { + ArgumentNullException.ThrowIfNull(update.Message?.From); + + await bot.SendMessage(update.Message.From.Id, TextResource.Info, cancellationToken: ct); + } +} \ No newline at end of file diff --git a/JOBot.TClient/Commands/MenuCommand.cs b/JOBot.TClient/Commands/MenuCommand.cs new file mode 100644 index 0000000..b5b7393 --- /dev/null +++ b/JOBot.TClient/Commands/MenuCommand.cs @@ -0,0 +1,13 @@ +using JOBot.Proto; +using JOBot.TClient.Services; +using Telegram.Bot.Types; + +namespace JOBot.TClient.Commands; + +public class MenuCommand(MenuService menuService) : IAuthorizedTelegramCommand +{ + public async Task ExecuteAsync(Update update, GetUserResponse user, CancellationToken ct) + { + await menuService.RenderMenu(update, ct); + } +} \ No newline at end of file diff --git a/JOBot.TClient/Core/HostedServices/BotBackgroundService.cs b/JOBot.TClient/Core/HostedServices/BotBackgroundService.cs index 426326f..82264e8 100644 --- a/JOBot.TClient/Core/HostedServices/BotBackgroundService.cs +++ b/JOBot.TClient/Core/HostedServices/BotBackgroundService.cs @@ -1,5 +1,7 @@ using JOBot.TClient.Commands; using JOBot.TClient.Commands.Buttons; +using JOBot.TClient.Infrastructure.Attributes.Authorization; +using JOBot.TClient.Infrastructure.Extensions; using JOBot.TClient.Services; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; @@ -33,7 +35,8 @@ public sealed class BotBackgroundService( { //Commands ["/start"] = scope.ServiceProvider.GetRequiredService(), - ["/menu"] = scope.ServiceProvider.GetRequiredService(), + ["/menu"] = scope.ServiceProvider.GetRequiredService(), + ["/info"] = scope.ServiceProvider.GetRequiredService(), //Buttons [ButtonResource.EULAAgrement] = scope.ServiceProvider.GetRequiredService(), @@ -52,8 +55,16 @@ public sealed class BotBackgroundService( if (commands.TryGetValue(text, out var command)) { if (command is IAuthorizedTelegramCommand authorizedTelegramCommand) + { + var attribute = Attribute.GetCustomAttribute(command.GetType(), typeof(AcceptNotPreparedAttribute)); + if (!user.IsPrepared() && attribute is not AcceptNotPreparedAttribute) + { + await commands["/start"].ExecuteAsync(update, ct); //заставляем пользователя завершить регистрацию + return; + } await authorizedTelegramCommand.ExecuteAsync(update, user, ct); - else await command.ExecuteAsync(update, ct); //А если вызвать /start когда зареган? То-то и оно, но всё равно надо подумать + } + else await command.ExecuteAsync(update, ct); return; } diff --git a/JOBot.TClient/DependencyInjection.cs b/JOBot.TClient/DependencyInjection.cs index faec336..71a7e93 100644 --- a/JOBot.TClient/DependencyInjection.cs +++ b/JOBot.TClient/DependencyInjection.cs @@ -24,6 +24,8 @@ public static class DependencyInjection #region Commands services.AddScoped(); + services.AddScoped(); + services.AddScoped(); //buttons services.AddScoped(); diff --git a/JOBot.TClient/Infrastructure/Attributes/Authorization/AcceptNotPreparedAttribute.cs b/JOBot.TClient/Infrastructure/Attributes/Authorization/AcceptNotPreparedAttribute.cs new file mode 100644 index 0000000..7929497 --- /dev/null +++ b/JOBot.TClient/Infrastructure/Attributes/Authorization/AcceptNotPreparedAttribute.cs @@ -0,0 +1,4 @@ +namespace JOBot.TClient.Infrastructure.Attributes.Authorization; + +[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class)] +public class AcceptNotPreparedAttribute : Attribute; \ No newline at end of file diff --git a/JOBot.TClient/Infrastructure/Extensions/GRpcModelsExtensions.cs b/JOBot.TClient/Infrastructure/Extensions/GRpcModelsExtensions.cs new file mode 100644 index 0000000..ba10820 --- /dev/null +++ b/JOBot.TClient/Infrastructure/Extensions/GRpcModelsExtensions.cs @@ -0,0 +1,9 @@ +using JOBot.Proto; + +namespace JOBot.TClient.Infrastructure.Extensions; + +public static class GRpcModelsExtensions +{ + public static bool IsPrepared(this GetUserResponse user) => user is { Eula: true, IsLogged: true } && + !string.IsNullOrEmpty(user.CVUrl); +} \ No newline at end of file diff --git a/JOBot.TClient/Services/PrepareUserService.cs b/JOBot.TClient/Services/PrepareUserService.cs index 53c753d..4f2921e 100644 --- a/JOBot.TClient/Services/PrepareUserService.cs +++ b/JOBot.TClient/Services/PrepareUserService.cs @@ -1,5 +1,3 @@ -using Grpc.Core; -using JetBrains.Annotations; using JOBot.Proto; using JOBot.TClient.Infrastructure.Exceptions; using Telegram.Bot; @@ -8,7 +6,6 @@ using User = JOBot.Proto.User; namespace JOBot.TClient.Services; -[UsedImplicitly] public class PrepareUserService(ITelegramBotClient bot, User.UserClient userClient) : UserService(bot, userClient) { private readonly ITelegramBotClient _bot = bot; diff --git a/JOBot.TClient/Statements/PrepareUserState.cs b/JOBot.TClient/Statements/PrepareUserState.cs index 11412a6..855ebe4 100644 --- a/JOBot.TClient/Statements/PrepareUserState.cs +++ b/JOBot.TClient/Statements/PrepareUserState.cs @@ -13,10 +13,8 @@ public class PrepareUserState(PrepareUserService prepareUserService, MenuService /// Cancellation token public async Task TryToPrepareUser(Update update, CancellationToken ct = default) { - var user = await prepareUserService.GetUser(update, ct); - if (user == null) - user = await prepareUserService.RegisterUser(update, ct); - + var user = await prepareUserService.GetUser(update, ct) ?? await prepareUserService.RegisterUser(update, ct); + if (!user.Eula) { await prepareUserService.AskForEulaAgreement(update, ct); @@ -31,12 +29,17 @@ public class PrepareUserState(PrepareUserService prepareUserService, MenuService /// /// /// - public async Task UserAcceptedEula(Update update, CancellationToken ct = default) + public async Task AcceptEula(Update update, CancellationToken ct = default) { await prepareUserService.AcceptEula(update, ct: ct); await OnUserEulaValidStage(update, ct); } + /// + /// Continue prepare stage + /// + /// + /// private async Task OnUserEulaValidStage(Update update, CancellationToken ct = default) { await menuService.RenderMenu(update, ct); //boilerplate diff --git a/JOBot.TClient/TextResource.Designer.cs b/JOBot.TClient/TextResource.Designer.cs index 05a2690..2e46a39 100644 --- a/JOBot.TClient/TextResource.Designer.cs +++ b/JOBot.TClient/TextResource.Designer.cs @@ -86,5 +86,14 @@ namespace JOBot.TClient { return ResourceManager.GetString("FallbackMessage", resourceCulture); } } + + /// + /// Looks up a localized string similar to Это бот для упрощения поиска работы на HH.ru. + /// + public static string Info { + get { + return ResourceManager.GetString("Info", resourceCulture); + } + } } } diff --git a/JOBot.TClient/TextResource.resx b/JOBot.TClient/TextResource.resx index 75fc37f..61b671f 100644 --- a/JOBot.TClient/TextResource.resx +++ b/JOBot.TClient/TextResource.resx @@ -28,4 +28,7 @@ Команда не найдена, попробуйте что-то другое + + Это бот для упрощения поиска работы на HH.ru + \ No newline at end of file