Working with Keyboards
A keyboard consists of buttons beneath the message. They can be used to open links, perform actions, and trigger commands.
Methods that support keyboard functionality:
- imbot.message.add — send a message on behalf of the chat bot
- imbot.message.update — modify a sent message from the chat bot
- imbot.command.answer — send a response to a chat bot command
- im.message.add — send a message in the chat
- im.message.update — modify a sent message
How to Add a Keyboard
To add a keyboard, pass the KEYBOARD parameter when creating or updating a message.
KEYBOARD can be passed as:
- a JSON string
- an object with the root key
BUTTONS - an array of buttons without wrapping
If the KEYBOARD does not contain the BUTTONS key, the server will automatically assume that a shortened format has been provided and will wrap the array in BUTTONS.
{
"KEYBOARD": {
"BUTTONS": [
{ "TEXT": "Button", "LINK": "https://example.com" }
]
}
}
{
"KEYBOARD": [
{ "TEXT": "Button", "LINK": "https://example.com" }
]
}
Button Fields
|
Name |
Description |
|
TEXT |
Button text. For all buttons except |
|
TYPE |
Move the button to a new line. The only allowed value is |
|
LINK |
Button link. |
|
APP_ID |
Application identifier for the chat. Deprecated scenario. To open an application from the chat, use widgets |
|
APP_PARAMS |
Parameters for launching the application in the chat. Pass together with Deprecated scenario. To open an application from the chat, use widgets Currently, the option with parameters |
|
ACTION |
Action:
Available starting from REST API IM revision 28 |
|
ACTION_VALUE |
Value for
Available starting from REST API IM revision 28 |
|
COMMAND |
Command for the bot. Read more about command processing by the chat bot below |
|
COMMAND_PARAMS |
Command parameters. Pass together with |
|
BLOCK |
Button blocking. Allowed values:
Default — |
|
DISABLED |
Button activity. Allowed values:
Default — |
|
CONTEXT |
Display context. Allowed values:
Default — |
|
DISPLAY |
Button display. Allowed values:
Default — |
|
WIDTH |
Button width in pixels |
|
BG_COLOR |
Button color in HEX code format |
|
BG_COLOR_TOKEN |
Button color token. Allowed values:
Default — |
|
TEXT_COLOR |
Button text color in HEX code format |
|
OFF_BG_COLOR |
Button color in HEX code format in inactive state |
|
OFF_TEXT_COLOR |
Button text color in HEX code format in inactive state |
Example of Sending a Message with a Keyboard from a Chat Bot
How to Use Examples in Documentation
curl -X POST \
-H "Content-Type: application/json" \
-H "Accept: application/json" \
-d '{"BOT_ID":1291,"DIALOG_ID":"chat2725","MESSAGE":"Select an action","URL_PREVIEW":"Y","KEYBOARD":{"BUTTONS":[{"TEXT":"Open Website","LINK":"https://www.example.com/","DISPLAY":"LINE","BG_COLOR_TOKEN":"primary"},{"TEXT":"Task Command","COMMAND":"task","COMMAND_PARAMS":"task #1","DISPLAY":"LINE","BG_COLOR_TOKEN":"secondary"},{"TYPE":"NEWLINE"},{"TEXT":"Insert Text","ACTION":"PUT","ACTION_VALUE":"/task task #1","DISPLAY":"BLOCK","BG_COLOR_TOKEN":"alert","TEXT_COLOR":"#FFFFFF"},{"TEXT":"Neutral Button","ACTION":"SEND","ACTION_VALUE":"Done","DISPLAY":"BLOCK","BG_COLOR_TOKEN":"base"}]},"CLIENT_ID":"**put_your_client_id_here**"}' \
https://**put_your_bitrix24_address**/rest/**put_your_user_id_here**/**put_your_webhook_here**/imbot.message.add
curl -X POST \
-H "Content-Type: application/json" \
-H "Accept: application/json" \
-d '{"BOT_ID":1291,"DIALOG_ID":"chat2725","MESSAGE":"Select an action","URL_PREVIEW":"Y","KEYBOARD":{"BUTTONS":[{"TEXT":"Open Website","LINK":"https://www.example.com/","DISPLAY":"LINE","BG_COLOR_TOKEN":"primary"},{"TEXT":"Task Command","COMMAND":"task","COMMAND_PARAMS":"task #1","DISPLAY":"LINE","BG_COLOR_TOKEN":"secondary"},{"TYPE":"NEWLINE"},{"TEXT":"Insert Text","ACTION":"PUT","ACTION_VALUE":"/task task #1","DISPLAY":"BLOCK","BG_COLOR_TOKEN":"alert","TEXT_COLOR":"#FFFFFF"},{"TEXT":"Neutral Button","ACTION":"SEND","ACTION_VALUE":"Done","DISPLAY":"BLOCK","BG_COLOR_TOKEN":"base"}]},"auth":"**put_access_token_here**"}' \
https://**put_your_bitrix24_address**/rest/imbot.message.add
try {
const response = await $b24.callMethod(
'imbot.message.add',
{
BOT_ID: 1291,
DIALOG_ID: 'chat2725',
MESSAGE: 'Select an action',
URL_PREVIEW: 'Y',
KEYBOARD: {
BUTTONS: [
{
TEXT: 'Open Website',
LINK: 'https://www.example.com/',
DISPLAY: 'LINE',
BG_COLOR_TOKEN: 'primary'
},
{
TEXT: 'Task Command',
COMMAND: 'task',
COMMAND_PARAMS: 'task #1',
DISPLAY: 'LINE',
BG_COLOR_TOKEN: 'secondary'
},
{ TYPE: 'NEWLINE' },
{
TEXT: 'Insert Text',
ACTION: 'PUT',
ACTION_VALUE: '/task task #1',
DISPLAY: 'BLOCK',
BG_COLOR_TOKEN: 'alert',
TEXT_COLOR: '#FFFFFF'
},
{
TEXT: 'Neutral Button',
ACTION: 'SEND',
ACTION_VALUE: 'Done',
DISPLAY: 'BLOCK',
BG_COLOR_TOKEN: 'base'
}
]
}
}
);
const result = response.getData().result;
console.log('Created message with ID:', result);
processResult(result);
} catch (error) {
console.error('Error:', error);
}
try {
$response = $b24Service
->core
->call(
'imbot.message.add',
[
'BOT_ID' => 1291,
'DIALOG_ID' => 'chat2725',
'MESSAGE' => 'Select an action',
'URL_PREVIEW' => 'Y',
'KEYBOARD' => [
'BUTTONS' => [
[
'TEXT' => 'Open Website',
'LINK' => 'https://www.example.com/',
'DISPLAY' => 'LINE',
'BG_COLOR_TOKEN' => 'primary'
],
[
'TEXT' => 'Task Command',
'COMMAND' => 'task',
'COMMAND_PARAMS' => 'task #1',
'DISPLAY' => 'LINE',
'BG_COLOR_TOKEN' => 'secondary'
],
['TYPE' => 'NEWLINE'],
[
'TEXT' => 'Insert Text',
'ACTION' => 'PUT',
'ACTION_VALUE' => '/task task #1',
'DISPLAY' => 'BLOCK',
'BG_COLOR_TOKEN' => 'alert',
'TEXT_COLOR' => '#FFFFFF'
],
[
'TEXT' => 'Neutral Button',
'ACTION' => 'SEND',
'ACTION_VALUE' => 'Done',
'DISPLAY' => 'BLOCK',
'BG_COLOR_TOKEN' => 'base'
]
]
]
]
);
$result = $response
->getResponseData()
->getResult();
echo 'Success: ' . print_r($result, true);
processData($result);
} catch (Throwable $e) {
error_log($e->getMessage());
echo 'Error adding message: ' . $e->getMessage();
}
BX24.callMethod(
'imbot.message.add',
{
BOT_ID: 1291,
DIALOG_ID: 'chat2725',
MESSAGE: 'Select an action',
URL_PREVIEW: 'Y',
KEYBOARD: {
BUTTONS: [
{
TEXT: 'Open Website',
LINK: 'https://www.example.com/',
DISPLAY: 'LINE',
BG_COLOR_TOKEN: 'primary'
},
{
TEXT: 'Task Command',
COMMAND: 'task',
COMMAND_PARAMS: 'task #1',
DISPLAY: 'LINE',
BG_COLOR_TOKEN: 'secondary'
},
{ TYPE: 'NEWLINE' },
{
TEXT: 'Insert Text',
ACTION: 'PUT',
ACTION_VALUE: '/task task #1',
DISPLAY: 'BLOCK',
BG_COLOR_TOKEN: 'alert',
TEXT_COLOR: '#FFFFFF'
},
{
TEXT: 'Neutral Button',
ACTION: 'SEND',
ACTION_VALUE: 'Done',
DISPLAY: 'BLOCK',
BG_COLOR_TOKEN: 'base'
}
]
}
},
function(result) {
if (result.error()) {
console.error(result.error().ex);
} else {
console.log(result.data());
}
}
);
require_once('crest.php');
$result = CRest::call(
'imbot.message.add',
[
'BOT_ID' => 1291,
'DIALOG_ID' => 'chat2725',
'MESSAGE' => 'Select an action',
'URL_PREVIEW' => 'Y',
'KEYBOARD' => [
'BUTTONS' => [
[
'TEXT' => 'Open Website',
'LINK' => 'https://www.example.com/',
'DISPLAY' => 'LINE',
'BG_COLOR_TOKEN' => 'primary'
],
[
'TEXT' => 'Task Command',
'COMMAND' => 'task',
'COMMAND_PARAMS' => 'task #1',
'DISPLAY' => 'LINE',
'BG_COLOR_TOKEN' => 'secondary'
],
['TYPE' => 'NEWLINE'],
[
'TEXT' => 'Insert Text',
'ACTION' => 'PUT',
'ACTION_VALUE' => '/task task #1',
'DISPLAY' => 'BLOCK',
'BG_COLOR_TOKEN' => 'alert',
'TEXT_COLOR' => '#FFFFFF'
],
[
'TEXT' => 'Neutral Button',
'ACTION' => 'SEND',
'ACTION_VALUE' => 'Done',
'DISPLAY' => 'BLOCK',
'BG_COLOR_TOKEN' => 'base'
]
]
]
]
);
echo '<PRE>';
print_r($result);
echo '</PRE>';
How to Update or Remove a Keyboard
To update the keyboard buttons, use the following methods:
To disable the display of buttons, pass:
KEYBOARD: 'N'- an empty value for
KEYBOARD
Command Processing by the Chat Bot
-
To ensure the command works with the keyboard, register it using the imbot.command.register method.
In the button, specify the following keys:
"COMMAND" => "example", // command that will be sent to the chat bot "COMMAND_PARAMS" => "example", // parameters for the command -
Pressing the button will generate the ONIMCOMMANDADD event.
-
Inside the event, the array
data[COMMAND]will contain data about the invoked event. The valueCOMMAND_CONTEXTwill indicate the context in which the command was invoked:TEXTAREA— command entered manuallyKEYBOARD— command invoked by buttonMENU— command invoked from the context menu