diff --git a/Directory.Packages.props b/Directory.Packages.props index bf70a22..ca21be6 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -7,8 +7,8 @@ 0.16.0 - - + + diff --git a/src/Nocr.TelegramClient.AppServices/Handlers/Messages/SubscribeMessage/SubscribeMessageHandler.cs b/src/Nocr.TelegramClient.AppServices/Handlers/Messages/SubscribeMessage/SubscribeMessageHandler.cs index db892d2..195db9f 100644 --- a/src/Nocr.TelegramClient.AppServices/Handlers/Messages/SubscribeMessage/SubscribeMessageHandler.cs +++ b/src/Nocr.TelegramClient.AppServices/Handlers/Messages/SubscribeMessage/SubscribeMessageHandler.cs @@ -6,6 +6,8 @@ using Nocr.TelegramClient.AppServices.Bots.MessageDispatcher; using Nocr.TelegramClient.AppServices.Users; using Nocr.TextMatcher.Api.Contracts.TextMatches; using Nocr.TextMatcher.Api.Contracts.TextMatches.Requests; +using Nocr.TextMatcher.Contracts; +using Nocr.Users.Api.Contracts.Users.Dto; using Telegram.Bot.Types; namespace Nocr.TelegramClient.AppServices.Handlers.Messages.SubscribeMessage; @@ -14,7 +16,7 @@ public class SubscribeMessageHandler : IMatchingUpdateHandler /// Regex to match command "/subscribe @username match_type keywords".
@@ -25,12 +27,12 @@ public class SubscribeMessageHandler : IMatchingUpdateHandler(match.Groups[2].Value, true, out var rule)) + { + _messageQueue.Enqueue(new TextMessage(telegramId) + { + Text = "Не удалось извлечь тип подписки" + }); + return; + } + var template = match.Groups[3].Value; var user = await _usersService.GetOrCreate(telegramId, update.Message.From.Username, cancellationToken); - var matchId = await _textMatchesController.Create(new CreateTextMatchRequest + var subscriptionId = await _textSubscriptionsController.Create(new CreateTextSubscriptionRequest { UserId = user.Id, ChatUsername = username, - Rule = (TextMatchRule)Convert.ToInt32(rule), + Rule = rule, Template = template, }, cancellationToken); _messageQueue.Enqueue(new TextMessage(telegramId) { - Text = $"Подписка создана: {matchId}" + Text = $"Подписка создана: {subscriptionId}" }); } } \ 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 index 051dc6c..ebdb638 100644 --- a/src/Nocr.TelegramClient.AppServices/Handlers/Messages/SubscriptionsMessage/SubscriptionsMessageHandler.cs +++ b/src/Nocr.TelegramClient.AppServices/Handlers/Messages/SubscriptionsMessage/SubscriptionsMessageHandler.cs @@ -5,6 +5,7 @@ using Nocr.TelegramClient.AppServices.Bots.MessageDispatcher; using Nocr.TelegramClient.AppServices.Matches; using Nocr.TelegramClient.AppServices.Users; using Nocr.TextMatcher.Api.Contracts.TextMatches; +using Nocr.Users.Api.Contracts.Users.Dto; using Telegram.Bot.Types; using Telegram.Bot.Types.Enums; @@ -14,14 +15,14 @@ public class SubscriptionsMessageHandler : IMatchingUpdateHandler +public class TextMatchMatchedHandler : IHandleMessages { private readonly ILogger _logger; private readonly IBot _bot; @@ -23,13 +23,13 @@ public class TextMatchMatchedHandler : IHandleMessages _usersService = usersService ?? throw new ArgumentNullException(nameof(usersService)); } - public async Task Handle(TextMatchMatched message) + public async Task Handle(TextSubscriptionMatched message) { - var user = await _usersService.GetById(message.MatchUserId); + var user = await _usersService.GetById(message.SubscriptionUserId); if (user == null) { - _logger.LogWarning("User [{UserId}] of [{MatchId}] from message {MessageId} not found", message.MatchUserId, - message.MatchId, message.Id); + _logger.LogWarning("User [{UserId}] of [{MatchId}] from message {MessageId} not found", message.SubscriptionUserId, + message.SubscriptionId, message.Id); return; } @@ -44,7 +44,7 @@ public class TextMatchMatchedHandler : IHandleMessages var fromUsername = string.IsNullOrWhiteSpace(message.From) ? "anonymous" : message.From; var textMessage = new TextMessage(long.Parse(identity.Identity)) { - Text = $"[{message.PublishedDateTime:MM.dd.yyyy HH:mm:ss}] Найдено совпадение.\nТип совпадения: '{((TextMatchRule)message.Rule).TextView()}'\nШаблон: '{message.Template}'\n{fromUsername} в @{message.ChatUsername}: {message.Text}", + Text = $"[{message.PublishedDateTime:MM.dd.yyyy HH:mm:ss}] Найдено совпадение.\nТип совпадения: '{message.Rule.TextView()}'\nШаблон: '{message.Template}'\n{fromUsername} в @{message.ChatUsername}: {message.Text}", ParseMode = ParseMode.Html }; diff --git a/src/Nocr.TelegramClient.AppServices/Matches/TextMatchExtensions.cs b/src/Nocr.TelegramClient.AppServices/Matches/TextMatchExtensions.cs index 72075ec..91ee631 100644 --- a/src/Nocr.TelegramClient.AppServices/Matches/TextMatchExtensions.cs +++ b/src/Nocr.TelegramClient.AppServices/Matches/TextMatchExtensions.cs @@ -1,11 +1,12 @@ using Nocr.TextMatcher.Api.Contracts.TextMatches; using Nocr.TextMatcher.Api.Contracts.TextMatches.Dto; +using Nocr.TextMatcher.Contracts; namespace Nocr.TelegramClient.AppServices.Matches; public static class TextMatchExtensions { - public static string TextView(this TextMatchData textMatch) + public static string TextView(this TextSubscriptionData textMatch) { var activeText = textMatch.Active ? "Активна" : "Не активна"; var activeCommandText = textMatch.Active @@ -15,15 +16,15 @@ public static class TextMatchExtensions return $"[{textMatch.Id}] (@{textMatch.ChatUsername}) {activeText}: '{textMatch.Rule.TextView()}' > '{textMatch.Template}'\n{activeCommandText}\n{deleteCommandText}"; } - public static string TextView(this TextMatchRule rule) + public static string TextView(this TextSubscriptionRule rule) { switch (rule) { - case TextMatchRule.Full: + case TextSubscriptionRule.Full: return "Полное"; - case TextMatchRule.AllWords: + case TextSubscriptionRule.AllWords: return "Все слова из списка"; - case TextMatchRule.AnyWord: + case TextSubscriptionRule.AnyWord: return "Одно слово из списка"; default: throw new IndexOutOfRangeException(nameof(rule)); diff --git a/src/Nocr.TelegramClient.AppServices/ServiceCollectionExtensions.cs b/src/Nocr.TelegramClient.AppServices/ServiceCollectionExtensions.cs index f5a62d8..79f9ec0 100644 --- a/src/Nocr.TelegramClient.AppServices/ServiceCollectionExtensions.cs +++ b/src/Nocr.TelegramClient.AppServices/ServiceCollectionExtensions.cs @@ -36,13 +36,13 @@ public static class ServiceCollectionExtensions }); services.Configure(configuration.GetSection(nameof(TextMatcherRestEaseOptions))); - services.AddScoped(ctx => + services.AddScoped(ctx => { var options = ctx.GetRequiredService>().Value; var httpClientFactory = ctx.GetRequiredService(); - var client = httpClientFactory.CreateClient(nameof(ITextMatchesController)); + var client = httpClientFactory.CreateClient(nameof(ITextSubscriptionsController)); client.BaseAddress = new Uri(options.BasePath); - return RestClient.For(client); + return RestClient.For(client); }); services.AddScoped(); diff --git a/src/Nocr.TelegramClient.AppServices/Users/IUsersService.cs b/src/Nocr.TelegramClient.AppServices/Users/IUsersService.cs index 4005d00..aff5dc9 100644 --- a/src/Nocr.TelegramClient.AppServices/Users/IUsersService.cs +++ b/src/Nocr.TelegramClient.AppServices/Users/IUsersService.cs @@ -4,7 +4,7 @@ namespace Nocr.TelegramClient.AppServices.Users; public interface IUsersService { - public Task GetOrCreate(long telegramId, string? username, CancellationToken cancellationToken = default); + public Task GetOrCreate(long telegramId, string? username, 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 0e4a656..62122c6 100644 --- a/src/Nocr.TelegramClient.AppServices/Users/UsersService.cs +++ b/src/Nocr.TelegramClient.AppServices/Users/UsersService.cs @@ -13,7 +13,7 @@ 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); if (user == null)