telegram-client/src/Nocr.TelegramClient.AppServices/Handlers/BaseHandlers/StartHandlerBase.cs
2025-07-18 19:45:54 +03:00

101 lines
3.5 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.Keyboards;
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.Enums;
using Telegram.Bot.Types.ReplyMarkups;
namespace Nocr.TelegramClient.AppServices.Handlers.BaseHandlers;
public abstract class StartHandlerBase
{
private readonly ILogger _logger;
protected IBot Bot { get; }
protected ILocalizer Localizer { get; }
protected IMessageDispatcherQueue MessageQueue { get; }
public StartHandlerBase(ILogger logger, ILocalizer localizer, IMessageDispatcherQueue messageQueue, IBot bot)
{
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
Bot = bot ?? throw new ArgumentNullException(nameof(bot));
Localizer = localizer ?? throw new ArgumentNullException(nameof(localizer));
MessageQueue = messageQueue ?? throw new ArgumentNullException(nameof(messageQueue));
}
public async Task Handle(Update update, CancellationToken cancellationToken = default)
{
if (update.Message?.From is null || update.CallbackQuery?.Message is null)
{
_logger.LogError("Не удалось выделить пользовательский колбэк.\n{@Update}", update);
return;
}
long telegramId;
switch (update.Type)
{
case UpdateType.Message:
telegramId = update.Message.From.Id;
break;
case UpdateType.CallbackQuery:
telegramId = update.CallbackQuery.From.Id;
break;
default:
throw new ArgumentOutOfRangeException(nameof(update.Type), "Unsupported update type");
}
var message = new TextMessage(telegramId)
{
Text = Localizer.Get(nameof(StartHandlerBase), "Text"),
ParseMode = ParseMode.Html,
ReplyMarkup = GetKeyboard()
};
if (update.Type == UpdateType.CallbackQuery)
{
var messageId = update.CallbackQuery.Message.MessageId;
if (update.CallbackQuery.Message.Type == MessageType.Text)
{
await Bot.EditMessageTextAsync(messageId, message, cancellationToken);
return;
}
var chatId = update.CallbackQuery.Message.Chat.Id;
try
{
await Bot.DeleteMessageAsync(chatId, messageId, cancellationToken);
}
catch (Exception ex)
{
_logger.LogWarning(ex, "Failed to delete message {MessageId} in chat {ChatId}", messageId, chatId);
}
}
MessageQueue.Enqueue(message);
}
private IReplyMarkup GetKeyboard()
{
var markup = new VerticalKeyboardMarkup();
markup.Add(new InlineKeyboardButton(Localizer.Get(nameof(StartHandlerBase), "AddSubscriptionButton"))
{
CallbackData = NocrCallbackData.AddSubscription().ToString()
});
markup.Add(new InlineKeyboardButton(Localizer.Get(nameof(StartHandlerBase), "SubscriptionsButton"))
{
CallbackData = NocrCallbackData.ViewSubscription().ToString()
});
markup.Add(new InlineKeyboardButton(Localizer.Get(nameof(StartHandlerBase), "FeedbackButton"))
{
CallbackData = NocrCallbackData.Feedback().ToString()
});
return markup.InlineKeyboardMarkup;
}
}