Question
Is it possible to send callback_data as stringified json? Do I have to format it in any way? It would be easier to parse afterward on the hook.
const callbackData = {
action: 'helpRequestInterested',
userId: user._id,
helpRequestId: helpRequest._id
};
const stringifiedCBdata = JSON.stringify(callbackData);
const keyboard = {
reply_markup: {
inline_keyboard:[[
{ text: "This works", callback_data: `${callbackData.action} ${callbackData.userId}_${callbackData.helpRequestId}` },
// { text: "This throws the err", callback_data: stringifiedCBdata },
]],
hide_keyboard: false,
resize_keyboard: true
}
};
I may found two related issues in other repositories:
Allowed charcters => maybe {} is not allowed?
Size limitation but the stringified json shouldn't be much longer.
why not just go JSON.stringify(your object) and then JSON.parse(callback_data) later in your code? with callback_data obviously being extracted from the appropriate place
@david1602 I don't quite understand the message is stringified in the second (commented) inline keyboard but it throws an err.
It works when I stringify only two fields, when I try to add another third field it fails with error BUTTON_DATA_INVALID
Looks like it has limitation on value length?

Limit = 64 bytes
Yes there is a limit, it's not documented because it's a Telegram side issue which can change anytime. We recommend to always cross reference with the official docs when using this library.
https://core.telegram.org/bots/api#inlinekeyboardbutton
callback_data | String | Optional. Data to be sent in a聽callback query聽to the bot when button is pressed, 1-64 bytes
Most helpful comment
why not just go
JSON.stringify(your object)and thenJSON.parse(callback_data)later in your code? withcallback_dataobviously being extracted from the appropriate place