Docs

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".
  • GET with parameters in the query string also works (?offset=10&timeout=30): integers are recognized. Pass complex parameters (buttons, commands) with POST only.
  • Method names are case-sensitive: sendMessage, not sendmessage.
  • 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

StatusWhen
400Wrong parameters: a required one missing, the wrong type, text too long, an unknown entity or button. description says what is wrong.
401No token, a wrong token or one already replaced, the bot is deleted or suspended together with its owner.
403The action is forbidden by rights or moderation (see reason below).
404Unknown method, the chat does not exist or the bot is not in it, an interaction older than 15 minutes.
405The HTTP method is neither POST nor GET.
409The 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).
413The body is larger than 1 MiB.
429Too often. Wait parameters.retry_after seconds.
500Server error. Details are in the server's log.
503The server is temporarily unavailable. Retry later.

Reasons (parameters.reason)

ReasonStatusMeaning
bot_stopped409The person stopped the bot. Wait for a my_chat_member update with status member.
reply_window_closed409A personal bot answers more than 24 hours after the other person's last message.
storage_quota409The sender ran out of stored-message quota.
chat_closed403Moderators closed the chat.
content_filtered403The text hits the federation's filter.
privacy_restricted403The person's privacy settings do not allow it.
profile_restricted403Moderators restricted the bot.
profile_suspended403The 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.

On this page