Hey everyone! I’m working on a Telegram bot and I’m wondering if it’s possible to attach some extra info to the custom keyboard buttons that the user can’t see.
For example, I want to add an ID to each button, but I don’t want it to show up on the actual keyboard. Something like this:
[
{text: 'Cool Button', hidden_data: {id: 123}},
{text: 'Awesome Button', hidden_data: {id: 456}}
]
Is there a way to do this with the Telegram Bot API? I’ve looked through the docs but couldn’t find anything about adding hidden data to keyboard buttons. Any ideas or workarounds would be super helpful! Thanks in advance!
hey there! i’ve run into this too. what about using a prefix system? like ‘btn_123_Cool Button’ and ‘btn_456_Awesome Button’. then u can parse the prefix when handling callbacks. it keeps everything in one place and doesn’t need extra server logic. just an idea!
I’ve actually tackled this issue before in one of my projects. Unfortunately, Telegram doesn’t provide a direct way to attach hidden data to keyboard buttons. However, I found a workaround that might help you out.
What I did was create a mapping on the server-side between the button text and the hidden data. So when a user clicks a button, you receive the button text in the callback query. You can then use that text to look up the corresponding hidden data in your server-side mapping.
For instance, you could store something like this on your server:
buttonData = {
‘Cool Button’: { id: 123 },
‘Awesome Button’: { id: 456 }
}
When you receive a callback query with the button text, you can easily retrieve the associated data. It’s not as clean as having the data directly in the button, but it gets the job done without cluttering the user interface.
Just remember to keep your button texts unique if you’re using this method. Hope this helps!
While Telegram doesn’t offer a built-in way to add hidden data to buttons, there’s another approach you might consider. Instead of trying to attach hidden data directly, you could encode the information into the button text itself using a delimiter that’s unlikely to appear in normal text.
For example, you could use something like this:
[
{text: 'Cool Button|123'},
{text: 'Awesome Button|456'}
]
When you receive the callback, you can split the text on the ‘|’ character to separate the visible text from the hidden ID. This method keeps your data tied directly to each button without complicating your server-side logic. Just be sure to handle any edge cases where the delimiter might appear in your button text naturally.