telegram-client/src/Nocr.TelegramClient.AppServices/Handlers/CallbackQueries/ActivateSubscription/ActivateSubscriptionHandler.cs
Sergey Nazarov 5e99e802ec nazarovsa/subscription_commands (#6)
Reviewed-on: #6
Co-authored-by: Sergey Nazarov <insight.appdev@gmail.com>
Co-committed-by: Sergey Nazarov <insight.appdev@gmail.com>
2024-04-16 13:03:31 +00:00

61 lines
2.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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<ActivateSubscriptionMatcher>
{
public ActivateSubscriptionHandler(ILogger<ActivateSubscriptionHandler> 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")
});
}
}