telegram-rich
Rust toolkit for Telegram Bot API rich messages (sendRichMessage, introduced in
Bot API 10.2; types up to date with Bot API 10.4).
Telegram rich messages render headings, lists, tables, block quotes, collapsible
details, formulas, media and buttons natively — but they only accept a specific
HTML subset and enforce hard limits. Feeding them raw web HTML from RSS items or
articles fails or renders badly. telegram-rich bridges that gap:
- HTML normalizer — turns arbitrary, messy web HTML into Telegram Rich HTML:
resolves relative URLs against a base URL, lifts images/videos out of paragraphs
and links into standalone media blocks, handles lazy-loaded images
(
data-src,srcset), drops scripts/iframes/forms/hidden elements and 1×1 tracking pixels, unwraps layout tables, maps<figure>/<figcaption>/<cite>to captioned media, preserves code languages, and extracts TeX from MathML. - Limit-aware truncation — measures documents against the official limits (32 768 characters, 500 blocks, 16 nesting levels, 50 media, 20 table columns) and cuts at block boundaries, descending into lists, quotes, details and tables and cutting text mid-paragraph only as a last resort. Reserve room for a "Read original" button row and an optional truncation notice.
- Typed document model — build documents by hand, rewrite media URLs
(e.g. to
tg://photo?id=…for explicit uploads) and render to Rich HTML. - Serde types —
InputRichMessage,InputRichMessageMedia, everyInputRichBlock, everyRichTextentity andRichMessageButton, mirroring the official documentation one-to-one, for callers who prefer explicit blocks.
No network I/O, no Telegram client dependency — plug the result into the HTTP client or bot framework of your choice.
Usage
[]
= "0.0.1"
use ;
use ;
let article_url = "https://example.com/post/1";
let parse = with_base_url;
let fit = FitOptions ;
let out = normalize;
assert!;
// `rich_message` parameter of sendRichMessage:
let rich_message = to_value.unwrap;
Building blocks explicitly with the serde types:
use ;
let msg = blocks;
Cargo features
| Feature | Default | Description |
|---|---|---|
html |
✓ | HTML normalizer (telegram_rich::html), pulls in scraper and url. Disable it to use only the types and the document model. |
Status
Early development (0.0.x): the API may change between releases. Feedback and
real-world HTML samples that render poorly are very welcome.
License
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or https://opensource.org/licenses/MIT)
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.