Skip to content

TgBot::ReplyKeyboardMarkup::Ptr and problems with it #339

@artemka-sh

Description

@artemka-sh

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions