/
d.alekseev
/
SmartMenuBot
Обзор
Документация
Войти
/
d.alekseev
/
SmartMenuBot
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
ConsoleBot/TelegramBot/Processors/Implementations/MessageUpdateProcessor.cs
53 строки
2 KB
d.alekseev
feat: списки задач, callback-сценарии и рефактор маршрутизации UpdateHandler
13 фев 2026, 17:30
13 фев 2026, 17:30
a225948
Код
Авторство
О чём код?
using Telegram.Bot; using Telegram.Bot.Types; using SmartMenuBot.TelegramBot.Commands; using SmartMenuBot.TelegramBot.Commands.Implementations; using SmartMenuBot.TelegramBot.Commands.Interfaces; using SmartMenuBot.TelegramBot.Scenarios.Interfaces; namespace SmartMenuBot.TelegramBot.Processors.Implementations { public class MessageUpdateProcessor( IEnumerable<IBotCommand> commands, IScenarioContextRepository contextRepository, IScenarioCoordinator scenarioCoordinator) : Interfaces.IMessageUpdateProcessor { private readonly IReadOnlyList<IBotCommand> _commands = commands.ToList(); private readonly UnknownCommand _unknownCommand = commands.OfType<UnknownCommand>().First(); public async Task ProcessAsync( ITelegramBotClient botClient, Update update, Message message, CancellationToken ct) { if (message.From is null) return; var context = new CommandContext(update, ct); try { var scenarioContext = await contextRepository.GetContext(message.From.Id, ct); var matchingCommand = _commands.FirstOrDefault(c => c.CanExecute(context)); if (matchingCommand is not null && (scenarioContext is null || matchingCommand.ExecutionPolicy == CommandExecutionPolicy.Always)) { await matchingCommand.ExecuteAsync(context); return; } if (scenarioContext is not null) { await scenarioCoordinator.ProcessMessageAsync(botClient, scenarioContext, message, ct); return; } await _unknownCommand.ExecuteAsync(context); } catch (Exception ex) { await botClient.SendMessage(message.Chat.Id, ex.Message, cancellationToken: ct); } } } }