Bot API: buttons, keyboards and commands

SENSEVER

The official Sensever channel: guides and API documentation for Chat, Requests, Flow, Studio, bots and messaging channels.

7 মিনিটে পড়া

Bot API: buttons, keyboards and commands

A bot can run a conversation without any typing: inline buttons below a message, a keyboard below the input field, a command menu on "/" and a "Start" button. This article describes them, as well as the bot profile and links with a start parameter.

Only a bot can attach buttons and keyboards: a person's message with them is rejected (403 buttons_bots_only).

Inline buttons

Buttons below a message are set in payload.buttons of a text or media message as rows of buttons:

{
  "client_message_id": "c3a9f4d2-6b1e-4f0a-9c8d-7e6f5a4b3c2d",
  "kind": "text",
  "text": "Confirm order #42?",
  "payload": {"buttons": [
    [{"text": "Yes", "callback_data": "order:42:yes"}, {"text": "No", "callback_data": "order:42:no"}],
    [{"text": "Open order", "url": "https://shop.example/orders/42"}],
    [{"text": "Catalog", "web_app": {"url": "https://shop.example/app"}}]
  ]},
  "send_as": "self"
}

InlineKeyboardButton

A button is an object with exactly two fields: text and one of callback_data, url, web_app.

Field

Type

Description

text

String

Label, 1–64 characters, no control characters

callback_data

String

Data for the bot, 1–64 bytes. A tap sends the bot a callback_query update

url

String

An http or https link, up to 2048 characters; opens in a new tab

web_app

Object

{"url": "https://…"} — the bot's Mini App, see "Bot API: Mini Apps"

From 1 to 8 rows, with 1 to 8 buttons per row. An invalid button returns 422 validation_failed with a hint in details.fields["payload.buttons"].

To change or remove buttons, edit the message: PATCH with a new payload (without buttons, there will be no buttons).

Tap and answer

  1. A person taps a button with callback_data. The button shows a loading state.

  2. The bot receives a callback_query update: the tap id, data and user, and in message, the message with the button.

  3. The bot answers with answerCallbackQuery. The client waits up to 10 seconds for the answer and shows it: a short notification, a dialog, or it opens a link.

Answer even if there is nothing to show: this way the button stops waiting sooner.

Only a button currently in the message can be tapped, and only by a person who can see that message. If the message is deleted or the button is gone, the person sees an error and the bot gets no update. In a group or channel, the bot must still be a member; in a direct chat or project request, it must still be able to hear this person.

answerCallbackQuery

POST /v1/chat/bot/callbacks/{id}/answer

Parameter

Type

Required

Description

text

String

No

Notification text, up to 200 characters

show_alert

Boolean

No

true shows a dialog the person closes themselves; by default, a short notification

url

String

No

An http or https link the client opens in a new tab

Response: 204 No Content. You can answer once, within 15 minutes of the tap. Errors: 404 not_found — no such tap, or it is not for this bot; 409 callback_already_answered_or_expired; 422 validation_failed.

Keyboard below the input field

The keyboard replaces typing with ready-made replies. It is set in the payload.reply_keyboard field of a bot message:

{
  "client_message_id": "1e2d3c4b-5a69-4788-9aab-bccddeeff001",
  "kind": "text",
  "text": "How can I help?",
  "payload": {"reply_keyboard": {
    "rows": [[{"text": "Order status"}, {"text": "Agent"}], [{"text": "Send phone number", "request_contact": true}]],
    "one_time": true,
    "placeholder": "Choose an option"
  }},
  "send_as": "self"
}

ReplyKeyboard

Field

Type

Required

Description

rows

Array of Array of KeyboardButton

Yes

From 1 to 12 rows, with 1 to 8 buttons per row

one_time

Boolean

No

Hide the keyboard once the person sends a message

placeholder

String

No

Hint in the input field, up to 64 characters

To remove the keyboard, send a message with "reply_keyboard": {"remove": true}.

KeyboardButton

Field

Type

Description

text

String

Label, 1–64 characters. A tap sends this text as a regular message from the person

request_contact

Boolean

Optional, only true: the button asks the person for their phone number

The active keyboard is the one from the bot's latest message that set or removed it. A tap reaches the bot as a regular message update with the button text.

Phone number: request_contact

A request_contact button works in a direct chat with the bot. The person taps it and confirms sending. The bot receives a message update with message.payload.contact:

Field

Type

Description

ref

String

The person's p.<number>

phone

String

The number verified in the Sensever account

name

String

The person's name

The server fills in the number: a person cannot send someone else's or a made-up number, and a bot cannot forge the contact field. If the person has no verified number, nothing is sent.

Commands

A command is a message that starts with /: /start, /help, /order 42. The bot receives it as a regular message update. The list of commands with descriptions is shown when the person types "/" in the input field, and via the "Menu" button next to the field. Commands in the text of bot messages become buttons that send the command.

setMyCommands

PUT /v1/chat/bot/commands
{"commands": [{"command": "start", "description": "Start over"}, {"command": "orders", "description": "My orders"}], "scope": "private", "language_code": "ru"}

Parameter

Type

Required

Description

commands

Array of BotCommand

Yes

Up to 100 commands; an empty list deletes the menu for this scope and language

scope

String

No

default (default) — everywhere; private — direct chats

language_code

String

No

Two-letter language code (ru, en); empty means all languages

BotCommand has exactly two fields: command — 1–32 characters of [a-z0-9_], without /; description — 1–256 characters. Commands in the list must not repeat. Response: 204 No Content.

getMyCommands and deleteMyCommands

GET    /v1/chat/bot/commands?scope=private&language_code=ru
DELETE /v1/chat/bot/commands

GET returns {"commands": [BotCommand, …]} — exactly the list saved for this scope and language, with no fallbacks. DELETE takes optional scope and language_code in the body and deletes that list; the response is 204 No Content.

Which menu a person sees

  • Direct chat: the first match among private in the person's interface language, then private without a language, then default in their language, then default without a language.

  • Project request: only default: in the person's language, then without a language.

  • Group: the default lists of all bots in the group. If there are several bots, the bot name is appended to the command: /start@shop_bot. In privacy mode, such a command goes only to that bot.

The "Start" button and links with a parameter

An empty chat with a bot shows the bot's description and a "Start" button that sends /start.

A link to a bot with a parameter:

https://sensever.com/@shop_bot?start=promo_42

It opens the bot's page; "Open" leads to the chat, where "Start" sends /start promo_42. The bot receives this as a regular message and parses the parameter itself. The parameter is 1–64 characters from A–Z, a–z, 0–9, _, -; any other parameter is dropped. The same link straight into the chat: https://sensever.com/?write=p.812&start=promo_42, where 812 is the bot_id.

Bot profile

A bot has a name, username, avatar, description and short description. The owner changes the name and avatar; both the owner and the bot itself can change the descriptions.

  • Description (up to 512 characters) is shown in an empty chat with the bot above the "Start" button.

  • Short description (up to 120 characters) is a line on the bot's page https://sensever.com/@shop_bot.

getMe

GET /v1/chat/bot/me

Response: {"id": "812", "username": "shop_bot", "name": "Shop assistant", "delivery": "polling"}; delivery is polling or webhook.

Profile

GET /v1/chat/bot/profile
PUT /v1/chat/bot/profile

GET returns a BotProfile object:

Field

Type

Description

id

String

bot_id

username

String

Username ending in _bot

name

String

Display name

description

String or null

Description

short_description

String or null

Short description

avatar_url

String or null

Public avatar URL

PUT takes description and short_description (both optional; null or an empty string clears the value) and returns BotProfile. Any other fields in the body return 422 validation_failed.

মন্তব্য

লেখকগণ