Skip to main content

Module rest

Module rest 

Source
Expand description

Telegram Bot API REST client — getUpdates long-polling plus sendMessage/editMessageText for outbound delivery.

Rate limiting: v1 implements only per-chat 1 msg/s spacing (enforced by the caller in poller.rs) and reactive 429/retry_after backoff. Full token-bucket accounting for the 20/min-per-group and ~30/s-global tiers is explicitly out of scope (spec §2.5) — mirrors Discord’s rest.rs, which logs 429s and returns an error rather than doing header-driven throttling.

Never log the full request URL: the bot token is embedded in the path (https://api.telegram.org/bot{TOKEN}/...), unlike Discord’s Authorization header. Log method names only, never the URL.

Structs§

TelegramApiError
Error from a Telegram Bot API call, carrying enough of the 429 envelope for the caller to implement per-chat backoff (spec §2.5).

Constants§

POLL_CLIENT_TIMEOUT_SECS 🔒
HTTP client timeout for getUpdates — must exceed POLL_TIMEOUT_SECS with margin, or the client aborts the request right as data would have arrived (spec §9 — long-poll timeout vs HTTP client timeout mismatch).
POLL_TIMEOUT_SECS 🔒
Telegram’s own long-poll wait, in seconds (passed as the timeout query param).
SEND_TIMEOUT_SECS 🔒
Per-request timeout for send/edit calls. Inbound polling and outbound sending run as fully independent tasks (poller.rs), so a stuck send can no longer block polling — this timeout instead bounds how long a single outbound send can delay the next queued send on the same task.
TELEGRAM_API 🔒

Functions§

build_poll_client
Build a dedicated client for getUpdates with a timeout that safely exceeds Telegram’s own 30s long-poll wait. Kept distinct from the client used for sends (spec §9).
edit_message_text
POST /bot{token}/editMessageText
escape_html
Escapes the three characters HTML parse_mode treats specially. Per spec §2.2, Telegram’s HTML mode only requires escaping <, >, & — simpler than MarkdownV2. Order matters: & must be escaped first so the escape sequences for </> aren’t themselves re-escaped.
get_updates
GET /bot{token}/getUpdates
post_for_message 🔒
redact 🔒
Strips the bot token out of an error message before it’s ever logged or returned. reqwest::Error’s Display embeds the request URL on send/connection failures, and the URL contains bot{token} — so {e} must never reach a log line or a Result<_, String> unredacted. This is the single choke point every error-construction site routes through, so callers can’t accidentally skip it.
send_message
POST /bot{token}/sendMessage
send_or_edit
Dispatches to send_message or edit_message_text depending on whether edit_message_id is set (spec §2.3 — streaming-output simulation).