Forming the BotHandlers class structure
This commit is contained in:
parent
ea29fa79f0
commit
cdc1aa2e6b
6 changed files with 92 additions and 46 deletions
|
|
@ -39,16 +39,16 @@ TgBot::InlineKeyboardMarkup::Ptr KeyboardFactory::createMyTitles(std::vector<Tit
|
|||
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);
|
||||
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);
|
||||
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);
|
||||
button_next->callbackData = BotConstants::Callback::LIST_NEXT + ':' + std::to_string(titles[5].num);
|
||||
layout.push_back({button_prev, button_next});
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -3,7 +3,9 @@
|
|||
#include "constants.hpp"
|
||||
#include "handlers.hpp"
|
||||
|
||||
AnimeBot::AnimeBot(const std::string& token) : bot(token) {
|
||||
AnimeBot::AnimeBot(const std::string& token)
|
||||
: bot(token)
|
||||
, handler(bot.getApi()) {
|
||||
setupHandlers();
|
||||
}
|
||||
|
||||
|
|
@ -11,20 +13,13 @@ void AnimeBot::setupHandlers() {
|
|||
bot.getEvents().onCommand("start", [this](TgBot::Message::Ptr message) {
|
||||
sendMainMenu(message->chat->id);
|
||||
});
|
||||
|
||||
bot.getEvents().onCallbackQuery([this](TgBot::CallbackQuery::Ptr query) {
|
||||
handler.handleCallback(query);
|
||||
});
|
||||
|
||||
auto [text, kb] = BotHandlers::returnMyTitles(321);
|
||||
|
||||
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
|
||||
);
|
||||
bot.getEvents().onAnyMessage([this](TgBot::Message::Ptr message) {
|
||||
handler.handleMessage(message);
|
||||
});
|
||||
}
|
||||
|
||||
|
|
@ -35,10 +30,4 @@ void AnimeBot::sendMainMenu(int64_t chatId) {
|
|||
|
||||
TgBot::Bot& AnimeBot::getBot() {
|
||||
return bot;
|
||||
}
|
||||
|
||||
/*
|
||||
void AnimeBot::showMyTitles() {
|
||||
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
|
@ -3,15 +3,23 @@
|
|||
#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);
|
||||
void BotHandlers::handleCallback(TgBot::CallbackQuery::Ptr query) {
|
||||
if (!query) {
|
||||
// TODO: log
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
// TODO: Тут mutex на многопоточке
|
||||
botApi.answerCallbackQuery(query->id);
|
||||
} catch (const std::exception& e) {
|
||||
std::cerr << "answerCallbackQuery error";
|
||||
//TODO: обработка ошибки
|
||||
}
|
||||
|
||||
processCallbackImpl(query);
|
||||
}
|
||||
|
||||
/// В угоду потокобезопасности создаем новый экземпляр TgBot::Api
|
||||
HandlerResult BotHandlers::returnMyTitles(int64_t userId) {
|
||||
// Здесь должен происходить запрос на сервер
|
||||
std::vector<Title> titles = {{123, "Школа мертвяков", "", 1}, {321, "KissXsis", "", 2}};
|
||||
|
|
@ -23,6 +31,37 @@ HandlerResult BotHandlers::returnMyTitles(int64_t userId) {
|
|||
return result;
|
||||
}
|
||||
|
||||
void BotHandlers::handleNavigation(const TgBot::CallbackQuery::Ptr query) {
|
||||
void BotHandlers::handleNavigation(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;
|
||||
}
|
||||
|
||||
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