diff --git a/src/Nocr.TelegramClient.AppServices/ServiceCollectionExtensions.cs b/src/Nocr.TelegramClient.AppServices/ServiceCollectionExtensions.cs index df353a1..b59c75d 100644 --- a/src/Nocr.TelegramClient.AppServices/ServiceCollectionExtensions.cs +++ b/src/Nocr.TelegramClient.AppServices/ServiceCollectionExtensions.cs @@ -24,6 +24,7 @@ public static class ServiceCollectionExtensions // Add registrations here services.AddRebusHandler(); + services.AddRebusHandler(); services.AddHttpClient(); services.Configure(configuration.GetSection(nameof(AdministrationOptions))); diff --git a/src/Nocr.TelegramClient.AppServices/TextSubscriptions/Handlers/TextSubscriptionUpdatedHandler.cs b/src/Nocr.TelegramClient.AppServices/TextSubscriptions/Handlers/TextSubscriptionUpdatedHandler.cs new file mode 100644 index 0000000..9ac5d99 --- /dev/null +++ b/src/Nocr.TelegramClient.AppServices/TextSubscriptions/Handlers/TextSubscriptionUpdatedHandler.cs @@ -0,0 +1,88 @@ +using FormatWith; +using Insight.Localizer; +using Insight.TelegramBot.Models; +using Microsoft.Extensions.Logging; +using Nocr.TelegramClient.AppServices.Bots.MessageDispatcher; +using Nocr.TelegramClient.AppServices.Links; +using Nocr.TelegramClient.AppServices.Users; +using Nocr.TextMatcher.Async.Api.Contracts; +using Nocr.Users.Api.Contracts.Users; +using Rebus.Handlers; +using Telegram.Bot.Types.Enums; + +namespace Nocr.TelegramClient.AppServices.TextSubscriptions.Handlers; + +/// +/// Handles updates to previously matched messages (when a Telegram message is edited) +/// +public sealed class TextSubscriptionUpdatedHandler : IHandleMessages +{ + private readonly ILogger _logger; + private readonly ILocalizer _localizer; + private readonly IMessageDispatcherQueue _messageDispatcherQueue; + private readonly IUsersService _usersService; + + public TextSubscriptionUpdatedHandler( + ILogger logger, + ILocalizer localizer, + IMessageDispatcherQueue messageDispatcherQueue, + IUsersService usersService) + { + _logger = logger ?? throw new ArgumentNullException(nameof(logger)); + _localizer = localizer ?? throw new ArgumentNullException(nameof(localizer)); + _messageDispatcherQueue = messageDispatcherQueue ?? throw new ArgumentNullException(nameof(messageDispatcherQueue)); + _usersService = usersService ?? throw new ArgumentNullException(nameof(usersService)); + } + + public async Task Handle(TextSubscriptionUpdated message) + { + Localizer.CurrentCulture = "ru-ru"; + + _logger.LogInformation("Received TextSubscriptionUpdated for message {MessageId}, subscription {SubscriptionId}, version {Version}.", + message.MessageId, message.SubscriptionId, message.Version); + + var user = await _usersService.GetById(message.SubscriptionUserId); + if (user == null) + { + _logger.LogWarning("User [{UserId}] of subscription {SubscriptionId} from message {MessageId} not found.", + message.SubscriptionUserId, + message.SubscriptionId, + message.MessageId); + return; + } + + var identity = user.Identities.FirstOrDefault(x => x.Type == UserIdentityType.TelegramId); + if (identity == null) + { + _logger.LogWarning("There is no identity with type '{IdentityType}' for user '{UserId}'.", + UserIdentityType.TelegramId, user.Id); + return; + } + + var link = new PublicTelegramMessageLink(false, message.ChatUsername, message.MessageId); + var fromUsername = string.IsNullOrWhiteSpace(message.From) ? "anonymous" : message.From; + + var text = _localizer.Get(nameof(TextSubscriptionUpdatedHandler), "Text") + .FormatWith(new + { + SubscriptionRule = message.Rule.TextView(_localizer), + message.Template, + FromUsername = fromUsername, + message.ChatUsername, + MessageText = message.Text, + Link = link.WebLink, + message.Version + }); + + var textMessage = new TextMessage(long.Parse(identity.Identity)) + { + Text = text, + ParseMode = ParseMode.Html + }; + + _messageDispatcherQueue.Enqueue(textMessage); + + _logger.LogInformation("Enqueued update notification for message {MessageId}, subscription {SubscriptionId}, version {Version}.", + message.MessageId, message.SubscriptionId, message.Version); + } +} diff --git a/src/Nocr.TelegramClient.Host/Resources/Handlers/EventHandlers/TextSubscriptionUpdated/TextSubscriptionUpdatedHandler.ru-ru.json b/src/Nocr.TelegramClient.Host/Resources/Handlers/EventHandlers/TextSubscriptionUpdated/TextSubscriptionUpdatedHandler.ru-ru.json new file mode 100644 index 0000000..792f3d6 --- /dev/null +++ b/src/Nocr.TelegramClient.Host/Resources/Handlers/EventHandlers/TextSubscriptionUpdated/TextSubscriptionUpdatedHandler.ru-ru.json @@ -0,0 +1,3 @@ +{ + "Text": "📝 Обновление (версия {Version})\nШаблон: {Template}\nСообщение от {FromUsername} в @{ChatUsername}" +}