telegram_rich/lib.rs
1//! # telegram-rich
2//!
3//! Toolkit for Telegram Bot API **rich messages** (`sendRichMessage`, Bot API 10.2+,
4//! types up to date with 10.4).
5//!
6//! - [`html`] *(feature `html`, on by default)*: turns arbitrary, messy web HTML
7//! (RSS items, articles, newsletters) into the HTML subset that Telegram rich
8//! messages accept, resolving relative URLs, lifting images out of paragraphs,
9//! dropping scripts and tracking pixels, and detecting layout tables.
10//! - [`Document`]: an intermediate representation that you can also build by hand,
11//! measure against the official [`Limits`] (32 768 characters, 500 blocks,
12//! 16 nesting levels, 50 media, 20 table columns), truncate at block boundaries
13//! with [`Document::fit`], and render with [`Document::to_html`].
14//! - [`types`]: serde types for `InputRichMessage`, every `InputRichBlock`,
15//! `RichText` entity and `RichMessageButton`, for callers who prefer the
16//! explicit `blocks` representation.
17//!
18//! The crate performs no network I/O and does not depend on any Telegram client;
19//! serialize [`types::InputRichMessage`] into the `rich_message` parameter with
20//! the HTTP client of your choice.
21//!
22//! ```
23//! # #[cfg(feature = "html")] {
24//! use telegram_rich::{Block, FitOptions};
25//! use telegram_rich::html::{normalize, ParseOptions};
26//!
27//! let fit = FitOptions {
28//! tail: vec![Block::link_button("Read original", "https://example.com/post/1")],
29//! truncation_notice: vec![Block::paragraph("The article was truncated.")],
30//! ..FitOptions::default()
31//! };
32//! let msg = normalize("<p>Hello <b>world</b></p>", &ParseOptions::default(), &fit)
33//! .into_input_rich_message();
34//! let json = serde_json::to_string(&msg).unwrap();
35//! assert!(json.starts_with(r#"{"html":"<p>Hello <b>world</b></p><tg-button-row>"#));
36//! # }
37//! ```
38
39#![forbid(unsafe_code)]
40#![warn(missing_docs)]
41#![cfg_attr(docsrs, feature(doc_cfg))]
42
43pub mod document;
44#[cfg(feature = "html")]
45#[cfg_attr(docsrs, doc(cfg(feature = "html")))]
46pub mod html;
47mod limits;
48pub mod types;
49
50pub use document::{
51 Align, Block, ButtonStyle, Document, FitOptions, Inline, ListItem, Media, MediaKind, Table,
52 TableCell, UrlButton, VAlign,
53};
54pub use limits::{Limits, Stats};