using Insight.Localizer; using Insight.TelegramBot; using Insight.TelegramBot.Handling.Handlers; using Insight.TelegramBot.Models; using Microsoft.Extensions.Logging; using Nocr.TelegramClient.AppServices.Bots; using Nocr.TelegramClient.AppServices.Bots.MessageDispatcher; using Nocr.TelegramClient.AppServices.Handlers.BaseHandlers; using Nocr.TextMatcher.Api.Contracts.TextMatches; using Telegram.Bot.Types; namespace Nocr.TelegramClient.AppServices.Handlers.CallbackQueries.ActivateSubscription; public class ActivateSubscriptionHandler : ViewSubscriptionHandlerBase, IMatchingUpdateHandler { public ActivateSubscriptionHandler(ILogger logger, ILocalizer localizer, IMessageDispatcherQueue messageQueue, IBot bot, ITextSubscriptionsController subscriptionsController) : base(logger, localizer, messageQueue, bot, subscriptionsController) { } public async Task Handle(Update update, CancellationToken cancellationToken = default) { 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)) { Logger.LogWarning( "Не удалось извлечь идентификатор подписки. CallbackData: {@CallbackData}, ChatId: {ChatId}, MessageId {MessageId}", callbackData, from, update.CallbackQuery.Message.Chat.Id); SendErrorMessage(from); return; } try { await SubscriptionsController.Activate(subscriptionId, cancellationToken); } catch (Exception ex) { Logger.LogError(ex, "Не удалось деактивировать подписку"); SendErrorMessage(from); throw; } await EditSubscriptionMessage(from, update.CallbackQuery.Message.MessageId, subscriptionId, CancellationToken.None); } private void SendErrorMessage(long from) { MessageQueue.Enqueue(new TextMessage(from) { Text = Localizer.Get(nameof(ActivateSubscriptionHandler), "FailedToActivateSubscription") }); } }