diff --git a/modules/bot/front/include/KeyboardFactory.hpp b/modules/bot/front/include/KeyboardFactory.hpp index 0d3d5f0..71e5d10 100644 --- a/modules/bot/front/include/KeyboardFactory.hpp +++ b/modules/bot/front/include/KeyboardFactory.hpp @@ -8,4 +8,7 @@ public: /// Create keyboard for My_Titles static TgBot::InlineKeyboardMarkup::Ptr createMyTitles(std::vector titles); + + /// Create keyboard for sendError + static TgBot::InlineKeyboardMarkup::Ptr createError(const std::string& errorCallback); }; diff --git a/modules/bot/front/include/constants.hpp b/modules/bot/front/include/constants.hpp index 8fee9d5..516bc54 100644 --- a/modules/bot/front/include/constants.hpp +++ b/modules/bot/front/include/constants.hpp @@ -8,6 +8,7 @@ namespace BotConstants { const int64_t DISP_REVIEW_NUM = 4; // Количество ревью, отображаемых на страничке namespace Button { + const std::string TO_MAIN_MENU = "Главное меню"; const std::string FIND_ANIME = "Найти аниме"; const std::string MY_TITLES = "Мои тайтлы"; const std::string PREV = "<<Назад"; @@ -24,11 +25,15 @@ namespace BotConstants { const std::string WANT = STATUS + "want"; const std::string THROWN = STATUS + "thrown"; const std::string NAVIGATION = "navigation:"; + const std::string MAIN_MENU = NAVIGATION + "main_menu"; 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 NAV_BACK = NAVIGATION + "back"; // Возврат по стеку состояний const std::string CHOICE = "choice:"; + const std::string ERROR = "error:"; + const std::string ERROR_NAVIGATION = ERROR + NAVIGATION; + const std::string ERROR_AUTH = ERROR + "auth"; } 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 572984d..60ac79d 100644 --- a/modules/bot/front/include/handlers.hpp +++ b/modules/bot/front/include/handlers.hpp @@ -59,7 +59,9 @@ private: TgBot::Api botApi; std::unordered_map<int64_t, UserContext> userContexts; - void handleNavigation(TgBot::CallbackQuery::Ptr query); + void handleNavigation(TgBot::CallbackQuery::Ptr query, UserContext& ctx); + + void handleError(TgBot::CallbackQuery::Ptr query, UserContext& ctx); void processCallbackImpl(TgBot::CallbackQuery::Ptr query); diff --git a/modules/bot/front/src/KeyboardFactory.cpp b/modules/bot/front/src/KeyboardFactory.cpp index cf2500c..e3b02b4 100644 --- a/modules/bot/front/src/KeyboardFactory.cpp +++ b/modules/bot/front/src/KeyboardFactory.cpp @@ -67,3 +67,13 @@ TgBot::InlineKeyboardMarkup::Ptr KeyboardFactory::createMyTitles(std::vector<Tit return keyboard; } + +TgBot::InlineKeyboardMarkup::Ptr KeyboardFactory::createError(const std::string& errorCallback) { + auto keyboard = std::make_shared<TgBot::InlineKeyboardMarkup>(); + TgBot::InlineKeyboardButton::Ptr button(new TgBot::InlineKeyboardButton); + button->text = BotConstants::Button::TO_MAIN_MENU; + button->callbackData = errorCallback; + + keyboard->inlineKeyboard = {{button}}; + return keyboard; +} diff --git a/modules/bot/front/src/handlers.cpp b/modules/bot/front/src/handlers.cpp index 5bdef9d..04b2e8b 100644 --- a/modules/bot/front/src/handlers.cpp +++ b/modules/bot/front/src/handlers.cpp @@ -38,16 +38,6 @@ void BotHandlers::handleMessage(TgBot::Message::Ptr message) { void BotHandlers::processCallbackImpl(TgBot::CallbackQuery::Ptr query) { const std::string& data = query->data; - - if (data.starts_with(BotConstants::Callback::NAVIGATION)) { - handleNavigation(query); - } - else { - botApi.sendMessage(query->message->chat->id, BotConstants::Text::SAD_ERROR, nullptr, nullptr); - } -} - -void BotHandlers::handleNavigation(TgBot::CallbackQuery::Ptr query) { int64_t userId = query->from->id; auto it = userContexts.find(userId); if (it == userContexts.end()) { @@ -58,6 +48,19 @@ void BotHandlers::handleNavigation(TgBot::CallbackQuery::Ptr query) { } UserContext& ctx = it->second; + + if (data.starts_with(BotConstants::Callback::NAVIGATION)) { + handleNavigation(query, ctx); + } + else if (data.starts_with(BotConstants::Callback::ERROR)) { + handleError(query, ctx); + } + else { + botApi.sendMessage(query->message->chat->id, BotConstants::Text::SAD_ERROR, nullptr, nullptr); + } +} + +void BotHandlers::handleNavigation(TgBot::CallbackQuery::Ptr query, UserContext& ctx) { const auto& current = ctx.history.back(); // текущий экран const std::string& data = query->data; @@ -187,7 +190,13 @@ HandlerResult BotHandlers::showMainMenu() { void BotHandlers::sendError(TgBot::CallbackQuery::Ptr query, const std::string& errText) { //TODO: посылать сообщение с кнопкой возврата в главное меню - auto keyboard = nullptr; + TgBot::InlineKeyboardMarkup::Ptr keyboard; + if (errText == BotConstants::Text::SAD_ERROR) { + keyboard = KeyboardFactory::createError(BotConstants::Callback::ERROR_NAVIGATION); + } + else if (errText == BotConstants::Text::AUTH_ERROR) { + keyboard = nullptr; //KeyboardFactory::createError(BotConstants::Callback::ERROR_AUTH); + } editMessage(query, {errText, keyboard}); } @@ -221,8 +230,6 @@ std::optional<NavigationStep> BotHandlers::computeNextStep( return NavigationStep{UserState::VIEWING_REVIEW, reviewId}; } break; - - // Добавляйте другие переходы по мере роста функционала */ default: break; @@ -233,4 +240,20 @@ std::optional<NavigationStep> BotHandlers::computeNextStep( void BotHandlers::createInitContext(int64_t chatId) { NavigationStep init = {UserState::MAIN_MENU, BotConstants::NULL_PAYLOAD}; userContexts[chatId] = {chatId, {init}}; +} + +void BotHandlers::handleError(TgBot::CallbackQuery::Ptr query, UserContext& ctx) { + const std::string& data = query->data; + + if(data == BotConstants::Callback::ERROR_NAVIGATION) { + ctx.history.clear(); + ctx.history.push_back({UserState::MAIN_MENU, 0}); + auto result = showMainMenu(); + editMessage(query, result); + } + else if(data == BotConstants::Callback::ERROR_AUTH) { + // TODO: продумать логику + HandlerResult result = {BotConstants::Text::AUTH_ERROR, nullptr}; + editMessage(query, result); + } } \ No newline at end of file