refactor(tgbot-front): change editMessage args
This commit is contained in:
parent
19164b8d9d
commit
7e0222d6f1
4 changed files with 33 additions and 25 deletions
|
|
@ -101,7 +101,7 @@ private:
|
||||||
/// кнопки в интерфейсе
|
/// кнопки в интерфейсе
|
||||||
/// @param query Callback запрос
|
/// @param query Callback запрос
|
||||||
/// @param response Параметры ответа: клавиатура и текст
|
/// @param response Параметры ответа: клавиатура и текст
|
||||||
void editMessage(TgBot::CallbackQuery::Ptr query, HandlerResult response);
|
void editMessage(int64_t chatId, int64_t messageId, HandlerResult response);
|
||||||
|
|
||||||
/// @brief Отрисовка текущего экрана (соотв. контексту)
|
/// @brief Отрисовка текущего экрана (соотв. контексту)
|
||||||
/// @param ctx - текущий контекст
|
/// @param ctx - текущий контекст
|
||||||
|
|
@ -121,7 +121,7 @@ private:
|
||||||
|
|
||||||
/// @brief Посылает интерфейс обработки ошибки на callback запрос
|
/// @brief Посылает интерфейс обработки ошибки на callback запрос
|
||||||
/// @param query запрос
|
/// @param query запрос
|
||||||
void sendError(TgBot::CallbackQuery::Ptr query, const std::string& errText);
|
void sendError(int64_t chatId, int64_t messageId, const std::string& errText);
|
||||||
|
|
||||||
// Форматирование для отображения в сообщении
|
// Форматирование для отображения в сообщении
|
||||||
std::string formatTitlesList(const std::vector<BotStructs::Title>& titles);
|
std::string formatTitlesList(const std::vector<BotStructs::Title>& titles);
|
||||||
|
|
|
||||||
|
|
@ -22,12 +22,12 @@ TgBot::InlineKeyboardMarkup::Ptr KeyboardFactory::createMyTitles(std::vector<Bot
|
||||||
|
|
||||||
int counter = 0;
|
int counter = 0;
|
||||||
for(BotStructs::Title& title : titles) {
|
for(BotStructs::Title& title : titles) {
|
||||||
if(counter >= 6) {
|
if(counter >= BotConstants::DISP_TITLES_NUM) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
auto button = std::make_shared<TgBot::InlineKeyboardButton>();
|
auto button = std::make_shared<TgBot::InlineKeyboardButton>();
|
||||||
button->text = std::to_string(title.num) + " " + title.name;
|
button->text = std::to_string(title.num + 1) + " " + title.name;
|
||||||
button->callbackData = "title:" + std::to_string(title.num);
|
button->callbackData = BotConstants::Callback::CHOICE + std::to_string(title.num);
|
||||||
row.push_back(button);
|
row.push_back(button);
|
||||||
counter++;
|
counter++;
|
||||||
if(counter % 2 == 0) {
|
if(counter % 2 == 0) {
|
||||||
|
|
@ -40,7 +40,7 @@ TgBot::InlineKeyboardMarkup::Ptr KeyboardFactory::createMyTitles(std::vector<Bot
|
||||||
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;
|
||||||
if(titles[0].num == 1) {
|
if(titles[0].num == 0) {
|
||||||
button->callbackData = BotConstants::Callback::NAV_BACK;
|
button->callbackData = BotConstants::Callback::NAV_BACK;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|
@ -51,7 +51,7 @@ TgBot::InlineKeyboardMarkup::Ptr KeyboardFactory::createMyTitles(std::vector<Bot
|
||||||
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;
|
||||||
if(titles[0].num == 1) {
|
if(titles[0].num == 0) {
|
||||||
button_prev->callbackData = BotConstants::Callback::NAV_BACK;
|
button_prev->callbackData = BotConstants::Callback::NAV_BACK;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,8 @@
|
||||||
void BotHandlers::handleNavigation(TgBot::CallbackQuery::Ptr query, UserContext& ctx) {
|
void BotHandlers::handleNavigation(TgBot::CallbackQuery::Ptr query, UserContext& ctx) {
|
||||||
const auto& current = ctx.history.back(); // текущий экран
|
const auto& current = ctx.history.back(); // текущий экран
|
||||||
const std::string& data = query->data;
|
const std::string& data = query->data;
|
||||||
|
int64_t chatId = query->message->chat->id;
|
||||||
|
int64_t messageId = query->message->messageId;
|
||||||
|
|
||||||
// Пагинация (в списках)
|
// Пагинация (в списках)
|
||||||
if ((data == BotConstants::Callback::LIST_PREV || data == BotConstants::Callback::LIST_NEXT)
|
if ((data == BotConstants::Callback::LIST_PREV || data == BotConstants::Callback::LIST_NEXT)
|
||||||
|
|
@ -30,54 +32,56 @@ void BotHandlers::handleNavigation(TgBot::CallbackQuery::Ptr query, UserContext&
|
||||||
|
|
||||||
auto result = renderCurrent(query, ctx);
|
auto result = renderCurrent(query, ctx);
|
||||||
if(result.message == "meow") return; // TODO: убрать
|
if(result.message == "meow") return; // TODO: убрать
|
||||||
editMessage(query, result);
|
editMessage(chatId, messageId, result);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Обработка back по интерфейсу
|
// Обработка back по интерфейсу
|
||||||
if (data == BotConstants::Callback::NAV_BACK) {
|
if (data == BotConstants::Callback::NAV_BACK) {
|
||||||
if (!popState(ctx)) {
|
if (!popState(ctx)) {
|
||||||
sendError(query, BotConstants::Text::SAD_ERROR);
|
sendError(chatId, messageId, BotConstants::Text::SAD_ERROR);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
auto result = renderCurrent(query, ctx);
|
auto result = renderCurrent(query, ctx);
|
||||||
if(result.message == "meow") return; // TODO: убрать
|
if(result.message == "meow") return; // TODO: убрать
|
||||||
editMessage(query, result);
|
editMessage(chatId, messageId, result);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Переходы вперёд (push)
|
// Переходы вперёд (pyush)
|
||||||
auto newStepOpt = computeNextStep(query, current);
|
auto newStepOpt = computeNextStep(query, current);
|
||||||
if (!newStepOpt.has_value()) {
|
if (!newStepOpt.has_value()) {
|
||||||
sendError(query, BotConstants::Text::SAD_ERROR);
|
sendError(chatId, messageId, BotConstants::Text::SAD_ERROR);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx.history.push_back(*newStepOpt);
|
ctx.history.push_back(*newStepOpt);
|
||||||
auto result = renderCurrent(query, ctx);
|
auto result = renderCurrent(query, ctx);
|
||||||
if(result.message == "meow") return; // TODO: убрать
|
if(result.message == "meow") return; // TODO: убрать
|
||||||
editMessage(query, result);
|
editMessage(chatId, messageId, result);
|
||||||
}
|
}
|
||||||
|
|
||||||
HandlerResult BotHandlers::renderCurrent(TgBot::CallbackQuery::Ptr query, const UserContext& ctx) {
|
HandlerResult BotHandlers::renderCurrent(TgBot::CallbackQuery::Ptr query, const UserContext& ctx) {
|
||||||
const auto& step = ctx.history.back();
|
const auto& step = ctx.history.back();
|
||||||
int64_t userId = query->from->id;
|
//int64_t userId = query->from->id;
|
||||||
|
int64_t chatId = query->message->chat->id;
|
||||||
|
int64_t messageId = query->message->messageId;
|
||||||
switch (step.state) {
|
switch (step.state) {
|
||||||
case UserState::MAIN_MENU:
|
case UserState::MAIN_MENU:
|
||||||
return showMainMenu();
|
return showMainMenu();
|
||||||
case UserState::VIEWING_MY_TITLES:
|
case UserState::VIEWING_MY_TITLES:
|
||||||
server_.fetchUserTitlesAsync(std::to_string(2)) // ALARM: тестовое значение вместо userId
|
server_.fetchUserTitlesAsync(std::to_string(2)) // ALARM: тестовое значение вместо userId
|
||||||
.then([this, query](pplx::task<std::vector<BotStructs::Title>> t) {
|
.then([this, chatId, messageId](pplx::task<std::vector<BotStructs::Title>> t) {
|
||||||
try {
|
try {
|
||||||
auto titles = t.get();
|
auto titles = t.get();
|
||||||
|
|
||||||
std::string message = formatTitlesList(titles);
|
std::string message = formatTitlesList(titles);
|
||||||
auto keyboard = KeyboardFactory::createMyTitles(titles);
|
auto keyboard = KeyboardFactory::createMyTitles(titles);
|
||||||
|
|
||||||
editMessage(query, {message, keyboard});
|
editMessage(chatId, messageId, {message, keyboard});
|
||||||
|
|
||||||
} catch (const std::exception& e) {
|
} catch (const std::exception& e) {
|
||||||
sendError(query, BotConstants::Text::SERVER_ERROR);
|
sendError(chatId, messageId, BotConstants::Text::SERVER_ERROR);
|
||||||
// Логирование ошибки (например, в cerr)
|
// Логирование ошибки (например, в cerr)
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -40,10 +40,12 @@ void BotHandlers::handleMessage(TgBot::Message::Ptr message) {
|
||||||
void BotHandlers::processCallbackImpl(TgBot::CallbackQuery::Ptr query) {
|
void BotHandlers::processCallbackImpl(TgBot::CallbackQuery::Ptr query) {
|
||||||
const std::string& data = query->data;
|
const std::string& data = query->data;
|
||||||
int64_t userId = query->from->id;
|
int64_t userId = query->from->id;
|
||||||
|
int64_t chatId = query->message->chat->id;
|
||||||
|
int64_t messageId = query->message->messageId;
|
||||||
auto it = userContexts.find(userId);
|
auto it = userContexts.find(userId);
|
||||||
if (it == userContexts.end()) {
|
if (it == userContexts.end()) {
|
||||||
// TODO: log
|
// TODO: log
|
||||||
sendError(query, BotConstants::Text::AUTH_ERROR);
|
sendError(chatId, messageId, BotConstants::Text::AUTH_ERROR);
|
||||||
std::cout << "Error: Не нашел пользователя " << userId;
|
std::cout << "Error: Не нашел пользователя " << userId;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
@ -97,11 +99,11 @@ void BotHandlers::increasePayload(int64_t& payload, const UserState curState) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void BotHandlers::editMessage(TgBot::CallbackQuery::Ptr query, HandlerResult response) {
|
void BotHandlers::editMessage(int64_t chatId, int64_t messageId, HandlerResult response) {
|
||||||
botApi.editMessageText(
|
botApi.editMessageText(
|
||||||
response.message,
|
response.message,
|
||||||
query->message->chat->id,
|
chatId,
|
||||||
query->message->messageId,
|
messageId,
|
||||||
"",
|
"",
|
||||||
"",
|
"",
|
||||||
nullptr,
|
nullptr,
|
||||||
|
|
@ -115,7 +117,7 @@ HandlerResult BotHandlers::showMainMenu() {
|
||||||
return HandlerResult{BotConstants::Text::MAIN_MENU, keyboard};
|
return HandlerResult{BotConstants::Text::MAIN_MENU, keyboard};
|
||||||
}
|
}
|
||||||
|
|
||||||
void BotHandlers::sendError(TgBot::CallbackQuery::Ptr query, const std::string& errText) {
|
void BotHandlers::sendError(int64_t chatId, int64_t messageId, const std::string& errText) {
|
||||||
//TODO: посылать сообщение с кнопкой возврата в главное меню
|
//TODO: посылать сообщение с кнопкой возврата в главное меню
|
||||||
TgBot::InlineKeyboardMarkup::Ptr keyboard;
|
TgBot::InlineKeyboardMarkup::Ptr keyboard;
|
||||||
if (errText == BotConstants::Text::SAD_ERROR) {
|
if (errText == BotConstants::Text::SAD_ERROR) {
|
||||||
|
|
@ -125,7 +127,7 @@ void BotHandlers::sendError(TgBot::CallbackQuery::Ptr query, const std::string&
|
||||||
keyboard = nullptr; //KeyboardFactory::createError(BotConstants::Callback::ERROR_AUTH);
|
keyboard = nullptr; //KeyboardFactory::createError(BotConstants::Callback::ERROR_AUTH);
|
||||||
}
|
}
|
||||||
|
|
||||||
editMessage(query, {errText, keyboard});
|
editMessage(chatId, messageId, {errText, keyboard});
|
||||||
}
|
}
|
||||||
|
|
||||||
void BotHandlers::createInitContext(int64_t chatId) {
|
void BotHandlers::createInitContext(int64_t chatId) {
|
||||||
|
|
@ -135,16 +137,18 @@ void BotHandlers::createInitContext(int64_t chatId) {
|
||||||
|
|
||||||
void BotHandlers::handleError(TgBot::CallbackQuery::Ptr query, UserContext& ctx) {
|
void BotHandlers::handleError(TgBot::CallbackQuery::Ptr query, UserContext& ctx) {
|
||||||
const std::string& data = query->data;
|
const std::string& data = query->data;
|
||||||
|
int64_t chatId = query->message->chat->id;
|
||||||
|
int64_t messageId = query->message->messageId;
|
||||||
|
|
||||||
if(data == BotConstants::Callback::ERROR_NAVIGATION) {
|
if(data == BotConstants::Callback::ERROR_NAVIGATION) {
|
||||||
ctx.history.clear();
|
ctx.history.clear();
|
||||||
ctx.history.push_back({UserState::MAIN_MENU, 0});
|
ctx.history.push_back({UserState::MAIN_MENU, 0});
|
||||||
auto result = showMainMenu();
|
auto result = showMainMenu();
|
||||||
editMessage(query, result);
|
editMessage(chatId, messageId, result);
|
||||||
}
|
}
|
||||||
else if(data == BotConstants::Callback::ERROR_AUTH) {
|
else if(data == BotConstants::Callback::ERROR_AUTH) {
|
||||||
// TODO: продумать логику
|
// TODO: продумать логику
|
||||||
HandlerResult result = {BotConstants::Text::AUTH_ERROR, nullptr};
|
HandlerResult result = {BotConstants::Text::AUTH_ERROR, nullptr};
|
||||||
editMessage(query, result);
|
editMessage(chatId, messageId, result);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue