tgbot-cpp icon indicating copy to clipboard operation
tgbot-cpp copied to clipboard

TgBot::ReplyKeyboardMarkup::Ptr and problems with it

Open artemka-sh opened this issue 7 months ago • 0 comments

Using this keyboard as in examples sometimes I get something other than what I write in the buttons, sometimes a random offer to send a phone number or geolocation may pop up, although nothing like that was written

void TelegramMessenger::sendQuestionWithKeyboard(qint64 userId, const Question& question) {
    std::string text = question.question;
    TgBot::ReplyKeyboardMarkup::Ptr keyboard(new TgBot::ReplyKeyboardMarkup);
    keyboard->resizeKeyboard = true;
    keyboard->oneTimeKeyboard = true;
    if (!question.answers.empty()) {
        keyboard->keyboard.clear();
        for (const auto& ans : question.answers) {
            std::vector<TgBot::KeyboardButton::Ptr> row;
            TgBot::KeyboardButton::Ptr btn(new TgBot::KeyboardButton);
            btn->text = ans;
            row.push_back(btn);
            keyboard->keyboard.push_back(row);
        }
    } else {
        keyboard = nullptr;
    }
    bot->getApi().sendMessage(userId, text, nullptr, nullptr, keyboard);
} 

Here is the code that crashed, it is created and sent to functions in its scope I don't know why you need to add these parameters for everything to work well, I haven't tested it completely yet, but the message with the pop-up window asking to send a phone number no longer popped up

void TelegramMessenger::sendQuestionWithKeyboard(qint64 userId, const Question question) {
    std::string text = question.question;
    TgBot::ReplyKeyboardMarkup::Ptr keyboard(new TgBot::ReplyKeyboardMarkup);
    keyboard->resizeKeyboard = true;
    keyboard->oneTimeKeyboard = true;
    if (!question.answers.empty()) {
        keyboard->keyboard.clear();
        for (const auto& ans : question.answers) {
            std::vector<TgBot::KeyboardButton::Ptr> row;
            TgBot::KeyboardButton::Ptr btn(new TgBot::KeyboardButton);
            btn->text = ans;
            btn->requestContact = false;
            btn->requestLocation = false;
            btn->requestPoll = TgBot::KeyboardButtonPollType::Ptr();
            row.push_back(btn);
            keyboard->keyboard.push_back(row);
        }
    } else {
        keyboard = nullptr;
    }
    bot->getApi().sendMessage(userId, text, nullptr, nullptr, keyboard);
} 

I'm leaving this here, maybe it will help someone a little. I'll be glad if you tell me what I'm doing wrong, I remember creating this keyboard with permanent storage and tried everything, somehow I managed to do it by manipulating memory so that this error doesn't appear, but I don't remember how)

I hope I managed to convey the idea

artemka-sh avatar Jul 04 '25 17:07 artemka-sh