Compare commits
No commits in common. "3d8abc3f0c20b8df3e2fb4db3f5b70056bd354d2" and "ea29fa79f005884f63241742ebbd391268d03f57" have entirely different histories.
3d8abc3f0c
...
ea29fa79f0
7 changed files with 45 additions and 94 deletions
3
modules/bot/front/.gitignore
vendored
3
modules/bot/front/.gitignore
vendored
|
|
@ -1,4 +1 @@
|
||||||
build/
|
build/
|
||||||
out/
|
|
||||||
.vscode
|
|
||||||
api/generated-client
|
|
||||||
|
|
@ -11,22 +11,21 @@ namespace BotConstants {
|
||||||
}
|
}
|
||||||
namespace Callback {
|
namespace Callback {
|
||||||
const std::string ACTION = "action:";
|
const std::string ACTION = "action:";
|
||||||
const std::string FIND_ANIME = ACTION + "find_anime";
|
const std::string FIND_ANIME = ACTION + "find_anime";
|
||||||
const std::string ADD_REVIEW = ACTION + "add_review";
|
const std::string ADD_REVIEW = ACTION + "add_review";
|
||||||
const std::string ADD_STATUS = ACTION + "add_status";
|
const std::string ADD_STATUS = ACTION + "add_status";
|
||||||
const std::string STATUS = "status:";
|
const std::string STATUS = "status:";
|
||||||
const std::string WATCHING = STATUS + "watching";
|
const std::string WATCHING = STATUS + "watching";
|
||||||
const std::string SEEN = STATUS + "seen";
|
const std::string SEEN = STATUS + "seen";
|
||||||
const std::string WANT = STATUS + "want";
|
const std::string WANT = STATUS + "want";
|
||||||
const std::string THROWN = STATUS + "thrown";
|
const std::string THROWN = STATUS + "thrown";
|
||||||
const std::string NAVIGATION = "navigation:";
|
const std::string NAVIGATION = "navigation:";
|
||||||
const std::string MY_TITLES = NAVIGATION + "my_titles";
|
const std::string MY_TITLES = NAVIGATION + "my_titles";
|
||||||
const std::string LIST_PREV = NAVIGATION + "prev";
|
const std::string LIST_PREV = NAVIGATION + "prev";
|
||||||
const std::string LIST_NEXT = NAVIGATION + "next";
|
const std::string LIST_NEXT = NAVIGATION + "next";
|
||||||
const std::string CHOICE = "choice:";
|
const std::string CHOICE = "choice:";
|
||||||
}
|
}
|
||||||
namespace Text {
|
namespace Text {
|
||||||
const std::string MAIN_MENU = "Вас приветствует nyanimedb бот:)\nЧего будем делать?";
|
const std::string MAIN_MENU = "Вас приветствует nyanimedb бот:)\nЧего будем делать?";
|
||||||
const std::string SAD_ERROR = "У нас что-то случилось:(\nМы обязательно скоро исправимся";
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,12 +5,11 @@
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#include <tgbot/tgbot.h>
|
#include <tgbot/tgbot.h>
|
||||||
#include "handlers.hpp"
|
|
||||||
|
|
||||||
class AnimeBot {
|
class AnimeBot {
|
||||||
private:
|
private:
|
||||||
|
std::string token;
|
||||||
TgBot::Bot bot;
|
TgBot::Bot bot;
|
||||||
BotHandlers handler;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
/// Init Bot
|
/// Init Bot
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,3 @@
|
||||||
#pragma once
|
|
||||||
#include <tgbot/tgbot.h>
|
#include <tgbot/tgbot.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <structs.hpp>
|
#include <structs.hpp>
|
||||||
|
|
@ -10,28 +9,13 @@ struct HandlerResult {
|
||||||
};
|
};
|
||||||
|
|
||||||
class BotHandlers {
|
class BotHandlers {
|
||||||
public:
|
public:
|
||||||
BotHandlers(TgBot::Api api) : botApi(api) {;}
|
void handleCallback(const TgBot::CallbackQuery::Ptr query);
|
||||||
|
|
||||||
/// @brief Обработка callback'ов из кнопок интерфейса
|
|
||||||
/// @param query запрос callback
|
|
||||||
void handleCallback(TgBot::CallbackQuery::Ptr query);
|
|
||||||
|
|
||||||
/// @brief Обработка сообщений боту
|
|
||||||
/// @details
|
|
||||||
/// Функция для обработки сообщений, которые юзер отправляет
|
|
||||||
/// боту. Необходима для обработки ревью и названий искомого
|
|
||||||
/// аниме. Внутри себя проверяет текущий state пользователя
|
|
||||||
/// в боте.
|
|
||||||
/// @param message обрабатываемое сообщение
|
|
||||||
void handleMessage(TgBot::Message::Ptr message);
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
TgBot::Api botApi;
|
TgBot::Api botApi;
|
||||||
|
|
||||||
void handleNavigation(TgBot::CallbackQuery::Ptr query);
|
void handleNavigation(const TgBot::CallbackQuery::Ptr query);
|
||||||
|
|
||||||
void processCallbackImpl(TgBot::CallbackQuery::Ptr query);
|
|
||||||
|
|
||||||
/// @brief Получить очередную страницу тайтлов из списка пользователя
|
/// @brief Получить очередную страницу тайтлов из списка пользователя
|
||||||
/// @param userId Идентификатор пользователя
|
/// @param userId Идентификатор пользователя
|
||||||
|
|
|
||||||
|
|
@ -39,16 +39,16 @@ TgBot::InlineKeyboardMarkup::Ptr KeyboardFactory::createMyTitles(std::vector<Tit
|
||||||
if(counter % 2 == 1) {
|
if(counter % 2 == 1) {
|
||||||
auto button = std::make_shared<TgBot::InlineKeyboardButton>();
|
auto button = std::make_shared<TgBot::InlineKeyboardButton>();
|
||||||
button->text = BotConstants::Button::PREV;
|
button->text = BotConstants::Button::PREV;
|
||||||
button->callbackData = BotConstants::Callback::LIST_PREV + ':' + std::to_string(titles[0].num);
|
button->callbackData = BotConstants::Callback::LIST_PREV + std::to_string(titles[0].num);
|
||||||
layout[counter / 2].push_back(button);
|
layout[counter / 2].push_back(button);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
auto button_prev = std::make_shared<TgBot::InlineKeyboardButton>();
|
auto button_prev = std::make_shared<TgBot::InlineKeyboardButton>();
|
||||||
button_prev->text = BotConstants::Button::PREV;
|
button_prev->text = BotConstants::Button::PREV;
|
||||||
button_prev->callbackData = BotConstants::Callback::LIST_PREV + ':' + std::to_string(titles[0].num);
|
button_prev->callbackData = BotConstants::Callback::LIST_PREV + std::to_string(titles[0].num);
|
||||||
auto button_next = std::make_shared<TgBot::InlineKeyboardButton>();
|
auto button_next = std::make_shared<TgBot::InlineKeyboardButton>();
|
||||||
button_next->text = BotConstants::Button::NEXT;
|
button_next->text = BotConstants::Button::NEXT;
|
||||||
button_next->callbackData = BotConstants::Callback::LIST_NEXT + ':' + std::to_string(titles[5].num);
|
button_next->callbackData = BotConstants::Callback::LIST_NEXT + std::to_string(titles[5].num);
|
||||||
layout.push_back({button_prev, button_next});
|
layout.push_back({button_prev, button_next});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,9 +3,7 @@
|
||||||
#include "constants.hpp"
|
#include "constants.hpp"
|
||||||
#include "handlers.hpp"
|
#include "handlers.hpp"
|
||||||
|
|
||||||
AnimeBot::AnimeBot(const std::string& token)
|
AnimeBot::AnimeBot(const std::string& token) : bot(token) {
|
||||||
: bot(token)
|
|
||||||
, handler(bot.getApi()) {
|
|
||||||
setupHandlers();
|
setupHandlers();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -13,13 +11,20 @@ void AnimeBot::setupHandlers() {
|
||||||
bot.getEvents().onCommand("start", [this](TgBot::Message::Ptr message) {
|
bot.getEvents().onCommand("start", [this](TgBot::Message::Ptr message) {
|
||||||
sendMainMenu(message->chat->id);
|
sendMainMenu(message->chat->id);
|
||||||
});
|
});
|
||||||
|
|
||||||
bot.getEvents().onCallbackQuery([this](TgBot::CallbackQuery::Ptr query) {
|
|
||||||
handler.handleCallback(query);
|
|
||||||
});
|
|
||||||
|
|
||||||
bot.getEvents().onAnyMessage([this](TgBot::Message::Ptr message) {
|
auto [text, kb] = BotHandlers::returnMyTitles(321);
|
||||||
handler.handleMessage(message);
|
|
||||||
|
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,
|
||||||
|
"",
|
||||||
|
"",
|
||||||
|
nullptr,
|
||||||
|
kb
|
||||||
|
);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -30,4 +35,10 @@ void AnimeBot::sendMainMenu(int64_t chatId) {
|
||||||
|
|
||||||
TgBot::Bot& AnimeBot::getBot() {
|
TgBot::Bot& AnimeBot::getBot() {
|
||||||
return bot;
|
return bot;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
void AnimeBot::showMyTitles() {
|
||||||
|
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
|
||||||
|
|
@ -3,23 +3,15 @@
|
||||||
#include "structs.hpp"
|
#include "structs.hpp"
|
||||||
#include "constants.hpp"
|
#include "constants.hpp"
|
||||||
|
|
||||||
void BotHandlers::handleCallback(TgBot::CallbackQuery::Ptr query) {
|
void BotHandlers::handleCallback(const TgBot::CallbackQuery::Ptr query) {
|
||||||
if (!query) {
|
std::string data = query -> data;
|
||||||
// TODO: log
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
if (data.starts_with(BotConstants::Callback::NAVIGATION)) {
|
||||||
// TODO: Тут mutex на многопоточке
|
handleNavigation(query);
|
||||||
botApi.answerCallbackQuery(query->id);
|
|
||||||
} catch (const std::exception& e) {
|
|
||||||
std::cerr << "answerCallbackQuery error";
|
|
||||||
//TODO: обработка ошибки
|
|
||||||
}
|
}
|
||||||
|
|
||||||
processCallbackImpl(query);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// В угоду потокобезопасности создаем новый экземпляр TgBot::Api
|
||||||
HandlerResult BotHandlers::returnMyTitles(int64_t userId) {
|
HandlerResult BotHandlers::returnMyTitles(int64_t userId) {
|
||||||
// Здесь должен происходить запрос на сервер
|
// Здесь должен происходить запрос на сервер
|
||||||
std::vector<Title> titles = {{123, "Школа мертвяков", "", 1}, {321, "KissXsis", "", 2}};
|
std::vector<Title> titles = {{123, "Школа мертвяков", "", 1}, {321, "KissXsis", "", 2}};
|
||||||
|
|
@ -31,37 +23,6 @@ HandlerResult BotHandlers::returnMyTitles(int64_t userId) {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
void BotHandlers::handleNavigation(TgBot::CallbackQuery::Ptr query) {
|
void BotHandlers::handleNavigation(const TgBot::CallbackQuery::Ptr query) {
|
||||||
const std::string& data = query->data;
|
|
||||||
if (data == BotConstants::Callback::MY_TITLES) {
|
|
||||||
auto [text, kb] = BotHandlers::returnMyTitles(321);
|
|
||||||
botApi.editMessageText(
|
|
||||||
text,
|
|
||||||
query->message->chat->id,
|
|
||||||
query->message->messageId,
|
|
||||||
"",
|
|
||||||
"",
|
|
||||||
nullptr,
|
|
||||||
kb
|
|
||||||
);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
botApi.sendMessage(query->message->chat->id, BotConstants::Text::SAD_ERROR, nullptr, nullptr);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void BotHandlers::handleMessage(TgBot::Message::Ptr message) {
|
|
||||||
//TODO: просмотр состояния пользователя
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue