From 45ce5da0accff30f7d88de0acf63ec4545fc4f8e Mon Sep 17 00:00:00 2001 From: Kirill Date: Tue, 18 Nov 2025 17:44:03 +0300 Subject: [PATCH 1/3] Changed main menu text --- modules/bot/front/include/constants.hpp | 3 +++ modules/bot/front/src/front.cpp | 3 ++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/modules/bot/front/include/constants.hpp b/modules/bot/front/include/constants.hpp index 212a88f..0f24cae 100644 --- a/modules/bot/front/include/constants.hpp +++ b/modules/bot/front/include/constants.hpp @@ -11,4 +11,7 @@ namespace BotConstants { const std::string FIND_ANIME = "action:find_anime"; const std::string MY_TITLES = "navigation:my_titles"; } + namespace Text { + const std::string MAIN_MENU = "Вас приветствует nyanimedb бот:)\nЧего будем делать?"; + } } diff --git a/modules/bot/front/src/front.cpp b/modules/bot/front/src/front.cpp index d6a0b88..8294cfe 100644 --- a/modules/bot/front/src/front.cpp +++ b/modules/bot/front/src/front.cpp @@ -1,5 +1,6 @@ #include "front.hpp" #include "KeyboardFactory.hpp" +#include "constants.hpp" AnimeBot::AnimeBot(const std::string& token) : bot(token) { setupHandlers(); @@ -13,7 +14,7 @@ void AnimeBot::setupHandlers() { void AnimeBot::sendMainMenu(int64_t chatId) { auto keyboard = KeyboardFactory::createMainMenu(); - bot.getApi().sendMessage(chatId, "Главное меню", nullptr, nullptr, keyboard); + bot.getApi().sendMessage(chatId, BotConstants::Text::MAIN_MENU, nullptr, nullptr, keyboard); } TgBot::Bot& AnimeBot::getBot() { From 45a1df4cbbc1067e83de6a5dbe22ab69694761d9 Mon Sep 17 00:00:00 2001 From: Kirill Date: Tue, 25 Nov 2025 19:45:44 +0300 Subject: [PATCH 2/3] Added MyTitles page passing --- modules/bot/front/CMakeLists.txt | 3 +- modules/bot/front/include/KeyboardFactory.hpp | 4 ++ modules/bot/front/include/constants.hpp | 4 ++ modules/bot/front/include/handlers.hpp | 13 ++++++ modules/bot/front/include/structs.hpp | 8 ++++ modules/bot/front/src/KeyboardFactory.cpp | 43 +++++++++++++++++++ modules/bot/front/src/front.cpp | 21 +++++++++ modules/bot/front/src/handlers.cpp | 15 +++++++ 8 files changed, 110 insertions(+), 1 deletion(-) create mode 100644 modules/bot/front/include/handlers.hpp create mode 100644 modules/bot/front/include/structs.hpp create mode 100644 modules/bot/front/src/handlers.cpp diff --git a/modules/bot/front/CMakeLists.txt b/modules/bot/front/CMakeLists.txt index 206c47f..19ee60a 100644 --- a/modules/bot/front/CMakeLists.txt +++ b/modules/bot/front/CMakeLists.txt @@ -2,10 +2,11 @@ cmake_minimum_required(VERSION 3.10.2) project(AnimeBot) file(GLOB SOURCES "src/*.cpp") -set(CMAKE_CXX_STANDARD 14) +set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall") set(Boost_USE_MULTITHREADED ON) +set(CMAKE_BUILD_TYPE Debug) find_package(Threads REQUIRED) find_package(OpenSSL REQUIRED) diff --git a/modules/bot/front/include/KeyboardFactory.hpp b/modules/bot/front/include/KeyboardFactory.hpp index 9e15982..0d3d5f0 100644 --- a/modules/bot/front/include/KeyboardFactory.hpp +++ b/modules/bot/front/include/KeyboardFactory.hpp @@ -1,7 +1,11 @@ #include +#include "structs.hpp" class KeyboardFactory { public: /// Create keyboard for main menu static TgBot::InlineKeyboardMarkup::Ptr createMainMenu(); + + /// Create keyboard for My_Titles + static TgBot::InlineKeyboardMarkup::Ptr createMyTitles(std::vector titles); }; diff --git a/modules/bot/front/include/constants.hpp b/modules/bot/front/include/constants.hpp index 0f24cae..4d3d5e9 100644 --- a/modules/bot/front/include/constants.hpp +++ b/modules/bot/front/include/constants.hpp @@ -6,10 +6,14 @@ namespace BotConstants { namespace Button { const std::string FIND_ANIME = "Найти аниме"; const std::string MY_TITLES = "Мои тайтлы"; + const std::string PREV = "<<Назад"; + 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 = ""; } 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 new file mode 100644 index 0000000..eb9d2ec --- /dev/null +++ b/modules/bot/front/include/handlers.hpp @@ -0,0 +1,13 @@ +#include <tgbot/tgbot.h> +#include <string> +#include <structs.hpp> + +struct HandlerResult { + std::string message; + TgBot::InlineKeyboardMarkup::Ptr keyboard; +}; + +class BotHandlers { +public: + static HandlerResult MyTitles(int64_t userId); +}; diff --git a/modules/bot/front/include/structs.hpp b/modules/bot/front/include/structs.hpp new file mode 100644 index 0000000..57f5e4a --- /dev/null +++ b/modules/bot/front/include/structs.hpp @@ -0,0 +1,8 @@ +#pragma once + +struct Title { + int64_t id; + std::string name; + std::string description; + int64_t num; +}; diff --git a/modules/bot/front/src/KeyboardFactory.cpp b/modules/bot/front/src/KeyboardFactory.cpp index 1bd2cab..bc1bdd0 100644 --- a/modules/bot/front/src/KeyboardFactory.cpp +++ b/modules/bot/front/src/KeyboardFactory.cpp @@ -13,3 +13,46 @@ TgBot::InlineKeyboardMarkup::Ptr KeyboardFactory::createMainMenu() { keyboard->inlineKeyboard = {{button1, button2}}; return keyboard; } + +TgBot::InlineKeyboardMarkup::Ptr KeyboardFactory::createMyTitles(std::vector<Title> titles) { + auto keyboard = std::make_shared<TgBot::InlineKeyboardMarkup>(); + std::vector<TgBot::InlineKeyboardButton::Ptr> row; + std::vector<std::vector<TgBot::InlineKeyboardButton::Ptr>> layout; + + int counter = 0; + for(Title& title : titles) { + if(counter >= 6) { + break; + } + auto button = std::make_shared<TgBot::InlineKeyboardButton>(); + button->text = std::to_string(title.num) + " " + title.name; + button->callbackData = "title:" + std::to_string(title.num); + row.push_back(button); + counter++; + if(counter % 2 == 0) { + layout.push_back(row); + row.clear(); + } + } + + // TODO: Додумать логику, когда пришло 6 записей в конце + if(counter % 2 == 1) { + auto button = std::make_shared<TgBot::InlineKeyboardButton>(); + button->text = BotConstants::Button::PREV; + button->callbackData = BotConstants::Callback::LIST_PREV + std::to_string(titles[0].num); + layout[counter / 2].push_back(button); + } + else { + auto button_prev = std::make_shared<TgBot::InlineKeyboardButton>(); + button_prev->text = BotConstants::Button::PREV; + button_prev->callbackData = BotConstants::Callback::LIST_PREV + std::to_string(titles[0].num); + auto button_next = std::make_shared<TgBot::InlineKeyboardButton>(); + button_next->text = BotConstants::Button::NEXT; + button_next->callbackData = BotConstants::Callback::LIST_NEXT + std::to_string(titles[5].num); + layout.push_back({button_prev, button_next}); + } + + keyboard->inlineKeyboard = layout; + + return keyboard; +} diff --git a/modules/bot/front/src/front.cpp b/modules/bot/front/src/front.cpp index 8294cfe..85fe2f5 100644 --- a/modules/bot/front/src/front.cpp +++ b/modules/bot/front/src/front.cpp @@ -1,6 +1,7 @@ #include "front.hpp" #include "KeyboardFactory.hpp" #include "constants.hpp" +#include "handlers.hpp" AnimeBot::AnimeBot(const std::string& token) : bot(token) { setupHandlers(); @@ -10,6 +11,20 @@ void AnimeBot::setupHandlers() { bot.getEvents().onCommand("start", [this](TgBot::Message::Ptr message) { sendMainMenu(message->chat->id); }); + + auto [text, kb] = BotHandlers::MyTitles(321); + + bot.getEvents().onCallbackQuery([text, kb, this](TgBot::CallbackQuery::Ptr query) { + bot.getApi().editMessageText( + text, + query->message->chat->id, + query->message->messageId, + "", + "", + nullptr, + kb + ); + }); } void AnimeBot::sendMainMenu(int64_t chatId) { @@ -20,3 +35,9 @@ void AnimeBot::sendMainMenu(int64_t chatId) { TgBot::Bot& AnimeBot::getBot() { return bot; } + +/* +void AnimeBot::showMyTitles() { + +} +*/ diff --git a/modules/bot/front/src/handlers.cpp b/modules/bot/front/src/handlers.cpp new file mode 100644 index 0000000..0b887c1 --- /dev/null +++ b/modules/bot/front/src/handlers.cpp @@ -0,0 +1,15 @@ +#include "handlers.hpp" +#include "KeyboardFactory.hpp" +#include "structs.hpp" + +/// В угоду потокобезопасности создаем новый экземпляр TgBot::Api +HandlerResult BotHandlers::MyTitles(int64_t userId) { + // Здесь должен происходить запрос на сервер + std::vector<Title> titles = {{123, "Школа мертвяков", "", 1}, {321, "KissXsis", "", 2}}; + + struct HandlerResult result; + result.keyboard = KeyboardFactory::createMyTitles(titles); + result.message = "1. Школа мертвяков\n2. KissXsis\n"; + + return result; +} From ea29fa79f005884f63241742ebbd391268d03f57 Mon Sep 17 00:00:00 2001 From: Kirill <mymail@mail.com> Date: Tue, 25 Nov 2025 22:05:12 +0300 Subject: [PATCH 3/3] 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 <string> #include <structs.hpp> +/// @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<Title> 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; +}