edisonchee / slimbot

Telegram Bot API for Node.js
MIT License
223 stars 37 forks source link

Size limit of callback_data (inlineKeyboard) #25

Closed ghost closed 5 years ago

ghost commented 5 years ago

Dear edisonchee,

your API keeps its promise. It's straightforward but effective. Thank you.

I experienced something (maybe) odd regarding the attribute callback_data of a button in an inlineKeyboard. In my case, every button represents an object of the class Event, which is retrieved from a database.

Obviously I want to determine which button was clicked by listening for the event callback_query. query.data is a stringified JSON object. Parsing does work but if the string contains too many characters I would get the following error.

Unhandled rejection Error: 400: {"ok":false,"error_code":400,"description":"Bad Request: BUTTON_DATA_INVALID"}

Here is my sourcecode for reference.

slimbot.on('callback_query', query => {
    const data = JSON.parse(query.data);
    switch (data.type) {
        case "info": Responser.info(data.value, query.message.chat.id); break;
        case "register": Responser.register(data.value.eId, data.value.u , query.message.chat.id);
    }
});

Creation of the buttons:

for (i = 0; i < events.length; i++) {
                    inlineKeyboard.push([{
                        text: events[i].toString(),
                        callback_data: JSON.stringify({
                            type: 'register',
                            value: {
                                eId: events[i].eventId,
                                u: user.username
                            }
                        })
                    }]);
}

This works but I actually want to store more data in value. Is there an alternative?

Thank you in advance! Joachim

edisonchee commented 5 years ago

Hi @jbBorya, thank you and I'm glad you found the library useful.

According to Telegram's Bot API documentation, the callback_data to be sent for each button has a size limit of 64 bytes.

I'm not aware of any way to go beyond the size limit. Sorry about that!

ghost commented 5 years ago

Thank you for the response ^^