Updates
How a bot receives events through long polling or a webhook, how to verify the signature, and every kind of update.
Everything that happens to a bot arrives as updates. The server keeps them in the bot's queue, so they survive restarts of the server and of the bot. You can receive them in one of two ways, long polling or a webhook, but not both at once.
Each update is an object with a number and exactly one kind field:
{ "update_id": 42, "message": { ... } }Numbers grow in order. The queue keeps updates for 24 hours and at most 100,000 per bot; the oldest are dropped first.
Long polling: getUpdates
curl -s -X POST "$API/getUpdates" -H "Authorization: Bearer $TOKEN" \
-H 'Content-Type: application/json' \
-d '{"offset": 43, "timeout": 50, "allowed_updates": ["message", "callback_query"]}'| Parameter | Type | Meaning |
|---|---|---|
offset | integer | The first update you want. Every lower number counts as received and is deleted. Pass the last processed update_id + 1. |
limit | integer | How many to return, 1 to 100. 100 by default. |
timeout | integer | Seconds to wait when the queue is empty, 0 to 50. 0 by default (answer at once). |
allowed_updates | list of strings | Which kinds to receive. Remembered for later requests and for the webhook. Without it the previous list applies, or every kind if none was ever given. |
The result is an array of updates (possibly empty). A new getUpdates of the same bot ends the
previous waiting request with an empty answer, so keep one polling loop per bot.
When a webhook is set, getUpdates answers 409: delete the webhook (deleteWebhook) to poll.
Webhook: setWebhook
The server sends each update to the bot's address in a POST request.
curl -s -X POST "$API/setWebhook" -H "Authorization: Bearer $TOKEN" \
-H 'Content-Type: application/json' \
-d '{"url": "https://bot.example.org/comon", "secret": "a-long-random-secret-0123"}'| Parameter | Type | Meaning |
|---|---|---|
url | string | An HTTPS address on a public server. Required. |
secret | string | 16 to 256 characters: letters, digits, _ and -. With it every request is signed. |
allowed_updates | list of strings | Which kinds to receive. |
drop_pending_updates | true | Delete the updates already waiting. |
How the server delivers:
- one update per request, in order: the next goes only when the previous one is accepted;
- the body is the update's JSON,
Content-Type: application/json; - a 2xx answer means delivered. Any other status, a 3xx redirect, a connection error or 10 seconds without an answer is a failure; the server retries with a growing pause of up to a minute until it succeeds or the update expires;
- the address must lead to a public IP: private addresses,
localhostand IPv6 forms carrying an IPv4 address (NAT64, 6to4, Teredo) are forbidden. The name is checked onsetWebhookand before every post: if it now points at a private address, the update waits. The certificate is checked against the name in the address.
deleteWebhook (with an optional drop_pending_updates) removes the webhook. getWebhookInfo
shows the current state:
{ "url": "https://bot.example.org/comon", "has_secret": true, "pending_update_count": 0,
"last_error_date": 1793050000, "last_error_message": "HTTP status 502" }Verifying the signature
With a secret every request carries the header:
X-Comon-Signature: t=1793050000,v1=5f2b…v1 is the hex HMAC-SHA256 of the string <t>.<request body>, keyed by secret. Verify it over
the raw body bytes, before parsing the JSON, and reject old t values so an intercepted request
cannot be replayed.
import hashlib, hmac, time
def verify(secret: str, header: str, body: bytes, tolerance: int = 300) -> bool:
parts = dict(item.split("=", 1) for item in header.split(","))
t, v1 = parts.get("t", ""), parts.get("v1", "")
if not t.isdigit() or abs(time.time() - int(t)) > tolerance:
return False
expected = hmac.new(secret.encode(), t.encode() + b"." + body, hashlib.sha256).hexdigest()
return hmac.compare_digest(expected, v1)Kinds of updates
| Kind | When it arrives |
|---|---|
message | A new message in a direct chat, group or room that the bot hears. |
edited_message | It was edited. |
deleted_messages | Messages were deleted. |
channel_post | A new post in a channel the bot belongs to. |
callback_query | Someone pressed a button with callback_data. |
ephemeral_command | Someone ran an ephemeral command. |
autocomplete_query | Someone is filling in an option with suggestions. |
my_chat_member | The bot was added to or removed from a chat, stopped or restarted. |
personal_connection | Someone connected or disconnected the bot as their personal bot. |
personal_message | A new message in a direct chat of the person who connected the bot. |
edited_personal_message | It was edited. |
deleted_personal_messages | Messages in such a chat were deleted. |
The shapes of User, Chat and Message are in objects.
message, edited_message, channel_post
A Message. An edited channel post also arrives as channel_post, with
"edited": true. When it is an ordinary command with options run from the app, it also has
command:
{
"update_id": 7,
"message": {
"message_id": "…", "chat": {"id": "…", "type": "group", "title": "Team"},
"from": {"id": "…", "is_bot": false, "first_name": "Anna"},
"date": 1793050000, "order": 118,
"text": "/remind when:2026-11-01 what:release",
"command": {"name": "remind", "options": {"when": "2026-11-01", "what": "release"}}
}
}deleted_messages
{ "chat": {"id": "…", "type": "private", "user": {...}}, "message_ids": ["…", "…"] }callback_query
{
"id": "…",
"from": {...}, "chat": {...},
"message": {...},
"ephemeral_message_id": "…",
"button_id": "…",
"data": "more"
}message is the message with the button (when a button of an ordinary message was pressed),
ephemeral_message_id is present when a button of an ephemeral message was pressed. Answer with
answerCallbackQuery within 10 seconds (buttons).
ephemeral_command
{
"id": "…", "from": {...}, "chat": {...},
"text": "/secret hello", "command": "secret", "args": "hello",
"options": {"to": "<profile id>"},
"date": 1793050000
}id is the interaction ID: answer with an ephemeral message within 15 minutes
(ephemeral messages).
autocomplete_query
{
"id": "…", "from": {...}, "chat": {...},
"command": "weather", "options": {"country": "KZ"},
"focused": "city", "query": "Al"
}Answer with answerAutocompleteQuery within 3 seconds (commands).
my_chat_member
{ "chat": {...}, "from": {...}, "status": "member", "date": 1793050000 }status | When |
|---|---|
member | The bot was added to a group, channel or community, or a person restarted the bot in a direct chat. |
left | The bot was removed from a group, channel or community. chat then holds only id. |
stopped | A person stopped the bot in a direct chat. |
from is present only for a stop or restart in a direct chat: the person who did it.
personal_connection, personal_message
See personal bots.