I’m working on a Telegram bot and need to include extra information with my custom keyboard buttons that users can’t see.
Basically, I want each button to carry some hidden data like database IDs or other values that get sent back when the button is pressed. Something like this structure:
The user would only see “ProductA” and “ProductB” as button text, but when they click, I need to receive the corresponding item_id value along with the button press.
Is this possible with the Telegram Bot API? What’s the best approach to handle this kind of hidden data transmission through keyboard buttons?
totally! use inline keyboards for this, just add the callback_data with your hidden stuff (like item IDs). when the button’s pressed, your bot will get that data back. just a heads up tho, callback_data has a 64-byte limit, so be concise!
I’ve dealt with this exact scenario before. It depends on which keyboard type you’re using. For inline keyboards, just store the hidden data directly in the callback_data parameter - works perfectly for what you need. But if you’re using regular custom keyboards (ReplyKeyboardMarkup), there’s no built-in way to attach hidden data since they only send back the button text. You’ll need to maintain a mapping on your server - store the connection between button text and your hidden data (like item IDs) in memory or a database, then look it up when you get the button text. I’ve found this server-side mapping approach pretty reliable for custom keyboards, though inline keyboards are way more straightforward for this.
I’ve had good luck combining inline keyboards with simple encoding when callback_data gets too long. Skip the full JSON objects - just use short encoded strings that map to your database records. Something like ‘p_42’ or ‘p_73’ for product IDs, then decode them in your callback handler. Keeps things compact but still readable.
If you’re doing server-side mapping, definitely add a cleanup mechanism. Stale mappings pile up and eat memory. But honestly, inline keyboards are cleaner overall since everything stays in the Telegram message context.