Keyboards
One of the coolest things about Telegram Bot API are the new custom keyboards. Whenever your bot sends a message, it can pass along a special keyboard with predefined reply options. Telegram apps that receive the message will display your keyboard to the user. Tapping any of the buttons will immediately send the respective command. This way you can drastically simplify user interaction with your bot.
Telegram currently support text and emoji for your buttons. Here are some custom keyboard examples:
Reply Keyboard Markup
The below example will send a message and automatically show a custom keyboard.
If one_time_keyboard
is set as true
, the keyboard will be shown only once.
See ReplyKeyboardMarkup docs for a list of supported parameters and other info.
Example:
- Standalone
- Laravel
$keyboard = [
['7', '8', '9'],
['4', '5', '6'],
['1', '2', '3'],
['0']
];
$reply_markup = $telegram->replyKeyboardMarkup($keyboard, true, true);
$response = $telegram->sendMessage('CHAT_ID', 'Hello World', false, null, $reply_markup);
$messageId = $response->getMessageId();
$keyboard = [
['7', '8', '9'],
['4', '5', '6'],
['1', '2', '3'],
['0']
];
$reply_markup = Telegram::replyKeyboardMarkup($keyboard, true, true);
$response = Telegram::sendMessage('CHAT_ID', 'Hello World', false, null, $reply_markup);
$messageId = $response->getMessageId();
Reply Keyboard Hide
Telegram clients will hide the current custom keyboard and display the default letter-keyboard. See ReplyKeyboardHide docs for more info.
If called with no parameters, the selective
option defaults to false
.
- Standalone
- Laravel
$reply_markup = $telegram->replyKeyboardHide();
$response = $telegram->sendMessage('CHAT_ID', 'Hello World', false, null, $reply_markup);
$reply_markup = Telegram::replyKeyboardHide();
$response = Telegram::sendMessage('CHAT_ID', 'Hello World', false, null, $reply_markup);
Force Reply
Shows reply interface to the user, as if they manually selected the bot‘s message and tapped ’Reply'. See ForceReply docs for more info.
If called with no parameters, the selective
option defaults to false
.
- Standalone
- Laravel
$reply_markup = $telegram->forceReply();
$response = $telegram->sendMessage('CHAT_ID', 'Hello World', false, null, $reply_markup);
$reply_markup = Telegram::forceReply();
$response = Telegram::sendMessage('CHAT_ID', 'Hello World', false, null, $reply_markup);