/
d.alekseev
/
SmartMenuBot
Обзор
Документация
Войти
/
d.alekseev
/
SmartMenuBot
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
ConsoleBot/TelegramBot/Callbacks/Implementations/DeleteListCallbackCommand.cs
88 строк
4 KB
d.alekseev
feat: списки задач, callback-сценарии и рефактор маршрутизации UpdateHandler
13 фев 2026, 17:30
13 фев 2026, 17:30
a225948
Код
Авторство
О чём код?
using Telegram.Bot; using Telegram.Bot.Types.ReplyMarkups; using SmartMenuBot.Core.Entities; using SmartMenuBot.Core.Services.Interfaces; using SmartMenuBot.TelegramBot.Callbacks; using SmartMenuBot.TelegramBot.Callbacks.Interfaces; using SmartMenuBot.TelegramBot.Dto; using SmartMenuBot.TelegramBot.Scenarios.Implementations; using SmartMenuBot.TelegramBot.Scenarios.Interfaces; using SmartMenuBot.TelegramBot.Scenarios.Models; namespace SmartMenuBot.TelegramBot.Callbacks.Implementations { public class DeleteListCallbackCommand( ITelegramBotClient botClient, IUserService userService, IToDoListService toDoListService, IScenarioContextRepository contextRepository) : ICallbackCommand { public string Action => "deletelist"; public bool CanExecute(CallbackCommandContext context) { return string.Equals(context.Dto.Action, Action, StringComparison.OrdinalIgnoreCase) && string.Equals(context.Query.Data, Action, StringComparison.OrdinalIgnoreCase); } public async Task ExecuteAsync(CallbackCommandContext context) { if (context.Query.Message is null) return; var ct = context.CancellationToken; var user = await userService.GetUserAsync(context.Query.From.Id, ct); if (user is null) { await botClient.SendMessage( context.Query.Message.Chat.Id, "Для использования этой функции необходимо авторизоваться командой /start", replyMarkup: ReplyKeyboards.PreRegistration, cancellationToken: ct); return; } var lists = await toDoListService.GetUserListsAsync(user.UserId, ct); if (lists.Count == 0) { await botClient.SendMessage( context.Query.Message.Chat.Id, "У вас нет списков для удаления.", replyMarkup: ReplyKeyboards.PostRegistration, cancellationToken: ct); return; } // Инициализация старта сценария на уровне callback-команды сохраняет единый вход через callback Command pattern. var scenarioContext = new ScenarioContext(ScenarioType.DeleteList) { CurrentStep = DeleteListScenario.ApproveStep }; scenarioContext.Data[DeleteListScenario.UserDataKey] = user; await contextRepository.SetContext(context.Query.From.Id, scenarioContext, ct); await botClient.SendMessage( context.Query.Message.Chat.Id, "Выберите список для удаления:", replyMarkup: BuildDeleteListSelectionKeyboard(lists), cancellationToken: ct); } private static InlineKeyboardMarkup BuildDeleteListSelectionKeyboard(IReadOnlyList<ToDoList> lists) { var rows = new List<List<InlineKeyboardButton>>(); foreach (var list in lists) { rows.Add(new List<InlineKeyboardButton> { InlineKeyboardButton.WithCallbackData( list.Name, new ToDoListCallbackDto { Action = "deletelist", ToDoListId = list.Id }.ToString()) }); } return new InlineKeyboardMarkup(rows); } } }