/
d.alekseev
/
SmartMenuBot
Обзор
Документация
Войти
/
d.alekseev
/
SmartMenuBot
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
ConsoleBot/TelegramBot/Processors/Implementations/CallbackUpdateProcessor.cs
67 строк
3 KB
d.alekseev
feat: списки задач, callback-сценарии и рефактор маршрутизации UpdateHandler
13 фев 2026, 17:30
13 фев 2026, 17:30
a225948
Код
Авторство
О чём код?
using Telegram.Bot; using Telegram.Bot.Types; using SmartMenuBot.Core.Services.Interfaces; using SmartMenuBot.TelegramBot.Callbacks; using SmartMenuBot.TelegramBot.Callbacks.Interfaces; using SmartMenuBot.TelegramBot.Dto; using SmartMenuBot.TelegramBot.Scenarios.Interfaces; namespace SmartMenuBot.TelegramBot.Processors.Implementations { public class CallbackUpdateProcessor( IUserService userService, IEnumerable<ICallbackCommand> callbackCommands, IScenarioContextRepository contextRepository, IScenarioCoordinator scenarioCoordinator) : Interfaces.ICallbackUpdateProcessor { private readonly IReadOnlyList<ICallbackCommand> _callbackCommands = callbackCommands.ToList(); public async Task ProcessAsync( ITelegramBotClient botClient, Update update, CallbackQuery callbackQuery, CancellationToken ct) { try { var user = await userService.GetUserAsync(callbackQuery.From.Id, ct); if (user is null) return; var scenarioContext = await contextRepository.GetContext(callbackQuery.From.Id, ct); if (scenarioContext is not null) { var handledByScenario = await scenarioCoordinator.ProcessCallbackAsync( botClient, scenarioContext, callbackQuery, ct); if (handledByScenario) return; } var dto = CallbackDto.FromString(callbackQuery.Data ?? string.Empty); var context = new CallbackCommandContext(update, callbackQuery, dto, ct); var callbackCommand = _callbackCommands.FirstOrDefault(c => c.CanExecute(context)); if (callbackCommand is not null) await callbackCommand.ExecuteAsync(context); } catch (Exception ex) { if (callbackQuery.Message is not null) await botClient.SendMessage(callbackQuery.Message.Chat.Id, ex.Message, cancellationToken: ct); } finally { try { await botClient.AnswerCallbackQuery(callbackQuery.Id, cancellationToken: ct); } catch { // Callback already answered or outdated. } } } } }