diff --git a/src/Nocr.TelegramClient.AppServices/Handlers/BaseHandlers/StartHandlerBase.cs b/src/Nocr.TelegramClient.AppServices/Handlers/BaseHandlers/StartHandlerBase.cs index 255d577..5565123 100644 --- a/src/Nocr.TelegramClient.AppServices/Handlers/BaseHandlers/StartHandlerBase.cs +++ b/src/Nocr.TelegramClient.AppServices/Handlers/BaseHandlers/StartHandlerBase.cs @@ -35,7 +35,7 @@ public abstract class StartHandlerBase telegramId = update.Message.From.Id; break; case UpdateType.CallbackQuery: - telegramId = update.CallbackQuery.From.Id; + telegramId = update.CallbackQuery.From.Id; break; default: throw new ArgumentOutOfRangeException(nameof(update.Type), "Unsupported update type"); diff --git a/src/Nocr.TelegramClient.AppServices/Handlers/CallbackQueries/AddSubscription/AddSubscriptionHandler.cs b/src/Nocr.TelegramClient.AppServices/Handlers/CallbackQueries/AddSubscription/AddSubscriptionHandler.cs index a65a575..15c2d91 100644 --- a/src/Nocr.TelegramClient.AppServices/Handlers/CallbackQueries/AddSubscription/AddSubscriptionHandler.cs +++ b/src/Nocr.TelegramClient.AppServices/Handlers/CallbackQueries/AddSubscription/AddSubscriptionHandler.cs @@ -17,8 +17,6 @@ public sealed class AddSubscriptionHandler : IMatchingUpdateHandler logger, ILocalizer localizer, IBot bot, IMessageDispatcherQueue messageDispatcherQueue) { _logger = logger ?? throw new ArgumentNullException(nameof(logger)); @@ -32,10 +30,9 @@ public sealed class AddSubscriptionHandler : IMatchingUpdateHandler +{ + /// + /// Regex to match command "/subscribe_all @username keywords".
+ /// For instance, "/subscribe_all @baraholka обувь ботинки сапоги" will create match for a current user with type "All" and pattern "обувь ботинки сапоги". + ///
+ private static Regex _commandRegex = + new Regex(@"^/subscribe_all (.*\B@(?=\w{5,32}\b)[a-zA-Z0-9]+(?:_[а-яА-Яa-zA-Z0-9]+)*) (\s*[а-яА-ЯA-Za-z0-9]+(?:\s+[а-яА-ЯA-Za-z0-9]+)*\s*)$", + RegexOptions.Compiled); + + public SubscribeAllMessageHandler( + ILogger logger, + ILocalizer localizer, + IMessageDispatcherQueue messageQueue, + IUsersService usersService, + IBot bot, + ITextSubscriptionsController subscriptionsController) + : base(logger, localizer, messageQueue, bot, usersService, subscriptionsController) + { + } + + public Task Handle(Update update, CancellationToken cancellationToken = default) + { + return base.Handle(update, _commandRegex, TextSubscriptionRule.AllWords, cancellationToken); + } +} \ No newline at end of file diff --git a/src/Nocr.TelegramClient.AppServices/Handlers/Messages/SubscribeAllMessage/SubscribeAllMessageMatcher.cs b/src/Nocr.TelegramClient.AppServices/Handlers/Messages/SubscribeAllMessage/SubscribeAllMessageMatcher.cs new file mode 100644 index 0000000..8e1265d --- /dev/null +++ b/src/Nocr.TelegramClient.AppServices/Handlers/Messages/SubscribeAllMessage/SubscribeAllMessageMatcher.cs @@ -0,0 +1,11 @@ +using Insight.TelegramBot.Handling.Matchers.TextMatchers; + +namespace Nocr.TelegramClient.AppServices.Handlers.Messages.SubscribeAllMessage; + +public sealed class SubscribeAllMessageMatcher : TextStartWithUpdateMatcher +{ + public SubscribeAllMessageMatcher() + { + Template = "/subscribe_all "; + } +} \ No newline at end of file diff --git a/src/Nocr.TelegramClient.AppServices/Handlers/Messages/SubscribeAnyMessage/SubscribeAnyMessageHandler.cs b/src/Nocr.TelegramClient.AppServices/Handlers/Messages/SubscribeAnyMessage/SubscribeAnyMessageHandler.cs new file mode 100644 index 0000000..4480f08 --- /dev/null +++ b/src/Nocr.TelegramClient.AppServices/Handlers/Messages/SubscribeAnyMessage/SubscribeAnyMessageHandler.cs @@ -0,0 +1,42 @@ +using System.Text.RegularExpressions; +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.Messages.SubscribeExactMessage; +using Nocr.TelegramClient.AppServices.Users; +using Nocr.TextMatcher.Api.Contracts.TextMatches; +using Nocr.TextMatcher.Contracts; +using Telegram.Bot.Types; + +namespace Nocr.TelegramClient.AppServices.Handlers.Messages.SubscribeAnyMessage; + +public sealed class SubscribeAnyMessageHandler : SubscribeHandlerBase, + IMatchingUpdateHandler +{ + /// + /// Regex to match command "/subscribe_any @username keywords".
+ /// For instance, "/subscribe_any @baraholka обувь ботинки сапоги" will create match for a current user with type "Any" and pattern "обувь ботинки сапоги". + ///
+ private static Regex _commandRegex = + new Regex( + @"^/subscribe_any (.*\B@(?=\w{5,32}\b)[a-zA-Z0-9]+(?:_[а-яА-Яa-zA-Z0-9]+)*) (\s*[а-яА-ЯA-Za-z0-9]+(?:\s+[а-яА-ЯA-Za-z0-9]+)*\s*)$", + RegexOptions.Compiled); + + public SubscribeAnyMessageHandler( + ILogger logger, + ILocalizer localizer, + IMessageDispatcherQueue messageQueue, + IUsersService usersService, + IBot bot, + ITextSubscriptionsController subscriptionsController) + : base(logger, localizer, messageQueue, bot, usersService, subscriptionsController) + { + } + + public Task Handle(Update update, CancellationToken cancellationToken = default) + { + return base.Handle(update, _commandRegex, TextSubscriptionRule.AnyWord, cancellationToken); + } +} \ No newline at end of file diff --git a/src/Nocr.TelegramClient.AppServices/Handlers/Messages/SubscribeAnyMessage/SubscribeAnyMessageMatcher.cs b/src/Nocr.TelegramClient.AppServices/Handlers/Messages/SubscribeAnyMessage/SubscribeAnyMessageMatcher.cs new file mode 100644 index 0000000..00ef795 --- /dev/null +++ b/src/Nocr.TelegramClient.AppServices/Handlers/Messages/SubscribeAnyMessage/SubscribeAnyMessageMatcher.cs @@ -0,0 +1,11 @@ +using Insight.TelegramBot.Handling.Matchers.TextMatchers; + +namespace Nocr.TelegramClient.AppServices.Handlers.Messages.SubscribeAnyMessage; + +public sealed class SubscribeAnyMessageMatcher : TextStartWithUpdateMatcher +{ + public SubscribeAnyMessageMatcher() + { + Template = "/subscribe_any "; + } +} \ No newline at end of file diff --git a/src/Nocr.TelegramClient.AppServices/Handlers/Messages/SubscribeExactMessage/SubscribeExactMessageHandler.cs b/src/Nocr.TelegramClient.AppServices/Handlers/Messages/SubscribeExactMessage/SubscribeExactMessageHandler.cs new file mode 100644 index 0000000..a3d9a9f --- /dev/null +++ b/src/Nocr.TelegramClient.AppServices/Handlers/Messages/SubscribeExactMessage/SubscribeExactMessageHandler.cs @@ -0,0 +1,39 @@ +using System.Text.RegularExpressions; +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.Users; +using Nocr.TextMatcher.Api.Contracts.TextMatches; +using Nocr.TextMatcher.Contracts; +using Telegram.Bot.Types; + +namespace Nocr.TelegramClient.AppServices.Handlers.Messages.SubscribeExactMessage; + +public class SubscribeExactMessageHandler : SubscribeHandlerBase, IMatchingUpdateHandler +{ + /// + /// Regex to match command "/subscribe @username keywords".
+ /// For instance, "/subscribe @baraholka обувь" will create match for a current user with type "Full" and pattern "обувь". + ///
+ private static Regex _commandRegex = + new Regex(@"^/subscribe (.*\B@(?=\w{5,32}\b)[a-zA-Z0-9]+(?:_[a-zA-Z0-9]+)*.*) ([а-яА-Яa-zA-Z0-9]{3,})$", + RegexOptions.Compiled); + + public SubscribeExactMessageHandler( + ILogger logger, + ILocalizer localizer, + IMessageDispatcherQueue messageQueue, + IUsersService usersService, + IBot bot, + ITextSubscriptionsController subscriptionsController) + : base(logger, localizer, messageQueue, bot, usersService, subscriptionsController) + { + } + + public Task Handle(Update update, CancellationToken cancellationToken = default) + { + return base.Handle(update, _commandRegex, TextSubscriptionRule.Full, cancellationToken); + } +} \ No newline at end of file diff --git a/src/Nocr.TelegramClient.AppServices/Handlers/Messages/SubscribeExactMessage/SubscribeExactMessageMatcher.cs b/src/Nocr.TelegramClient.AppServices/Handlers/Messages/SubscribeExactMessage/SubscribeExactMessageMatcher.cs new file mode 100644 index 0000000..e48666a --- /dev/null +++ b/src/Nocr.TelegramClient.AppServices/Handlers/Messages/SubscribeExactMessage/SubscribeExactMessageMatcher.cs @@ -0,0 +1,11 @@ +using Insight.TelegramBot.Handling.Matchers.TextMatchers; + +namespace Nocr.TelegramClient.AppServices.Handlers.Messages.SubscribeExactMessage; + +public sealed class SubscribeExactMessageMatcher : TextStartWithUpdateMatcher +{ + public SubscribeExactMessageMatcher() + { + Template = "/subscribe "; + } +} \ 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/SubscribeExactMessage/SubscribeHandlerBase.cs similarity index 54% rename from src/Nocr.TelegramClient.AppServices/Handlers/Messages/SubscribeMessage/SubscribeMessageHandler.cs rename to src/Nocr.TelegramClient.AppServices/Handlers/Messages/SubscribeExactMessage/SubscribeHandlerBase.cs index bf329d7..1a81456 100644 --- a/src/Nocr.TelegramClient.AppServices/Handlers/Messages/SubscribeMessage/SubscribeMessageHandler.cs +++ b/src/Nocr.TelegramClient.AppServices/Handlers/Messages/SubscribeExactMessage/SubscribeHandlerBase.cs @@ -2,7 +2,6 @@ using System.Text.RegularExpressions; using FormatWith; using Insight.Localizer; using Insight.TelegramBot; -using Insight.TelegramBot.Handling.Handlers; using Insight.TelegramBot.Models; using Microsoft.Extensions.Logging; using Nocr.TelegramClient.AppServices.Bots.MessageDispatcher; @@ -13,47 +12,32 @@ using Nocr.TextMatcher.Api.Contracts.TextMatches.Requests; using Nocr.TextMatcher.Contracts; using Telegram.Bot.Types; -namespace Nocr.TelegramClient.AppServices.Handlers.Messages.SubscribeMessage; +namespace Nocr.TelegramClient.AppServices.Handlers.Messages.SubscribeExactMessage; -public class SubscribeMessageHandler : ViewSubscriptionHandlerBase, IMatchingUpdateHandler +public abstract class SubscribeHandlerBase : ViewSubscriptionHandlerBase { - /// - /// 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 "обувь ботинки сапоги". - ///
- private static Regex _commandRegex = - new Regex(@"^/subscribe (.*\B@(?=\w{5,32}\b)[a-zA-Z0-9]+(?:_[a-zA-Z0-9]+)*.*) (.{3,})$", - RegexOptions.Compiled); - - public SubscribeMessageHandler( - ILogger logger, - ILocalizer localizer, - IMessageDispatcherQueue messageQueue, - IUsersService usersService, - IBot bot, - ITextSubscriptionsController subscriptionsController) + protected SubscribeHandlerBase(ILogger logger, ILocalizer localizer, IMessageDispatcherQueue messageQueue, IBot bot, + IUsersService usersService, ITextSubscriptionsController subscriptionsController) : base(logger, localizer, messageQueue, bot, usersService, subscriptionsController) { } - public async Task Handle(Update update, CancellationToken cancellationToken = default) + protected async Task Handle(Update update, Regex regex, TextSubscriptionRule rule, CancellationToken cancellationToken = default) { var receiverId = update.Message.Chat.Id; - var match = _commandRegex.Match(update.Message.Text); + var match = regex.Match(update.Message.Text); if (!match.Success) { MessageQueue.Enqueue(new TextMessage(receiverId) { - Text = Localizer.Get(nameof(SubscribeMessageHandler), "CommandHasWrongFormat") + Text = Localizer.Get(nameof(SubscribeHandlerBase), "CommandHasWrongFormat") }); return; } var username = match.Groups[1].Value.TrimStart('@'); var template = match.Groups[2].Value; - // note(nazarovsa): Temporary for simplify usage - var rule = TextSubscriptionRule.Full; var user = await UsersService.GetOrCreate(receiverId, update.Message.From.Username, cancellationToken); var subscriptionId = await SubscriptionsController.Create(new CreateTextSubscriptionRequest @@ -66,7 +50,7 @@ public class SubscribeMessageHandler : ViewSubscriptionHandlerBase, IMatchingUpd MessageQueue.Enqueue(new TextMessage(receiverId) { - Text = Localizer.Get(nameof(SubscribeMessageHandler), "Text") + Text = Localizer.Get(nameof(SubscribeHandlerBase), "Text") .FormatWith(new { Id = subscriptionId }) }); diff --git a/src/Nocr.TelegramClient.AppServices/Handlers/Messages/SubscribeMessage/SubscribeMessageMatcher.cs b/src/Nocr.TelegramClient.AppServices/Handlers/Messages/SubscribeMessage/SubscribeMessageMatcher.cs deleted file mode 100644 index c5c3d13..0000000 --- a/src/Nocr.TelegramClient.AppServices/Handlers/Messages/SubscribeMessage/SubscribeMessageMatcher.cs +++ /dev/null @@ -1,11 +0,0 @@ -using Insight.TelegramBot.Handling.Matchers.TextMatchers; - -namespace Nocr.TelegramClient.AppServices.Handlers.Messages.SubscribeMessage; - -public sealed class SubscribeMessageMatcher : TextStartWithUpdateMatcher -{ - public SubscribeMessageMatcher() - { - Template = "/subscribe"; - } -} \ No newline at end of file diff --git a/src/Nocr.TelegramClient.Host/Resources/Elements/TextSubscriptionRules/TextSubscriptionRule.ru-ru.json b/src/Nocr.TelegramClient.Host/Resources/Elements/TextSubscriptionRules/TextSubscriptionRule.ru-ru.json index 1b9f9dd..06fcd40 100644 --- a/src/Nocr.TelegramClient.Host/Resources/Elements/TextSubscriptionRules/TextSubscriptionRule.ru-ru.json +++ b/src/Nocr.TelegramClient.Host/Resources/Elements/TextSubscriptionRules/TextSubscriptionRule.ru-ru.json @@ -1,5 +1,5 @@ { - "Full": "Полное", + "Full": "Полное совпадение", "AllWords": "Все слова из списка", "AnyWord": "Одно слово из списка" } \ No newline at end of file diff --git a/src/Nocr.TelegramClient.Host/Resources/Handlers/TelegramHandlers/Messages/SubscribeMessage/SubscribeMessageHandler.ru-ru.json b/src/Nocr.TelegramClient.Host/Resources/Handlers/TelegramHandlers/Base/SubscribeHandlerBase/SubscribeHandlerBase.ru-ru.json similarity index 100% rename from src/Nocr.TelegramClient.Host/Resources/Handlers/TelegramHandlers/Messages/SubscribeMessage/SubscribeMessageHandler.ru-ru.json rename to src/Nocr.TelegramClient.Host/Resources/Handlers/TelegramHandlers/Base/SubscribeHandlerBase/SubscribeHandlerBase.ru-ru.json diff --git a/src/Nocr.TelegramClient.Host/Resources/Handlers/TelegramHandlers/Base/SubscriptionHandlerBase/ViewSubscriptionHandlerBase.ru-ru.json b/src/Nocr.TelegramClient.Host/Resources/Handlers/TelegramHandlers/Base/SubscriptionHandlerBase/ViewSubscriptionHandlerBase.ru-ru.json index b9b4a66..44e52bd 100644 --- a/src/Nocr.TelegramClient.Host/Resources/Handlers/TelegramHandlers/Base/SubscriptionHandlerBase/ViewSubscriptionHandlerBase.ru-ru.json +++ b/src/Nocr.TelegramClient.Host/Resources/Handlers/TelegramHandlers/Base/SubscriptionHandlerBase/ViewSubscriptionHandlerBase.ru-ru.json @@ -1,7 +1,7 @@ { "Active": "Активна", "Inactive": "Не активна", - "Text": "Подписка #{Id}\nЧат: (@{ChatUsername})\nСтатус: {ActiveText}\nШаблон: {Template}", + "Text": "✉️ Подписка #{Id}\n💬 Чат: @{ChatUsername}\n👀 Статус: {ActiveText}\n🎲 Тип подписки: {Rule}\n🔑 Шаблон: {Template}", "ActivateButton": "Активировать", "DeactivateButton": "Деактивировать" } \ No newline at end of file diff --git a/src/Nocr.TelegramClient.Host/Resources/Handlers/TelegramHandlers/CallbackQueries/AddSubscription/AddSubscriptionHandler.ru-ru.json b/src/Nocr.TelegramClient.Host/Resources/Handlers/TelegramHandlers/CallbackQueries/AddSubscription/AddSubscriptionHandler.ru-ru.json index cec2d66..d57b242 100644 --- a/src/Nocr.TelegramClient.Host/Resources/Handlers/TelegramHandlers/CallbackQueries/AddSubscription/AddSubscriptionHandler.ru-ru.json +++ b/src/Nocr.TelegramClient.Host/Resources/Handlers/TelegramHandlers/CallbackQueries/AddSubscription/AddSubscriptionHandler.ru-ru.json @@ -1,3 +1,3 @@ { - "Text": "Формат команды для подписки:\n\n`/subscribe @chat_username ключевое_слово`.\n\nКлючевое слово должно быть более 2 символов. \n\nНапример: `/subscribe @baraholka_tbi iphone`." + "Text": "Существует несколько вариантов подписки: точное совпадение, одно слово из списка, все слова из списка.\n\nДля подписки с точным совпадением пришлите команду в формате:\n`/subscribe @chat_username ключевое_слово`.\nНапример: `/subscribe @baraholka_tbi iphone` подпишется на все сообщения, в которых содержится ключевое слово iphone.\n\nДля подписки на одно слово из списка пришлите команду в формате:\n`/subscribe_any @chat_username ключевые_слова_через_пробел`\nНапример `/subscribe_any @baraholka_tbi macbook mac макбук` создаст подписку на сообщения, в которых встречается ХОТЯ БЫ ОДНО слово из списка macbook mac макбук\n\nДля подписки на все слова из списка пришлите команду в формате: `/subscribe_all @chat_username ключевые_слова_через_пробел`\nНапример: `/subscribe_any @baraholka_tbi велосипед cube` подпишется на все сообщения, в которых ОДНОВРЕМЕННО содержатся все ключевые слова из списка велосипед cube.\n\nКлючевое слово должно содержать более 2 символов." } \ No newline at end of file