tgbot-cpp
tgbot-cpp copied to clipboard
TgTypeParser: wrong json serialization in InlineKeyboardMarkup
I am using the InlineReplyKeyboard and Google Protobuf to store the callback data. My code looks like this:
InlineKeyboardMarkup::Ptr keyboard(new InlineKeyboardMarkup);
std::vector<InlineKeyboardButton::Ptr> keyButtons;
InlineKeyboardButton::Ptr btnYes(new InlineKeyboardButton);
btnYes->text = "Yes";
it->second.set_flash(true);
btnYes->callbackData = it->second.SerializeAsString(); //here I call the SerializeAsString from the protobuf message
keyButtons.push_back(btnYes);
InlineKeyboardButton::Ptr btnNo(new InlineKeyboardButton);
btnNo->text = "No";
it->second.set_flash(false);
btnNo->callbackData = it->second.SerializeAsString(); //here I call the SerializeAsString from the protobuf message
keyButtons.push_back(btnNo);
keyboard->inlineKeyboard.push_back(keyButtons);
master->bot->getApi().sendMessage(message->chat->id, "Force flash?", false, 0, keyboard);
A string I want to send looks like this: " FF Karlshorst43774906
or as hex representation 080210011803220D4646204B61726C73686F72737432083433373734393036
. These are two strings, one containing a text and the other one the chat id as text.
The corresponding JSON generated by the TgTypeParser is:
{"inline_keyboard":[[{"text":"Yes","callback_data":"
FF Karlshorst43774906P"},{"text":"No","callback_data":"
FF Karlshorst43774906"}]]}
or again as hex:
7B22696E6C696E655F6B6579626F617264223A5B5B7B2274657874223A22596573222C2263616C6C6261636B5F64617461223A22080210011803220D4646204B61726C73686F727374320834333737343930365001227D2C7B2274657874223A224E6F222C2263616C6C6261636B5F64617461223A22080210011803220D4646204B61726C73686F72737432083433373734393036227D5D5D7D
Whit this I get the error message: Bad Request: can't parse reply keyboard markup JSON object
. I already tried to put quotation marks manually around the serialized object but had no luck.
I would like to add that I used similar code and got no errors. I have no idea why now... Help is much appreciated.
The "
in �����" FF Karlshorst43774906
is not escaped and causes a problem. Actually, the problem can also occur with all other api functions with invalid strings. Does anyone have a good idea how to address the problem globally? For example, you can't just encode the whole API request at the very end. In this case, you would have to decode the callback data again.