Commands
Bot commands, scopes, typed options, conditions and suggestions as the person types.
Commands are /names with descriptions that the app shows when someone types / in a chat with the
bot. As in Discord, a command may have options with types, choices and suggestions from the bot.
Setting commands
curl -s -X POST "$API/setMyCommands" -H "Authorization: Bearer $TOKEN" \
-H 'Content-Type: application/json' -d @- <<'JSON'
{
"commands": [
{ "command": "start", "description": "Start" },
{ "command": "weather", "description": "Weather in a city",
"options": [
{ "name": "city", "description": "Where", "required": true, "autocomplete": true },
{ "name": "days", "type": "integer", "min_value": 1, "max_value": 7 }
] }
]
}
JSON- Names are lowercase Latin letters, digits and
_; descriptions are up to 256 characters. - Up to 100 commands in one scope, at most 64 KiB together.
setMyCommandsreplaces every command of the scope; an empty list deletes them.
The app caches a chat's commands for five minutes, but sees changes at once when the bot makes them.
Scopes
scope | Where it applies |
|---|---|
{"type": "default"} (the default) | Everywhere. |
{"type": "all_private_chats"} | In every direct chat. |
{"type": "all_group_chats"} | In every group and room. |
{"type": "chat", "chat_id": "…"} | In one chat the bot belongs to. |
The most specific scope wins. A bot may have commands in up to 64 scopes.
How a command reaches the bot
An ordinary command. The app sends the command's text as the person's message, which everyone in
the chat sees. The bot gets a message update and, when the command was picked from the list, the
parsed options too:
"command": { "name": "weather", "options": { "city": "Almaty", "days": 3 } }A command typed by hand reaches the bot as plain text, without options. In groups commands reach
the bot in privacy mode too; /command@bot_username reaches only the bot with that name.
An ephemeral command ("ephemeral": true). Nothing is written to the chat, nobody else sees the
invocation, and the bot gets an ephemeral_command with an interaction ID and answers with an
ephemeral message (ephemeral messages).
Options
| Field | Type | |
|---|---|---|
name | string | The option's name. |
description | string | A hint. |
required | boolean | Required. Required options without a condition come first. |
type | string | string (default), integer, number, boolean, user, date. |
choices | array | Up to 25 choices {"name", "value"}; other values are refused. |
autocomplete | boolean | The bot suggests values as the person types (instead of choices). |
min_value, max_value | number | Bounds for integer and number. |
min_length, max_length | integer | Length for string, up to 1024. |
shown_when | object | A condition: the option exists only while another one has one of the values. |
Values reach the bot by option name:
| Type | JSON value | How the person chooses |
|---|---|---|
string | string | Types text. |
integer, number | number | Types a number. |
boolean | true / false | "Yes" or "No". |
user | profile ID | Picks a member of the chat. |
date | string YYYY-MM-DD | Picks a date. |
The server itself refuses an unknown option, a value of the wrong type, out of bounds or not one of the choices, a person outside the chat, and a missing required option: the bot gets only checked values.
Conditions
An option with shown_when appears only while an earlier option has one of the values:
{ "command": "event", "description": "Event", "options": [
{ "name": "kind", "required": true, "choices": [
{ "name": "One-off", "value": "once" }, { "name": "Weekly", "value": "weekly" } ] },
{ "name": "date", "type": "date", "required": true,
"shown_when": { "option": "kind", "values": ["once"] } }
] }Here the date is required only for a one-off event. The server refuses a value of a hidden option.
Suggestions
For an option with "autocomplete": true the app asks the bot what to offer. The bot gets an
autocomplete_query:
{ "id": "…", "from": {...}, "chat": {...}, "command": "weather",
"options": {"days": 3}, "focused": "city", "query": "Al" }and answers with up to 25 choices within 3 seconds, or the list stays empty:
curl -s -X POST "$API/answerAutocompleteQuery" -H "Authorization: Bearer $TOKEN" \
-H 'Content-Type: application/json' \
-d '{"autocomplete_query_id": "…", "choices": [{"name": "Almaty", "value": "Almaty"}, {"name": "Altai", "value": "Altai"}]}'value must fit the option's type and bounds.
How it looks to people
Options are written right in the composer: /weather city:Almaty days:3. Above it the app shows the
command with a pill per option and suggestions for the option under the caret: choices, the bot's
answers, "Yes"/"No", members or a calendar. A wrong value turns red, and sending is refused until it
is fixed.