API basics
How to call Bot API methods, pass the token, read answers and handle errors.
Request
POST https://<federation domain>/bot/v1/<method>
Authorization: Bearer <token>
Content-Type: application/json
{ ...parameters... }- The body is a JSON object of at most 1 MiB. An empty body means "no parameters".
GETwith parameters in the query string also works (?offset=10&timeout=30): integers are recognized. Pass complex parameters (buttons, commands) withPOSTonly.- Method names are case-sensitive:
sendMessage, notsendmessage. - The address is the same domain as the federation in the app. There is no separate API domain.
Answer
Success is status 200:
{ "ok": true, "result": <the method's result> }An error has a status from the table below and:
{
"ok": false,
"error_code": 409,
"description": "Refused: bot_stopped",
"parameters": { "reason": "bot_stopped" }
}parameters is not always present: it holds retry_after (seconds, for 429) and reason (the
machine-readable reason of a refusal).
Errors
| Status | When |
|---|---|
| 400 | Wrong parameters: a required one missing, the wrong type, text too long, an unknown entity or button. description says what is wrong. |
| 401 | No token, a wrong token or one already replaced, the bot is deleted or suspended together with its owner. |
| 403 | The action is forbidden by rights or moderation (see reason below). |
| 404 | Unknown method, the chat does not exist or the bot is not in it, an interaction older than 15 minutes. |
| 405 | The HTTP method is neither POST nor GET. |
| 409 | The state does not allow it: the person stopped the bot, the reply window closed, a webhook is set when calling getUpdates, data changed concurrently (retry). |
| 413 | The body is larger than 1 MiB. |
| 429 | Too often. Wait parameters.retry_after seconds. |
| 500 | Server error. Details are in the server's log. |
| 503 | The server is temporarily unavailable. Retry later. |
Reasons (parameters.reason)
| Reason | Status | Meaning |
|---|---|---|
bot_stopped | 409 | The person stopped the bot. Wait for a my_chat_member update with status member. |
reply_window_closed | 409 | A personal bot answers more than 24 hours after the other person's last message. |
storage_quota | 409 | The sender ran out of stored-message quota. |
chat_closed | 403 | Moderators closed the chat. |
content_filtered | 403 | The text hits the federation's filter. |
privacy_restricted | 403 | The person's privacy settings do not allow it. |
profile_restricted | 403 | Moderators restricted the bot. |
profile_suspended | 403 | The profile is suspended. |
A 403 is the federation's decision, not a temporary state: do not retry such a request in a loop.
Retries and idempotency
The network may fail after the server has already done the request. So that a retried
sendMessage does not send twice, pass idempotency_key (1 to 128 characters): a retry with the
same key returns the same message.
{ "chat_id": "…", "text": "Build 42 is ready", "idempotency_key": "build-42" }The key is scoped to one bot. The ID of the event the bot answers works well.
Rates
Sending, editing and deleting messages spend the same budget as a person's: 30 actions at once,
then 5 per second. Past it comes 429 with retry_after. Nothing is stored until a request is
accepted.
Server log
The server writes a [bots] line to its log for every 4xx and 5xx answer (bot, method, status),
every webhook failure (bot, host, status or error), every press or suggestion the bot left
unanswered, and the first update of each kind dropped by allowed_updates. The token, the secret
and the webhook URL's path never get into the log. If something does not work, ask the
federation's administrator.