I’m trying to set up a custom keyboard for my Telegram bot. Right now when a user presses a button it just sends the button text to the bot. Is there a way to make the buttons send different codes or messages instead of their labels?
For example I want the ‘Hello’ button to send ‘100’ and the ‘Hi’ button to send ‘200’ when pressed. Here’s what my current keyboard setup looks like:
Is there some way to customize the data sent by each button? I couldn’t find any options for this in the Telegram bot API docs. Any help would be appreciated!
I’ve actually implemented something similar in one of my Telegram bots. Using inline keyboards is definitely the way to go for custom button responses. One thing to keep in mind though - make sure you set up a callback query handler in your bot code to process those custom callback data values.
Here’s a quick example of how I structured my callback handler:
$bot->on(function($update) use ($bot) {
if ($callbackQuery = $update->getCallbackQuery()) {
$data = $callbackQuery->getData();
switch($data) {
case '100':
// Handle 'Hello' logic
break;
case '200':
// Handle 'Hi' logic
break;
}
}
});
This lets you define specific actions for each button press. Just remember to call answerCallbackQuery() to acknowledge the button press to Telegram.
You can achieve this by using inline keyboards instead of regular keyboards. Inline keyboards allow you to define custom callback data for each button. Here’s how you can modify your code:
With this setup, when a user presses ‘Hello’, your bot will receive ‘100’ as the callback data. You’ll need to handle these callback queries in your bot’s logic. This approach gives you more flexibility in defining custom actions for each button press.