From ea29fa79f005884f63241742ebbd391268d03f57 Mon Sep 17 00:00:00 2001 From: Kirill Date: Tue, 25 Nov 2025 22:05:12 +0300 Subject: [PATCH] getting ready to refactor the handlers structure --- modules/bot/front/include/constants.hpp | 20 +++++++++++++++----- modules/bot/front/include/handlers.hpp | 13 ++++++++++++- modules/bot/front/src/front.cpp | 7 ++++--- modules/bot/front/src/handlers.cpp | 15 ++++++++++++++- 4 files changed, 45 insertions(+), 10 deletions(-) diff --git a/modules/bot/front/include/constants.hpp b/modules/bot/front/include/constants.hpp index 4d3d5e9..595a369 100644 --- a/modules/bot/front/include/constants.hpp +++ b/modules/bot/front/include/constants.hpp @@ -7,13 +7,23 @@ namespace BotConstants { const std::string FIND_ANIME = "Найти аниме"; const std::string MY_TITLES = "Мои тайтлы"; const std::string PREV = "<<Назад"; - const std::string NEXT = "Следующий>>"; + const std::string NEXT = "Дальше>>"; } namespace Callback { - const std::string FIND_ANIME = "action:find_anime"; - const std::string MY_TITLES = "navigation:my_titles"; - const std::string LIST_PREV = ""; - const std::string LIST_NEXT = ""; + const std::string ACTION = "action:"; + const std::string FIND_ANIME = ACTION + "find_anime"; + const std::string ADD_REVIEW = ACTION + "add_review"; + const std::string ADD_STATUS = ACTION + "add_status"; + const std::string STATUS = "status:"; + const std::string WATCHING = STATUS + "watching"; + const std::string SEEN = STATUS + "seen"; + const std::string WANT = STATUS + "want"; + const std::string THROWN = STATUS + "thrown"; + const std::string NAVIGATION = "navigation:"; + const std::string MY_TITLES = NAVIGATION + "my_titles"; + const std::string LIST_PREV = NAVIGATION + "prev"; + const std::string LIST_NEXT = NAVIGATION + "next"; + const std::string CHOICE = "choice:"; } namespace Text { const std::string MAIN_MENU = "Вас приветствует nyanimedb бот:)\nЧего будем делать?"; diff --git a/modules/bot/front/include/handlers.hpp b/modules/bot/front/include/handlers.hpp index eb9d2ec..27aee3a 100644 --- a/modules/bot/front/include/handlers.hpp +++ b/modules/bot/front/include/handlers.hpp @@ -2,6 +2,7 @@ #include #include +/// @brief Структура возвращаемого значения класса BotHandlers для изменения текущего сообщения struct HandlerResult { std::string message; TgBot::InlineKeyboardMarkup::Ptr keyboard; @@ -9,5 +10,15 @@ struct HandlerResult { class BotHandlers { public: - static HandlerResult MyTitles(int64_t userId); + void handleCallback(const TgBot::CallbackQuery::Ptr query); + +private: + TgBot::Api botApi; + + void handleNavigation(const TgBot::CallbackQuery::Ptr query); + + /// @brief Получить очередную страницу тайтлов из списка пользователя + /// @param userId Идентификатор пользователя + /// @return HandlerResult + static HandlerResult returnMyTitles(int64_t userId); }; diff --git a/modules/bot/front/src/front.cpp b/modules/bot/front/src/front.cpp index 85fe2f5..e6d3034 100644 --- a/modules/bot/front/src/front.cpp +++ b/modules/bot/front/src/front.cpp @@ -12,10 +12,11 @@ void AnimeBot::setupHandlers() { sendMainMenu(message->chat->id); }); - auto [text, kb] = BotHandlers::MyTitles(321); + auto [text, kb] = BotHandlers::returnMyTitles(321); - bot.getEvents().onCallbackQuery([text, kb, this](TgBot::CallbackQuery::Ptr query) { - bot.getApi().editMessageText( + auto cp_api = bot.getApi(); + bot.getEvents().onCallbackQuery([text, kb, cp_api](TgBot::CallbackQuery::Ptr query) { + cp_api.editMessageText( text, query->message->chat->id, query->message->messageId, diff --git a/modules/bot/front/src/handlers.cpp b/modules/bot/front/src/handlers.cpp index 0b887c1..cbd90da 100644 --- a/modules/bot/front/src/handlers.cpp +++ b/modules/bot/front/src/handlers.cpp @@ -1,9 +1,18 @@ #include "handlers.hpp" #include "KeyboardFactory.hpp" #include "structs.hpp" +#include "constants.hpp" + +void BotHandlers::handleCallback(const TgBot::CallbackQuery::Ptr query) { + std::string data = query -> data; + + if (data.starts_with(BotConstants::Callback::NAVIGATION)) { + handleNavigation(query); + } +} /// В угоду потокобезопасности создаем новый экземпляр TgBot::Api -HandlerResult BotHandlers::MyTitles(int64_t userId) { +HandlerResult BotHandlers::returnMyTitles(int64_t userId) { // Здесь должен происходить запрос на сервер std::vector titles = {{123, "Школа мертвяков", "", 1}, {321, "KissXsis", "", 2}}; @@ -13,3 +22,7 @@ HandlerResult BotHandlers::MyTitles(int64_t userId) { return result; } + +void BotHandlers::handleNavigation(const TgBot::CallbackQuery::Ptr query) { + return; +}