/
d.alekseev
/
SmartMenuBot
Обзор
Документация
Войти
/
d.alekseev
/
SmartMenuBot
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
ConsoleBot/TelegramBot/Commands/Implementations/FindCommand.cs
68 строк
3 KB
d.alekseev
feat: миграция на Telegram.Bot с reply-клавиатурами и меню команд
08 фев 2026, 17:00
08 фев 2026, 17:00
054ee94
Код
Авторство
О чём код?
using Telegram.Bot; using Telegram.Bot.Types; using SmartMenuBot.Core.Entities; using SmartMenuBot.Core.Services.Interfaces; using SmartMenuBot.TelegramBot.Commands.Interfaces; namespace SmartMenuBot.TelegramBot.Commands.Implementations { public class FindCommand(ITelegramBotClient botClient, IUserService userService, IToDoService toDoService) : IBotCommand { public string CommandText => "/find"; 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 existingUser = await userService.GetUserAsync(context.Update.Message!.From!.Id, ct); if (existingUser == null) { await botClient.SendMessage( context.Update.Message.Chat.Id, "\nДля использования этой функции необходимо авторизоваться командой /start", replyMarkup: ReplyKeyboards.PreRegistration, cancellationToken: ct ); return; } var allItems = await toDoService.GetAllByUserIdAsync(existingUser.UserId, ct); if (allItems.Count == 0) { await botClient.SendMessage(context.Update.Message.Chat.Id, $"\nСписок задач пуст", cancellationToken: ct); return; } var searchQuery = context.Update.Message.Text!.Replace(CommandText, "", StringComparison.OrdinalIgnoreCase).Trim(); if (string.IsNullOrEmpty(searchQuery)) { await botClient.SendMessage(context.Update.Message.Chat.Id, $"\nНе задана строка поиска", cancellationToken: ct); return; } var taskList = await toDoService.FindAsync(existingUser, searchQuery, ct); await ShowTasks(context.Update, taskList, ct); } private async Task ShowTasks(Update update, IReadOnlyList<ToDoItem> taskList, CancellationToken ct) { if (taskList.Count == 0) { await botClient.SendMessage(update.Message!.Chat.Id, $"\nСовпадения не найдены", cancellationToken: ct); return; } int i = 0; foreach (var item in taskList) { i++; await botClient.SendMessage(update.Message!.Chat.Id, $"Задача #{i}: \"{item.Name}\" - {item.CreatedAt} - {item.Id}\n", cancellationToken: ct); } } } }