From 3a91d414438f3e50de8044633f2bdaedbafd3704 Mon Sep 17 00:00:00 2001 From: Sergey Nazarov Date: Fri, 19 Apr 2024 19:43:07 +0000 Subject: [PATCH] nazarovsa/main_menu (#8) Reviewed-on: https://gitea.musk.fun/nocr/telegram-client/pulls/8 Co-authored-by: Sergey Nazarov Co-committed-by: Sergey Nazarov --- .../{IBotExtensions.cs => BotExtensions.cs} | 0 .../Bots/NocrCallbackData.cs | 6 +- .../Bots/NocrState.cs | 7 +- .../Handlers/BaseHandlers/StartHandlerBase.cs | 69 +++++++++++++++++++ .../ViewSubscriptionHandlerBase.cs | 54 ++++++++++++--- .../ActivateSubscriptionHandler.cs | 4 +- .../DeactivateSubscriptionHandler.cs | 4 +- .../DeleteSubscriptionHandler.cs | 19 +++-- .../CallbackQueries/Start/StartHandler.cs | 15 ++++ .../CallbackQueries/Start/StartMatcher.cs | 12 ++++ .../ViewSubscriptionHandler.cs | 13 ++-- .../StartMessage/StartMessageHandler.cs | 29 ++------ .../SubscribeMessageHandler.cs | 10 +-- .../SubscriptionsMessageHandler.cs | 40 ----------- .../SubscriptionsMessageMatcher.cs | 11 --- .../Users/IUsersService.cs | 2 + .../Users/UsersService.cs | 11 ++- .../Nocr.TelegramClient.Host.csproj | 4 ++ .../StartMessageHandler.ru-ru.json | 5 ++ .../StartMessageHandler.ru-ru.json | 3 - 20 files changed, 203 insertions(+), 115 deletions(-) rename src/Nocr.TelegramClient.AppServices/Bots/{IBotExtensions.cs => BotExtensions.cs} (100%) create mode 100644 src/Nocr.TelegramClient.AppServices/Handlers/BaseHandlers/StartHandlerBase.cs create mode 100644 src/Nocr.TelegramClient.AppServices/Handlers/CallbackQueries/Start/StartHandler.cs create mode 100644 src/Nocr.TelegramClient.AppServices/Handlers/CallbackQueries/Start/StartMatcher.cs delete mode 100644 src/Nocr.TelegramClient.AppServices/Handlers/Messages/SubscriptionsMessage/SubscriptionsMessageHandler.cs delete mode 100644 src/Nocr.TelegramClient.AppServices/Handlers/Messages/SubscriptionsMessage/SubscriptionsMessageMatcher.cs create mode 100644 src/Nocr.TelegramClient.Host/Resources/Handlers/TelegramHandlers/Base/StartHandlerBase/StartMessageHandler.ru-ru.json delete mode 100644 src/Nocr.TelegramClient.Host/Resources/Handlers/TelegramHandlers/Messages/StartMessage/StartMessageHandler.ru-ru.json diff --git a/src/Nocr.TelegramClient.AppServices/Bots/IBotExtensions.cs b/src/Nocr.TelegramClient.AppServices/Bots/BotExtensions.cs similarity index 100% rename from src/Nocr.TelegramClient.AppServices/Bots/IBotExtensions.cs rename to src/Nocr.TelegramClient.AppServices/Bots/BotExtensions.cs diff --git a/src/Nocr.TelegramClient.AppServices/Bots/NocrCallbackData.cs b/src/Nocr.TelegramClient.AppServices/Bots/NocrCallbackData.cs index d7bccad..5b2e281 100644 --- a/src/Nocr.TelegramClient.AppServices/Bots/NocrCallbackData.cs +++ b/src/Nocr.TelegramClient.AppServices/Bots/NocrCallbackData.cs @@ -23,8 +23,10 @@ public class NocrCallbackData : CallbackData return new NocrCallbackData(NocrState.DeleteSubscription, subscriptionId.ToString()); } - public static NocrCallbackData ViewSubscription(long subscriptionId) + public static NocrCallbackData ViewSubscription(long? subscriptionId = null) { - return new NocrCallbackData(NocrState.ViewSubscription, subscriptionId.ToString()); + return subscriptionId.HasValue ? + new NocrCallbackData(NocrState.ViewSubscription, subscriptionId.Value.ToString()): + new NocrCallbackData(NocrState.ViewSubscription); } } \ No newline at end of file diff --git a/src/Nocr.TelegramClient.AppServices/Bots/NocrState.cs b/src/Nocr.TelegramClient.AppServices/Bots/NocrState.cs index 01c1d8e..9ff60d0 100644 --- a/src/Nocr.TelegramClient.AppServices/Bots/NocrState.cs +++ b/src/Nocr.TelegramClient.AppServices/Bots/NocrState.cs @@ -4,9 +4,10 @@ public enum NocrState { // Commands ActivateSubscription = 1, - DeactivateSubscription = 2, - DeleteSubscription = 3, + DeactivateSubscription, + DeleteSubscription, // States - ViewSubscription = 4 + Start, + ViewSubscription } \ No newline at end of file diff --git a/src/Nocr.TelegramClient.AppServices/Handlers/BaseHandlers/StartHandlerBase.cs b/src/Nocr.TelegramClient.AppServices/Handlers/BaseHandlers/StartHandlerBase.cs new file mode 100644 index 0000000..6122238 --- /dev/null +++ b/src/Nocr.TelegramClient.AppServices/Handlers/BaseHandlers/StartHandlerBase.cs @@ -0,0 +1,69 @@ +using Insight.Localizer; +using Insight.TelegramBot; +using Insight.TelegramBot.Keyboards; +using Insight.TelegramBot.Models; +using Nocr.TelegramClient.AppServices.Bots; +using Nocr.TelegramClient.AppServices.Bots.MessageDispatcher; +using Nocr.TelegramClient.AppServices.Handlers.Messages.StartMessage; +using Telegram.Bot.Types; +using Telegram.Bot.Types.Enums; +using Telegram.Bot.Types.ReplyMarkups; + +namespace Nocr.TelegramClient.AppServices.Handlers.BaseHandlers; + +public abstract class StartHandlerBase +{ + protected IBot Bot { get; } + protected ILocalizer Localizer { get; } + protected IMessageDispatcherQueue MessageQueue { get; } + + public StartHandlerBase(ILocalizer localizer, IMessageDispatcherQueue messageQueue, IBot bot) + { + Bot = bot ?? throw new ArgumentNullException(nameof(bot)); + Localizer = localizer ?? throw new ArgumentNullException(nameof(localizer)); + MessageQueue = messageQueue ?? throw new ArgumentNullException(nameof(messageQueue)); + } + + public Task Handle(Update update, CancellationToken cancellationToken = default) + { + long telegramId; + switch (update.Type) + { + case UpdateType.Message: + telegramId = update.Message.From.Id; + break; + case UpdateType.CallbackQuery: + telegramId = update.CallbackQuery.From.Id; + break; + default: + throw new ArgumentOutOfRangeException(nameof(update.Type), "Unsupported update type"); + } + + var message = new TextMessage(telegramId) + { + Text = Localizer.Get(nameof(StartMessageHandler), "Text"), + ParseMode = ParseMode.Html, + ReplyMarkup = GetKeyboard() + }; + + if (update.Type == UpdateType.CallbackQuery) + { + return Bot.EditMessageTextAsync(update.CallbackQuery.Message.MessageId, message, cancellationToken); + } + + MessageQueue.Enqueue(message); + return Task.CompletedTask; + } + + private IReplyMarkup GetKeyboard() + { + var markup = new VerticalKeyboardMarkup(); + + markup.Add(new InlineKeyboardButton(Localizer.Get(nameof(StartMessageHandler), "SubscriptionsButton")) + { + CallbackData = NocrCallbackData.ViewSubscription().ToString() + }); + + return markup.InlineKeyboardMarkup; + } +} \ No newline at end of file diff --git a/src/Nocr.TelegramClient.AppServices/Handlers/BaseHandlers/ViewSubscriptionHandlerBase.cs b/src/Nocr.TelegramClient.AppServices/Handlers/BaseHandlers/ViewSubscriptionHandlerBase.cs index 9d59cb1..56baa75 100644 --- a/src/Nocr.TelegramClient.AppServices/Handlers/BaseHandlers/ViewSubscriptionHandlerBase.cs +++ b/src/Nocr.TelegramClient.AppServices/Handlers/BaseHandlers/ViewSubscriptionHandlerBase.cs @@ -7,6 +7,7 @@ using Microsoft.Extensions.Logging; using Nocr.TelegramClient.AppServices.Bots; using Nocr.TelegramClient.AppServices.Bots.MessageDispatcher; using Nocr.TelegramClient.AppServices.TextSubscriptions; +using Nocr.TelegramClient.AppServices.Users; using Nocr.TextMatcher.Api.Contracts.TextMatches; using Nocr.TextMatcher.Api.Contracts.TextMatches.Dto; using Telegram.Bot.Types.Enums; @@ -16,6 +17,7 @@ namespace Nocr.TelegramClient.AppServices.Handlers.BaseHandlers; public abstract class ViewSubscriptionHandlerBase { + protected IUsersService UsersService { get; } protected ILogger Logger { get; } protected IBot Bot { get; } protected ITextSubscriptionsController SubscriptionsController { get; } @@ -23,8 +25,9 @@ public abstract class ViewSubscriptionHandlerBase protected IMessageDispatcherQueue MessageQueue { get; } protected ViewSubscriptionHandlerBase(ILogger logger, ILocalizer localizer, IMessageDispatcherQueue messageQueue, - IBot bot, ITextSubscriptionsController subscriptionsController) + IBot bot, IUsersService usersService, ITextSubscriptionsController subscriptionsController) { + UsersService = usersService ?? throw new ArgumentNullException(nameof(usersService)); Logger = logger ?? throw new ArgumentNullException(nameof(logger)); Bot = bot ?? throw new ArgumentNullException(nameof(bot)); SubscriptionsController = @@ -33,26 +36,53 @@ public abstract class ViewSubscriptionHandlerBase MessageQueue = messageQueue ?? throw new ArgumentNullException(nameof(messageQueue)); } - public async Task SendSubscriptionMessage(long receiverId, TextSubscriptionData subscription, + public async Task SendSubscriptionMessage(long receiverId, long? subscriptionId, CancellationToken cancellationToken = default) { - var message = await GetMessage(receiverId, subscription, cancellationToken); + var message = await GetMessage(receiverId, subscriptionId, cancellationToken); MessageQueue.Enqueue(message); } - public async Task EditSubscriptionMessage(long receiverId, int messageId, long subscriptionId, + public async Task EditSubscriptionMessage(long receiverId, int messageId, long? subscriptionId, CancellationToken cancellationToken = default) { - var subscription = await SubscriptionsController.GetById(subscriptionId, cancellationToken); - var message = await GetMessage(receiverId, subscription, cancellationToken); + var message = await GetMessage(receiverId, subscriptionId, cancellationToken); await Bot.EditOrSendTextMessage(messageId, message, Logger, cancellationToken); } - private async Task GetMessage(long receiverId, TextSubscriptionData subscription, + private async Task GetMessage(long receiverId, long? subscriptionId, CancellationToken cancellationToken = default) { - var subscriptions = await SubscriptionsController.GetByUserId(subscription.UserId, cancellationToken); + // note(nazarovsa): Works only if receiverId is if of an user. If channel - refactor arguments + var user = await UsersService.GetByIdentity(receiverId, cancellationToken); + if (user == null) + { + throw new NotImplementedException($"User with telegramId {receiverId} not found"); + } + + var subscriptions = await SubscriptionsController.GetByUserId(user.Id, cancellationToken); + + if (!subscriptions.Any()) + { + throw new NotImplementedException($"There is no subscriptions for user with id {user.Id}"); + } + var ordered = subscriptions.OrderBy(x => x.Id).ToList(); + + TextSubscriptionData? subscription = null; + if (subscriptionId.HasValue) + { + subscription = subscriptions.FirstOrDefault(x => x.Id == subscriptionId.Value); + if (subscription == null) + { + throw new NotImplementedException($"Subscription with id {subscriptionId.Value} not found"); + } + } + else + { + subscription = ordered.First(); + } + var indexOf = ordered.FindIndex(x => x.Id == subscription.Id); long? prevId = indexOf > 0 ? ordered[indexOf - 1].Id : null; long? nextId = indexOf < ordered.Count - 1 ? ordered[indexOf + 1].Id : null; @@ -95,7 +125,7 @@ public abstract class ViewSubscriptionHandlerBase var deleteButtonText = Localizer.Get("Buttons", "Delete"); var markup = new VerticalKeyboardMarkup(); - + var row = new List(2); if (prevId.HasValue) { @@ -122,10 +152,14 @@ public abstract class ViewSubscriptionHandlerBase ? NocrCallbackData.DeactivateSubscription(textSubscription.Id).ToString() : NocrCallbackData.ActivateSubscription(textSubscription.Id).ToString() }); - + markup.Add(new InlineKeyboardButton(deleteButtonText) { CallbackData = NocrCallbackData.DeleteSubscription(textSubscription.Id).ToString() }); + markup.Add(new InlineKeyboardButton(Localizer.Get("Buttons", "Back")) + { + CallbackData = new NocrCallbackData(NocrState.Start).ToString() + }); return markup; } diff --git a/src/Nocr.TelegramClient.AppServices/Handlers/CallbackQueries/ActivateSubscription/ActivateSubscriptionHandler.cs b/src/Nocr.TelegramClient.AppServices/Handlers/CallbackQueries/ActivateSubscription/ActivateSubscriptionHandler.cs index 610efec..7c50975 100644 --- a/src/Nocr.TelegramClient.AppServices/Handlers/CallbackQueries/ActivateSubscription/ActivateSubscriptionHandler.cs +++ b/src/Nocr.TelegramClient.AppServices/Handlers/CallbackQueries/ActivateSubscription/ActivateSubscriptionHandler.cs @@ -6,6 +6,7 @@ using Microsoft.Extensions.Logging; using Nocr.TelegramClient.AppServices.Bots; using Nocr.TelegramClient.AppServices.Bots.MessageDispatcher; using Nocr.TelegramClient.AppServices.Handlers.BaseHandlers; +using Nocr.TelegramClient.AppServices.Users; using Nocr.TextMatcher.Api.Contracts.TextMatches; using Telegram.Bot.Types; @@ -17,8 +18,9 @@ public class ActivateSubscriptionHandler : ViewSubscriptionHandlerBase, IMatchin ILocalizer localizer, IMessageDispatcherQueue messageQueue, IBot bot, + IUsersService usersService, ITextSubscriptionsController subscriptionsController) - : base(logger, localizer, messageQueue, bot, subscriptionsController) + : base(logger, localizer, messageQueue, bot, usersService, subscriptionsController) { } diff --git a/src/Nocr.TelegramClient.AppServices/Handlers/CallbackQueries/DeactivateSubscription/DeactivateSubscriptionHandler.cs b/src/Nocr.TelegramClient.AppServices/Handlers/CallbackQueries/DeactivateSubscription/DeactivateSubscriptionHandler.cs index b412cb0..d45acaf 100644 --- a/src/Nocr.TelegramClient.AppServices/Handlers/CallbackQueries/DeactivateSubscription/DeactivateSubscriptionHandler.cs +++ b/src/Nocr.TelegramClient.AppServices/Handlers/CallbackQueries/DeactivateSubscription/DeactivateSubscriptionHandler.cs @@ -6,6 +6,7 @@ using Microsoft.Extensions.Logging; using Nocr.TelegramClient.AppServices.Bots; using Nocr.TelegramClient.AppServices.Bots.MessageDispatcher; using Nocr.TelegramClient.AppServices.Handlers.BaseHandlers; +using Nocr.TelegramClient.AppServices.Users; using Nocr.TextMatcher.Api.Contracts.TextMatches; using Telegram.Bot.Types; @@ -19,8 +20,9 @@ public class DeactivateSubscriptionHandler : ViewSubscriptionHandlerBase, ILocalizer localizer, IMessageDispatcherQueue messageQueue, IBot bot, + IUsersService usersService, ITextSubscriptionsController subscriptionsController) - : base(logger, localizer, messageQueue, bot, subscriptionsController) + : base(logger, localizer, messageQueue, bot, usersService, subscriptionsController) { } diff --git a/src/Nocr.TelegramClient.AppServices/Handlers/CallbackQueries/DeleteSubscription/DeleteSubscriptionHandler.cs b/src/Nocr.TelegramClient.AppServices/Handlers/CallbackQueries/DeleteSubscription/DeleteSubscriptionHandler.cs index 2cdf094..5ee82e2 100644 --- a/src/Nocr.TelegramClient.AppServices/Handlers/CallbackQueries/DeleteSubscription/DeleteSubscriptionHandler.cs +++ b/src/Nocr.TelegramClient.AppServices/Handlers/CallbackQueries/DeleteSubscription/DeleteSubscriptionHandler.cs @@ -8,8 +8,10 @@ using Nocr.TelegramClient.AppServices.Bots; using Nocr.TelegramClient.AppServices.Bots.MessageDispatcher; using Nocr.TelegramClient.AppServices.Handlers.BaseHandlers; using Nocr.TelegramClient.AppServices.Handlers.CallbackQueries.DeactivateSubscription; +using Nocr.TelegramClient.AppServices.Users; using Nocr.TextMatcher.Api.Contracts.TextMatches; using Telegram.Bot.Types; +using Telegram.Bot.Types.ReplyMarkups; namespace Nocr.TelegramClient.AppServices.Handlers.CallbackQueries.DeleteSubscription; @@ -20,8 +22,9 @@ public class DeleteSubscriptionHandler : ViewSubscriptionHandlerBase, ILocalizer localizer, IMessageDispatcherQueue messageQueue, IBot bot, - ITextSubscriptionsController subscriptionsController) - : base(logger, localizer, messageQueue, bot, subscriptionsController) + IUsersService usersService, + ITextSubscriptionsController subscriptionsController) + : base(logger, localizer, messageQueue, bot, usersService, subscriptionsController) { } @@ -53,10 +56,16 @@ public class DeleteSubscriptionHandler : ViewSubscriptionHandlerBase, var message = new TextMessage(from) { Text = Localizer.Get(nameof(DeleteSubscriptionHandler), "Text") - .FormatWith(new { Id = subscriptionId }) + .FormatWith(new { Id = subscriptionId }), + ReplyMarkup = new InlineKeyboardMarkup( + new InlineKeyboardButton(Localizer.Get("Buttons", "Back")) + { + CallbackData = NocrCallbackData.ViewSubscription().ToString() + }) }; - - await Bot.EditOrSendTextMessage(update.CallbackQuery.Message.MessageId, message, Logger, CancellationToken.None); + + await Bot.EditOrSendTextMessage(update.CallbackQuery.Message.MessageId, message, Logger, + CancellationToken.None); } private void SendErrorMessage(long from) diff --git a/src/Nocr.TelegramClient.AppServices/Handlers/CallbackQueries/Start/StartHandler.cs b/src/Nocr.TelegramClient.AppServices/Handlers/CallbackQueries/Start/StartHandler.cs new file mode 100644 index 0000000..7886e11 --- /dev/null +++ b/src/Nocr.TelegramClient.AppServices/Handlers/CallbackQueries/Start/StartHandler.cs @@ -0,0 +1,15 @@ +using Insight.Localizer; +using Insight.TelegramBot; +using Insight.TelegramBot.Handling.Handlers; +using Nocr.TelegramClient.AppServices.Bots.MessageDispatcher; +using Nocr.TelegramClient.AppServices.Handlers.BaseHandlers; + +namespace Nocr.TelegramClient.AppServices.Handlers.CallbackQueries.Start; + +public class StartHandler : StartHandlerBase, IMatchingUpdateHandler +{ + public StartHandler(ILocalizer localizer, IMessageDispatcherQueue messageQueue, IBot bot) + : base(localizer, messageQueue, bot) + { + } +} \ No newline at end of file diff --git a/src/Nocr.TelegramClient.AppServices/Handlers/CallbackQueries/Start/StartMatcher.cs b/src/Nocr.TelegramClient.AppServices/Handlers/CallbackQueries/Start/StartMatcher.cs new file mode 100644 index 0000000..4917ee6 --- /dev/null +++ b/src/Nocr.TelegramClient.AppServices/Handlers/CallbackQueries/Start/StartMatcher.cs @@ -0,0 +1,12 @@ +using Insight.TelegramBot.Handling.Matchers.CallbackQueryMatchers; +using Nocr.TelegramClient.AppServices.Bots; + +namespace Nocr.TelegramClient.AppServices.Handlers.CallbackQueries.Start; + +public class StartMatcher : StateCallbackQueryMatcher +{ + public StartMatcher() + { + ExpectingState = NocrState.Start; + } +} \ No newline at end of file diff --git a/src/Nocr.TelegramClient.AppServices/Handlers/CallbackQueries/ViewSubscription/ViewSubscriptionHandler.cs b/src/Nocr.TelegramClient.AppServices/Handlers/CallbackQueries/ViewSubscription/ViewSubscriptionHandler.cs index 4437373..681ef06 100644 --- a/src/Nocr.TelegramClient.AppServices/Handlers/CallbackQueries/ViewSubscription/ViewSubscriptionHandler.cs +++ b/src/Nocr.TelegramClient.AppServices/Handlers/CallbackQueries/ViewSubscription/ViewSubscriptionHandler.cs @@ -6,6 +6,7 @@ using Microsoft.Extensions.Logging; using Nocr.TelegramClient.AppServices.Bots; using Nocr.TelegramClient.AppServices.Bots.MessageDispatcher; using Nocr.TelegramClient.AppServices.Handlers.BaseHandlers; +using Nocr.TelegramClient.AppServices.Users; using Nocr.TextMatcher.Api.Contracts.TextMatches; using Telegram.Bot.Types; @@ -17,8 +18,9 @@ public class ViewSubscriptionHandler : ViewSubscriptionHandlerBase, IMatchingUpd ILocalizer localizer, IMessageDispatcherQueue messageQueue, IBot bot, + IUsersService usersService, ITextSubscriptionsController subscriptionsController) : - base(logger, localizer, messageQueue, bot, subscriptionsController) + base(logger, localizer, messageQueue, bot, usersService, subscriptionsController) { } @@ -27,13 +29,10 @@ public class ViewSubscriptionHandler : ViewSubscriptionHandlerBase, IMatchingUpd var callbackData = NocrCallbackData.Parse(update.CallbackQuery.Data); var from = update.CallbackQuery.From.Id; - if (callbackData.Args.Count != 1 || !long.TryParse(callbackData.Args.First(), out var subscriptionId)) + long? subscriptionId = null; + if (callbackData.Args.Count != 0 && long.TryParse(callbackData.Args.First(), out var subscriptionIdFromMessage)) { - Logger.LogWarning( - "Не удалось извлечь идентификатор подписки. CallbackData: {@CallbackData}, ChatId: {ChatId}, MessageId {MessageId}", - callbackData, from, update.CallbackQuery.Message.Chat.Id); - SendErrorMessage(from); - return; + subscriptionId = subscriptionIdFromMessage; } await EditSubscriptionMessage(from, update.CallbackQuery.Message.MessageId, subscriptionId, cancellationToken); diff --git a/src/Nocr.TelegramClient.AppServices/Handlers/Messages/StartMessage/StartMessageHandler.cs b/src/Nocr.TelegramClient.AppServices/Handlers/Messages/StartMessage/StartMessageHandler.cs index cbc7d3a..f59bea9 100644 --- a/src/Nocr.TelegramClient.AppServices/Handlers/Messages/StartMessage/StartMessageHandler.cs +++ b/src/Nocr.TelegramClient.AppServices/Handlers/Messages/StartMessage/StartMessageHandler.cs @@ -1,33 +1,16 @@ using Insight.Localizer; +using Insight.TelegramBot; using Insight.TelegramBot.Handling.Handlers; -using Insight.TelegramBot.Models; using Nocr.TelegramClient.AppServices.Bots.MessageDispatcher; -using Telegram.Bot.Types; -using Telegram.Bot.Types.Enums; +using Nocr.TelegramClient.AppServices.Handlers.BaseHandlers; namespace Nocr.TelegramClient.AppServices.Handlers.Messages.StartMessage; -public class StartMessageHandler : IMatchingUpdateHandler + +public class StartMessageHandler : StartHandlerBase, IMatchingUpdateHandler { - private readonly ILocalizer _localizer; - private readonly IMessageDispatcherQueue _messageQueue; - - public StartMessageHandler(ILocalizer localizer, IMessageDispatcherQueue messageQueue) + public StartMessageHandler(ILocalizer localizer, IMessageDispatcherQueue messageQueue, IBot bot) + :base(localizer, messageQueue, bot) { - _localizer = localizer ?? throw new ArgumentNullException(nameof(localizer)); - _messageQueue = messageQueue ?? throw new ArgumentNullException(nameof(messageQueue)); - } - - public Task Handle(Update update, CancellationToken cancellationToken = default) - { - var telegramId = update.Message.From.Id; - var message = new TextMessage(telegramId) - { - Text = _localizer.Get(nameof(StartMessageHandler), "Text"), - ParseMode = ParseMode.Html - }; - - _messageQueue.Enqueue(message); - return Task.CompletedTask; } } \ No newline at end of file diff --git a/src/Nocr.TelegramClient.AppServices/Handlers/Messages/SubscribeMessage/SubscribeMessageHandler.cs b/src/Nocr.TelegramClient.AppServices/Handlers/Messages/SubscribeMessage/SubscribeMessageHandler.cs index 7f9ccff..a1de0f9 100644 --- a/src/Nocr.TelegramClient.AppServices/Handlers/Messages/SubscribeMessage/SubscribeMessageHandler.cs +++ b/src/Nocr.TelegramClient.AppServices/Handlers/Messages/SubscribeMessage/SubscribeMessageHandler.cs @@ -17,8 +17,6 @@ namespace Nocr.TelegramClient.AppServices.Handlers.Messages.SubscribeMessage; public class SubscribeMessageHandler : ViewSubscriptionHandlerBase, IMatchingUpdateHandler { - private readonly IUsersService _usersService; - /// /// Regex to match command "/subscribe @username match_type keywords".
/// For instance, "/subscribe @baraholka 1 обувь ботинки сапоги" will create match for a current user with type "All" and pattern "обувь ботинки сапоги". @@ -34,9 +32,8 @@ public class SubscribeMessageHandler : ViewSubscriptionHandlerBase, IMatchingUpd IUsersService usersService, IBot bot, ITextSubscriptionsController subscriptionsController) - : base(logger, localizer, messageQueue, bot, subscriptionsController) + : base(logger, localizer, messageQueue, bot, usersService, subscriptionsController) { - _usersService = usersService ?? throw new ArgumentNullException(nameof(usersService)); } public async Task Handle(Update update, CancellationToken cancellationToken = default) @@ -65,7 +62,7 @@ public class SubscribeMessageHandler : ViewSubscriptionHandlerBase, IMatchingUpd var template = match.Groups[3].Value; - var user = await _usersService.GetOrCreate(receiverId, update.Message.From.Username, cancellationToken); + var user = await UsersService.GetOrCreate(receiverId, update.Message.From.Username, cancellationToken); var subscriptionId = await SubscriptionsController.Create(new CreateTextSubscriptionRequest { UserId = user.Id, @@ -80,7 +77,6 @@ public class SubscribeMessageHandler : ViewSubscriptionHandlerBase, IMatchingUpd .FormatWith(new { Id = subscriptionId }) }); - var subscription = await SubscriptionsController.GetById(subscriptionId, CancellationToken.None); - await SendSubscriptionMessage(receiverId, subscription, CancellationToken.None); + await SendSubscriptionMessage(receiverId, subscriptionId, CancellationToken.None); } } \ No newline at end of file diff --git a/src/Nocr.TelegramClient.AppServices/Handlers/Messages/SubscriptionsMessage/SubscriptionsMessageHandler.cs b/src/Nocr.TelegramClient.AppServices/Handlers/Messages/SubscriptionsMessage/SubscriptionsMessageHandler.cs deleted file mode 100644 index e317db8..0000000 --- a/src/Nocr.TelegramClient.AppServices/Handlers/Messages/SubscriptionsMessage/SubscriptionsMessageHandler.cs +++ /dev/null @@ -1,40 +0,0 @@ -using Insight.Localizer; -using Insight.TelegramBot; -using Insight.TelegramBot.Handling.Handlers; -using Microsoft.Extensions.Logging; -using Nocr.TelegramClient.AppServices.Bots.MessageDispatcher; -using Nocr.TelegramClient.AppServices.Handlers.BaseHandlers; -using Nocr.TelegramClient.AppServices.Users; -using Nocr.TextMatcher.Api.Contracts.TextMatches; -using Telegram.Bot.Types; - -namespace Nocr.TelegramClient.AppServices.Handlers.Messages.SubscriptionsMessage; - -public class SubscriptionsMessageHandler : ViewSubscriptionHandlerBase, - IMatchingUpdateHandler -{ - private readonly IUsersService _usersService; - - public SubscriptionsMessageHandler( - ILogger logger, - ILocalizer localizer, - IMessageDispatcherQueue messageQueue, - IUsersService usersService, - IBot bot, - ITextSubscriptionsController subscriptionsController) - : base(logger, localizer, messageQueue, bot, subscriptionsController) - { - _usersService = usersService ?? throw new ArgumentNullException(nameof(usersService)); - } - - public async Task Handle(Update update, CancellationToken cancellationToken = default) - { - var telegramId = update.Message.From.Id; - var user = await _usersService.GetOrCreate(telegramId, update.Message.From.Username, cancellationToken); - - var subscriptions = await SubscriptionsController.GetByUserId(user.Id, cancellationToken); - var subscription = subscriptions.MinBy(x => x.Id); - - await SendSubscriptionMessage(telegramId, subscription, CancellationToken.None); - } -} \ No newline at end of file diff --git a/src/Nocr.TelegramClient.AppServices/Handlers/Messages/SubscriptionsMessage/SubscriptionsMessageMatcher.cs b/src/Nocr.TelegramClient.AppServices/Handlers/Messages/SubscriptionsMessage/SubscriptionsMessageMatcher.cs deleted file mode 100644 index ec217de..0000000 --- a/src/Nocr.TelegramClient.AppServices/Handlers/Messages/SubscriptionsMessage/SubscriptionsMessageMatcher.cs +++ /dev/null @@ -1,11 +0,0 @@ -using Insight.TelegramBot.Handling.Matchers.TextMatchers; - -namespace Nocr.TelegramClient.AppServices.Handlers.Messages.SubscriptionsMessage; - -public class SubscriptionsMessageMatcher : TextStartWithUpdateMatcher -{ - public SubscriptionsMessageMatcher() - { - Template = "/subscriptions"; - } -} \ No newline at end of file diff --git a/src/Nocr.TelegramClient.AppServices/Users/IUsersService.cs b/src/Nocr.TelegramClient.AppServices/Users/IUsersService.cs index aff5dc9..f96089d 100644 --- a/src/Nocr.TelegramClient.AppServices/Users/IUsersService.cs +++ b/src/Nocr.TelegramClient.AppServices/Users/IUsersService.cs @@ -5,6 +5,8 @@ namespace Nocr.TelegramClient.AppServices.Users; public interface IUsersService { public Task GetOrCreate(long telegramId, string? username, CancellationToken cancellationToken = default); + + public Task GetByIdentity(long telegramId, CancellationToken cancellationToken = default); public Task GetById(long id, CancellationToken cancellationToken = default); } \ No newline at end of file diff --git a/src/Nocr.TelegramClient.AppServices/Users/UsersService.cs b/src/Nocr.TelegramClient.AppServices/Users/UsersService.cs index 62122c6..2480f6c 100644 --- a/src/Nocr.TelegramClient.AppServices/Users/UsersService.cs +++ b/src/Nocr.TelegramClient.AppServices/Users/UsersService.cs @@ -13,9 +13,11 @@ public sealed class UsersService : IUsersService _usersController = usersController ?? throw new ArgumentNullException(nameof(usersController)); } - public async Task GetOrCreate(long telegramId, string? username, CancellationToken cancellationToken = default) + public async Task GetOrCreate(long telegramId, string? username, + CancellationToken cancellationToken = default) { - var user = await _usersController.GetByIdentity(UserIdentityType.TelegramId, telegramId.ToString(), cancellationToken); + var user = await _usersController.GetByIdentity(UserIdentityType.TelegramId, telegramId.ToString(), + cancellationToken); if (user == null) { await _usersController.Create(new CreateUserRequest @@ -32,6 +34,11 @@ public sealed class UsersService : IUsersService return user; } + public Task GetByIdentity(long telegramId, CancellationToken cancellationToken = default) + { + return _usersController.GetByIdentity(UserIdentityType.TelegramId, telegramId.ToString(), cancellationToken); + } + public Task GetById(long id, CancellationToken cancellationToken = default) { return _usersController.GetById(id, cancellationToken); diff --git a/src/Nocr.TelegramClient.Host/Nocr.TelegramClient.Host.csproj b/src/Nocr.TelegramClient.Host/Nocr.TelegramClient.Host.csproj index 4cf8745..660293c 100644 --- a/src/Nocr.TelegramClient.Host/Nocr.TelegramClient.Host.csproj +++ b/src/Nocr.TelegramClient.Host/Nocr.TelegramClient.Host.csproj @@ -25,5 +25,9 @@ + + <_ContentIncludedByDefault Remove="Resources\Handlers\TelegramHandlers\Messages\StartMessage\StartMessageHandler.ru-ru.json" /> + + diff --git a/src/Nocr.TelegramClient.Host/Resources/Handlers/TelegramHandlers/Base/StartHandlerBase/StartMessageHandler.ru-ru.json b/src/Nocr.TelegramClient.Host/Resources/Handlers/TelegramHandlers/Base/StartHandlerBase/StartMessageHandler.ru-ru.json new file mode 100644 index 0000000..f0a1a99 --- /dev/null +++ b/src/Nocr.TelegramClient.Host/Resources/Handlers/TelegramHandlers/Base/StartHandlerBase/StartMessageHandler.ru-ru.json @@ -0,0 +1,5 @@ +{ + "Text": "Привет! Я, Nocr 🤖!", + + "SubscriptionsButton": "Подписки 📩" +} \ No newline at end of file diff --git a/src/Nocr.TelegramClient.Host/Resources/Handlers/TelegramHandlers/Messages/StartMessage/StartMessageHandler.ru-ru.json b/src/Nocr.TelegramClient.Host/Resources/Handlers/TelegramHandlers/Messages/StartMessage/StartMessageHandler.ru-ru.json deleted file mode 100644 index 5f3dcbc..0000000 --- a/src/Nocr.TelegramClient.Host/Resources/Handlers/TelegramHandlers/Messages/StartMessage/StartMessageHandler.ru-ru.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "Text": "Привет! Я, Nocr 🤖!" -} \ No newline at end of file