/
d.alekseev
/
SmartMenuBot
Обзор
Документация
Войти
/
d.alekseev
/
SmartMenuBot
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
ConsoleBot/TelegramBot/Commands/Implementations/AddTaskCommand.cs
54 строки
2 KB
d.alekseev
feat: сценарии AddTask с сохранением состояния и поддержкой срока задачи
09 фев 2026, 21:39
09 фев 2026, 21:39
ed2e630
Код
Авторство
О чём код?
using Telegram.Bot; using SmartMenuBot.Core.Services.Interfaces; using SmartMenuBot.TelegramBot.Commands.Interfaces; using SmartMenuBot.TelegramBot.Scenarios.Interfaces; using SmartMenuBot.TelegramBot.Scenarios.Models; using SmartMenuBot.TelegramBot.Scenarios.Implementations; namespace SmartMenuBot.TelegramBot.Commands.Implementations { public class AddTaskCommand( ITelegramBotClient botClient, IUserService userService, IScenarioContextRepository contextRepository) : IBotCommand { public string CommandText => "/addtask"; public bool CanExecute(CommandContext context) { string? messageText = context.Update.Message?.Text; return messageText != null && messageText.StartsWith(CommandText, StringComparison.OrdinalIgnoreCase); } public async Task ExecuteAsync(CommandContext context) { var ct = context.CancellationToken; var message = context.Update.Message!; var user = await userService.GetUserAsync(message.From!.Id, ct); if (user is null) { await botClient.SendMessage( message.Chat.Id, "Для использования этой функции необходимо авторизоваться командой /start", replyMarkup: ReplyKeyboards.PreRegistration, cancellationToken: ct); return; } // Инициализация старта сценария на уровне команды сохраняет единый вход через Command pattern. var scenarioContext = new ScenarioContext(ScenarioType.AddTask) { CurrentStep = AddTaskScenario.NameStep }; scenarioContext.Data[AddTaskScenario.UserDataKey] = user; await contextRepository.SetContext(message.From.Id, scenarioContext, ct); await botClient.SendMessage( message.Chat.Id, "Введите название задачи:", replyMarkup: ReplyKeyboards.ScenarioCancel, cancellationToken: ct); } } }