Node-telegram-bot-api: Question/Bug: BUTTON_DATA_INVALID if callback_data is stringified json

Created on 22 Mar 2019  路  7Comments  路  Source: yagop/node-telegram-bot-api

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.

question

Most helpful comment

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

All 7 comments

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?

Screenshot_2020-01-30_14-04-30

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Kasra-S picture Kasra-S  路  3Comments

mbrammer picture mbrammer  路  3Comments

antonrifco picture antonrifco  路  3Comments

jacopocappelli1989 picture jacopocappelli1989  路  4Comments

lenny76 picture lenny76  路  3Comments