Add handler for TextSubscriptionUpdated events
Implements notification of message updates when Telegram messages are edited: - New TextSubscriptionUpdatedHandler to process update events - Sends new notification with version number to inform users of updates - Localization support (ru-ru) with emoji indicator for updates - Registered in DI container for Rebus message handling 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
parent
2692797dbc
commit
dd32edd403
@ -24,6 +24,7 @@ public static class ServiceCollectionExtensions
|
|||||||
|
|
||||||
// Add registrations here
|
// Add registrations here
|
||||||
services.AddRebusHandler<TextSubscriptionMatchedHandler>();
|
services.AddRebusHandler<TextSubscriptionMatchedHandler>();
|
||||||
|
services.AddRebusHandler<TextSubscriptionUpdatedHandler>();
|
||||||
services.AddHttpClient();
|
services.AddHttpClient();
|
||||||
|
|
||||||
services.Configure<AdministrationOptions>(configuration.GetSection(nameof(AdministrationOptions)));
|
services.Configure<AdministrationOptions>(configuration.GetSection(nameof(AdministrationOptions)));
|
||||||
|
|||||||
@ -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;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Handles updates to previously matched messages (when a Telegram message is edited)
|
||||||
|
/// </summary>
|
||||||
|
public sealed class TextSubscriptionUpdatedHandler : IHandleMessages<TextSubscriptionUpdated>
|
||||||
|
{
|
||||||
|
private readonly ILogger<TextSubscriptionUpdatedHandler> _logger;
|
||||||
|
private readonly ILocalizer _localizer;
|
||||||
|
private readonly IMessageDispatcherQueue _messageDispatcherQueue;
|
||||||
|
private readonly IUsersService _usersService;
|
||||||
|
|
||||||
|
public TextSubscriptionUpdatedHandler(
|
||||||
|
ILogger<TextSubscriptionUpdatedHandler> 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"Text": "📝 Обновление (версия {Version})\nШаблон: <i>{Template}</i>\n<a href='{Link}'>Сообщение</a> от {FromUsername} в @{ChatUsername}"
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user