How to include hidden data in Telegram Bot custom keyboard buttons?

Hey everyone! I’m working on a Telegram Bot and I’m stuck with a problem. I want to add some extra info to the custom keyboard buttons, but I don’t want the users to see it. Is there a way to do this?

For example, I’d like to attach an ID to each button, something like this:

[
  { text: "Option A", data: { id: 1 } },
  { text: "Option B", data: { id: 2 } },
  { text: "Option C", data: { id: 3 } }
]

This way, when a user taps a button, I could get both the visible text and the hidden ID. I’ve looked through the docs but couldn’t find anything about this. Any ideas or workarounds? Thanks in advance for your help!

I’ve actually tackled this issue before in one of my Telegram bot projects. Unfortunately, custom keyboard buttons don’t support hidden data fields like inline keyboard buttons do. However, I found a workaround that might help you.

What I did was create a mapping system on the server side. Each button’s visible text corresponded to a unique identifier in my database. When a user pressed a button, I’d receive the text, look up the corresponding ID in my mapping, and proceed from there.

It’s not as elegant as having the data directly in the button, but it worked well for my needs. You’ll need to ensure your button texts are unique, or use a combination of text and position if you have duplicate labels.

This approach does require a bit more server-side logic, but it keeps the user interface clean while still allowing you to associate additional data with each button press. Hope this helps!

yo, i got a hack for ya. try using emojis to hide ur IDs. like:

:red_circle: Option A
:orange_circle: Option B
:green_circle: Option C

then map each emoji to an ID in ur code. users won’t notice, but u can easily parse it. works like a charm for me, no extra database stuff needed. just keep it on the down low :wink:

While custom keyboard buttons don’t inherently support hidden data, there’s a clever workaround you can use. Consider encoding the ID into the button text itself, but in a way that’s not obvious to users. For example:

Option A [1]
Option B [2]
Option C [3]

When you receive the button press, you can parse out the ID from the text. This keeps your UI clean while still attaching the necessary data. Just remember to strip out the ID before displaying the text in your bot’s responses.

I’ve used this method in several projects, and it’s proven quite effective. It requires minimal changes to your existing code and doesn’t need any server-side mapping. Just be cautious with button text length, as Telegram has limits.