Testing the Telegram Bot API, where the token is part of the URL

Telegram bots are popular for deploy notifications, on-call alerts and small internal tools, and the Bot API is about as simple as an API gets. It has two habits worth knowing: the token is part of the URL path, and like Slack, the body's ok field is the real result.

Step 1: A bot and an environment

Talk to @BotFather, /newbot, and copy the token. Create a test group, add the bot, and send a message in the group so the bot can see it.

VariableValue
tgBasehttps://api.telegram.org/bot{{tgToken}}
tgToken123456:ABC-…, marked as secret
chatIdthe test group's id (negative for groups)

Because the token is in the path, the variable is what keeps it out of exported collections and shared requests. Requestarium masks secret values in history; don't copy the resolved URL into a ticket.

Step 2: Check the token

HTTP
GET {{tgBase}}/getMe
JavaScript
pm.test('bot token works', () => {
  const body = pm.response.json();
  pm.expect(body.ok).to.equal(true);
  pm.expect(body.result.is_bot).to.equal(true);
});

Find chatId with GET {{tgBase}}/getUpdates after sending a message in the group (this only works while no webhook is set).

Step 3: Send and edit a message

HTTP
POST {{tgBase}}/sendMessage
Content-Type: application/json

{"chat_id": "{{chatId}}", "text": "*Deploy finished*: orders-api v1.4.2", "parse_mode": "MarkdownV2"}
JavaScript
const body = pm.response.json();
pm.test('sent', () => pm.expect(body.ok).to.equal(true));
pm.environment.set('messageId', String(body.result.message_id));

That request fails: in MarkdownV2, . and - must be escaped. The answer is a 400 with "description": "Bad Request: can't parse entities: …". Send orders\\-api v1\\.4\\.2, or use "parse_mode": "HTML", which is easier to get right.

HTTP
POST {{tgBase}}/editMessageText
Content-Type: application/json

{"chat_id": "{{chatId}}", "message_id": {{messageId}}, "text": "Deploy finished (edited)"}

Step 4: Webhooks

HTTP
POST {{tgBase}}/setWebhook
Content-Type: application/json

{"url": "https://bot.example.com/telegram", "secret_token": "{{tgWebhookSecret}}", "allowed_updates": ["message"]}
HTTP
GET {{tgBase}}/getWebhookInfo
JavaScript
const info = pm.response.json().result;
pm.test('webhook healthy', () => {
  pm.expect(info.url).to.equal('https://bot.example.com/telegram');
  pm.expect(info.pending_update_count).to.be.below(10);
  pm.expect(info.last_error_message).to.not.exist;
});

getWebhookInfo is a good monitoring request: last_error_message tells you Telegram can't reach your server before your users do. Telegram sends your secret_token in the X-Telegram-Bot-Api-Secret-Token header, and your handler should check it. See testing incoming webhooks.

Step 5: Errors and limits

error_codeTypical description
400Bad Request: chat not found, can't parse entities
401Unauthorized: wrong token
403Forbidden: bot was kicked from the group chat
429Too Many Requests: retry after 5
JavaScript
if (pm.response.code === 429) {
  console.log('retry after', pm.response.json().parameters.retry_after, 's');
}

Telegram allows roughly one message per second per chat and about 20 per minute to a group. Use --delay in runs that post.