Expand description
An outbound Telegram Bot API notifier.
This module exists to replace the hand-rolled sendMessage calls which had
been copied between projects, each with a different subset of the hard parts
missing. It is send-only on purpose: it has no polling, no webhooks, and no
update handling, because none of the consuming projects receive anything.
§Formatting without escaping
Messages are built from Message::builder, which emits Telegram
entities rather than parse_mode markup. Telegram’s own documentation
describes entities as what a Markdown or HTML parser is converted into, so
nothing is lost by skipping that step — and because no markup is ever
embedded in the text, interpolated values never need escaping:
use webserver_base::telegram::{ChatId, Message, ReqwestTelegram, Telegram, TelegramSettings};
let settings = TelegramSettings::builder("123456789:AA...").build()?;
let telegram = ReqwestTelegram::new(settings, None)?;
telegram.send(
ChatId::Id(1234),
Message::builder()
.text("🎨 ")
.bold("New pattern")
.text("\nInput: ")
.code(untrusted_filename) // no escaping, ever
.build(),
);§What it handles
- Length. Telegram caps text at 4096 and captions at 1024 UTF-16 code units. Oversized messages are split at natural boundaries, with entities clamped and rebased onto each piece, capped at a configurable number of chunks so an upstream bug cannot become a flood.
- Rate limits. Sends are queued per chat and paced at Telegram’s documented limits: one message per second per chat, thirty per second overall.
- Retries. A
429is retried after theretry_afterTelegram supplies, up to a ceiling; transient failures back off exponentially; permanent client errors are never retried, and misconfiguration is logged loudly. - Secrets. The bot token is a path segment of every request URL, so it
is held in a
BotTokenwhich refuses to print itself, and everyreqwesterror has its URL stripped before it can reach a log.
Structs§
- BotToken
- A Telegram bot token.
- Entity
- A styled span of a message, positioned in UTF-16 code units.
- Inline
Builder - Builds the contents of a block-level entity, such as a blockquote.
- Message
- A fully constructed message, ready to send.
- Message
Builder - Builds a
Messagefrom styled spans. - Mock
Telegram - A
Telegramwhich records messages instead of sending them. - Reqwest
Telegram - A
Telegramwhich really talks to the Bot API over HTTP. - Send
Options - Optional per-send parameters.
- Sent
Message - One message captured by
MockTelegram. - Style
- A set of inline text styles which can be combined.
- Telegram
Settings - Configuration for a
ReqwestTelegram. - Telegram
Settings Builder - Builds a
TelegramSettings.
Enums§
- ChatId
- Identifies the chat a message is sent to.
- Entity
Kind - The kind of a message entity, including any data it carries.
- File
Source - Where the bytes of an uploaded photo or document come from.
- Media
- An attachment carried alongside a message’s caption.
- Telegram
Error - Every way a Telegram send can fail.
Constants§
- DEFAULT_
CONNECT_ TIMEOUT - Default timeout for establishing a connection.
- DEFAULT_
GLOBAL_ PER_ SECOND - Default ceiling on messages per second across all chats.
- DEFAULT_
MAX_ CHUNKS - Default ceiling on how many messages one oversized message may become.
- DEFAULT_
MAX_ INPUT_ BYTES - Default ceiling on the size of a single message, in bytes.
- DEFAULT_
MAX_ RETRIES - Default number of retries for transient failures.
- DEFAULT_
MAX_ RETRY_ AFTER - Default ceiling on an honored
retry_after. - DEFAULT_
PER_ CHAT_ INTERVAL - Default minimum spacing between two messages to the same chat.
- DEFAULT_
QUEUE_ CAPACITY - Default number of messages held per chat before new sends are dropped.
- DEFAULT_
REQUEST_ TIMEOUT - Default timeout for a complete request.
- MAX_
CAPTION_ LENGTH - Telegram’s maximum media caption length, in UTF-16 code units.
- MAX_
TEXT_ LENGTH - Telegram’s maximum message text length, in UTF-16 code units.
- TELEGRAM_
API_ BASE_ URL - The official Telegram Bot API host.
Traits§
- Telegram
- Sends outbound Telegram notifications.