59 lines
2.3 KiB
C#
59 lines
2.3 KiB
C#
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 Telegram.Bot.Types;
|
||
using Telegram.Bot.Types.ReplyMarkups;
|
||
|
||
namespace Nocr.TelegramClient.AppServices.Handlers.CallbackQueries.AddSubscription;
|
||
|
||
public sealed class AddSubscriptionHandler : IMatchingUpdateHandler<AddSubscriptionMatcher>
|
||
{
|
||
private readonly ILogger<AddSubscriptionHandler> _logger;
|
||
private readonly ILocalizer _localizer;
|
||
private readonly IBot _bot;
|
||
private readonly IMessageDispatcherQueue _messageDispatcherQueue;
|
||
|
||
public AddSubscriptionHandler(ILogger<AddSubscriptionHandler> logger, ILocalizer localizer, IBot bot, IMessageDispatcherQueue messageDispatcherQueue)
|
||
{
|
||
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
||
_localizer = localizer ?? throw new ArgumentNullException(nameof(localizer));
|
||
_bot = bot ?? throw new ArgumentNullException(nameof(bot));
|
||
_messageDispatcherQueue = messageDispatcherQueue ?? throw new ArgumentNullException(nameof(messageDispatcherQueue));
|
||
}
|
||
|
||
public async Task Handle(Update update, CancellationToken cancellationToken = default)
|
||
{
|
||
if (update.CallbackQuery?.Message is null)
|
||
{
|
||
_logger.LogError("Не удалось выделить пользовательский колбэк.\n{@Update}", update);
|
||
return;
|
||
}
|
||
|
||
var chatId = update.CallbackQuery.Message.Chat.Id;
|
||
var messageId = update.CallbackQuery.Message.MessageId;
|
||
|
||
var message = new TextMessage(chatId)
|
||
{
|
||
Text = _localizer.Get(nameof(AddSubscriptionHandler), "Text"),
|
||
ReplyMarkup = new InlineKeyboardMarkup(new InlineKeyboardButton(_localizer.Get("buttons", "back"))
|
||
{
|
||
CallbackData = NocrCallbackData.Start().ToString()
|
||
})
|
||
};
|
||
|
||
try
|
||
{
|
||
await _bot.DeleteMessageAsync(chatId, messageId, cancellationToken);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
_logger.LogWarning(ex, "Failed to delete message {MessageId} in chat {ChatId}", messageId, chatId);
|
||
}
|
||
|
||
_messageDispatcherQueue.Enqueue(message);
|
||
}
|
||
} |