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 response Параметры ответа: клавиатура и текст
|
||||
void editMessage(TgBot::CallbackQuery::Ptr query, HandlerResult response);
|
||||
void editMessage(int64_t chatId, int64_t messageId, HandlerResult response);
|
||||
|
||||
/// @brief Отрисовка текущего экрана (соотв. контексту)
|
||||
/// @param ctx - текущий контекст
|
||||
|
|
@ -121,7 +121,7 @@ private:
|
|||
|
||||
/// @brief Посылает интерфейс обработки ошибки на callback запрос
|
||||
/// @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);
|
||||
|
|
|
|||
|
|
@ -22,12 +22,12 @@ TgBot::InlineKeyboardMarkup::Ptr KeyboardFactory::createMyTitles(std::vector<Bot
|
|||
|
||||
int counter = 0;
|
||||
for(BotStructs::Title& title : titles) {
|
||||
if(counter >= 6) {
|
||||
if(counter >= BotConstants::DISP_TITLES_NUM) {
|
||||
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);
|
||||
button->text = std::to_string(title.num + 1) + " " + title.name;
|
||||
button->callbackData = BotConstants::Callback::CHOICE + std::to_string(title.num);
|
||||
row.push_back(button);
|
||||
counter++;
|
||||
if(counter % 2 == 0) {
|
||||
|
|
@ -40,7 +40,7 @@ TgBot::InlineKeyboardMarkup::Ptr KeyboardFactory::createMyTitles(std::vector<Bot
|
|||
if(counter % 2 == 1) {
|
||||
auto button = std::make_shared<TgBot::InlineKeyboardButton>();
|
||||
button->text = BotConstants::Button::PREV;
|
||||
if(titles[0].num == 1) {
|
||||
if(titles[0].num == 0) {
|
||||
button->callbackData = BotConstants::Callback::NAV_BACK;
|
||||
}
|
||||
else {
|
||||
|
|
@ -51,7 +51,7 @@ TgBot::InlineKeyboardMarkup::Ptr KeyboardFactory::createMyTitles(std::vector<Bot
|
|||
else {
|
||||
auto button_prev = std::make_shared<TgBot::InlineKeyboardButton>();
|
||||
button_prev->text = BotConstants::Button::PREV;
|
||||
if(titles[0].num == 1) {
|
||||
if(titles[0].num == 0) {
|
||||
button_prev->callbackData = BotConstants::Callback::NAV_BACK;
|
||||
}
|
||||
else {
|
||||
|
|
|
|||
|
|
@ -6,6 +6,8 @@
|
|||
void BotHandlers::handleNavigation(TgBot::CallbackQuery::Ptr query, UserContext& ctx) {
|
||||
const auto& current = ctx.history.back(); // текущий экран
|
||||
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)
|
||||
|
|
@ -30,54 +32,56 @@ void BotHandlers::handleNavigation(TgBot::CallbackQuery::Ptr query, UserContext&
|
|||
|
||||
auto result = renderCurrent(query, ctx);
|
||||
if(result.message == "meow") return; // TODO: убрать
|
||||
editMessage(query, result);
|
||||
editMessage(chatId, messageId, result);
|
||||
return;
|
||||
}
|
||||
|
||||
// Обработка back по интерфейсу
|
||||
if (data == BotConstants::Callback::NAV_BACK) {
|
||||
if (!popState(ctx)) {
|
||||
sendError(query, BotConstants::Text::SAD_ERROR);
|
||||
sendError(chatId, messageId, BotConstants::Text::SAD_ERROR);
|
||||
return;
|
||||
}
|
||||
auto result = renderCurrent(query, ctx);
|
||||
if(result.message == "meow") return; // TODO: убрать
|
||||
editMessage(query, result);
|
||||
editMessage(chatId, messageId, result);
|
||||
return;
|
||||
}
|
||||
|
||||
// Переходы вперёд (push)
|
||||
// Переходы вперёд (pyush)
|
||||
auto newStepOpt = computeNextStep(query, current);
|
||||
if (!newStepOpt.has_value()) {
|
||||
sendError(query, BotConstants::Text::SAD_ERROR);
|
||||
sendError(chatId, messageId, BotConstants::Text::SAD_ERROR);
|
||||
return;
|
||||
}
|
||||
|
||||
ctx.history.push_back(*newStepOpt);
|
||||
auto result = renderCurrent(query, ctx);
|
||||
if(result.message == "meow") return; // TODO: убрать
|
||||
editMessage(query, result);
|
||||
editMessage(chatId, messageId, result);
|
||||
}
|
||||
|
||||
HandlerResult BotHandlers::renderCurrent(TgBot::CallbackQuery::Ptr query, const UserContext& ctx) {
|
||||
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) {
|
||||
case UserState::MAIN_MENU:
|
||||
return showMainMenu();
|
||||
case UserState::VIEWING_MY_TITLES:
|
||||
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 {
|
||||
auto titles = t.get();
|
||||
|
||||
std::string message = formatTitlesList(titles);
|
||||
auto keyboard = KeyboardFactory::createMyTitles(titles);
|
||||
|
||||
editMessage(query, {message, keyboard});
|
||||
editMessage(chatId, messageId, {message, keyboard});
|
||||
|
||||
} catch (const std::exception& e) {
|
||||
sendError(query, BotConstants::Text::SERVER_ERROR);
|
||||
sendError(chatId, messageId, BotConstants::Text::SERVER_ERROR);
|
||||
// Логирование ошибки (например, в cerr)
|
||||
}
|
||||
});
|
||||
|
|
|
|||
|
|
@ -40,10 +40,12 @@ void BotHandlers::handleMessage(TgBot::Message::Ptr message) {
|
|||
void BotHandlers::processCallbackImpl(TgBot::CallbackQuery::Ptr query) {
|
||||
const std::string& data = query->data;
|
||||
int64_t userId = query->from->id;
|
||||
int64_t chatId = query->message->chat->id;
|
||||
int64_t messageId = query->message->messageId;
|
||||
auto it = userContexts.find(userId);
|
||||
if (it == userContexts.end()) {
|
||||
// TODO: log
|
||||
sendError(query, BotConstants::Text::AUTH_ERROR);
|
||||
sendError(chatId, messageId, BotConstants::Text::AUTH_ERROR);
|
||||
std::cout << "Error: Не нашел пользователя " << userId;
|
||||
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(
|
||||
response.message,
|
||||
query->message->chat->id,
|
||||
query->message->messageId,
|
||||
chatId,
|
||||
messageId,
|
||||
"",
|
||||
"",
|
||||
nullptr,
|
||||
|
|
@ -115,7 +117,7 @@ HandlerResult BotHandlers::showMainMenu() {
|
|||
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: посылать сообщение с кнопкой возврата в главное меню
|
||||
TgBot::InlineKeyboardMarkup::Ptr keyboard;
|
||||
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);
|
||||
}
|
||||
|
||||
editMessage(query, {errText, keyboard});
|
||||
editMessage(chatId, messageId, {errText, keyboard});
|
||||
}
|
||||
|
||||
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) {
|
||||
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) {
|
||||
ctx.history.clear();
|
||||
ctx.history.push_back({UserState::MAIN_MENU, 0});
|
||||
auto result = showMainMenu();
|
||||
editMessage(query, result);
|
||||
editMessage(chatId, messageId, result);
|
||||
}
|
||||
else if(data == BotConstants::Callback::ERROR_AUTH) {
|
||||
// TODO: продумать логику
|
||||
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