Skip to content

Rich

Rich-message construction: a builder, inline helpers, a Markdown converter, and blocks.

Rich messages are this library's headline feature. Build one with RichMessage plus the inline helpers, convert existing Markdown with markdown_to_rich, or describe it as a list of InputRichBlock* structured blocks (Bot API 10.2); every form feeds Bot.send_rich_message.

InputRichBlockAnchor dataclass

A named anchor that other content can link to.

InputRichBlockAnimation dataclass

A block holding an animation. The media's own caption is ignored.

InputRichBlockAudio dataclass

A block holding a music file. The media's own caption is ignored.

InputRichBlockBlockQuotation dataclass

A block quotation wrapping nested blocks.

InputRichBlockCollage dataclass

A collage of nested blocks.

InputRichBlockDetails dataclass

An expandable details block with an always-visible summary.

InputRichBlockDivider dataclass

A horizontal divider.

InputRichBlockFooter dataclass

A footer line.

InputRichBlockList dataclass

An ordered or unordered list of items.

InputRichBlockListItem dataclass

An item in a list block.

type sets the ordered-list label style and must be "1", "a", "A", "i", or "I"; Telegram rejects the whole message on any other value.

InputRichBlockMap dataclass

A map centered on a location. location is a {latitude, longitude} mapping.

The width and height must not exceed 10000 in total, with a ratio of at most 20.

InputRichBlockMathematicalExpression dataclass

A block mathematical expression in LaTeX format.

InputRichBlockParagraph dataclass

A text paragraph.

InputRichBlockPhoto dataclass

A block holding a photo. The media's own caption is ignored.

InputRichBlockPreformatted dataclass

A preformatted code block, optionally tagged with a language.

InputRichBlockPullQuotation dataclass

A pull quotation with centered text.

InputRichBlockSectionHeading dataclass

A section heading. size runs 1 (largest) to 6 (smallest).

InputRichBlockSlideshow dataclass

A slideshow of nested blocks.

InputRichBlockTable dataclass

A table. cells is a list of rows, each a list of RichBlockTableCell.

InputRichBlockThinking dataclass

A "Thinking…" placeholder. Valid only in send_rich_message_draft.

InputRichBlockVideo dataclass

A block holding a video. The media's own caption is ignored.

InputRichBlockVoiceNote dataclass

A block holding a voice note. The media's own caption is ignored.

InputRichMessageMedia dataclass

A media element referenced from a rich message's html or markdown text.

id is the identifier used in the tg://photo?id=, tg://video?id=, or tg://audio?id= link inside the text; media carries the file itself.

RichBlockCaption dataclass

Caption of a rich block, with an optional cite-style credit.

RichBlockTableCell dataclass

One cell of a table block. An empty cell (no text) renders invisible.

Inline

A run of inline rich text whose .html is already escaped and tag-wrapped.

RichMessage

A builder for a rich message.

Each method appends a block and returns self, so calls chain. Inline content arguments accept plain strings (escaped automatically) or Inline runs from the helpers in this module. Call to_html() for the payload, or pass the builder straight to Bot.send_rich_message.

heading

heading(*content: InlineContent, level: int = 1) -> 'RichMessage'

Add a heading; level 1 renders as h1, anything else as h2.

paragraph

paragraph(*content: InlineContent) -> 'RichMessage'

Add a paragraph of inline content.

quote

quote(*content: InlineContent) -> 'RichMessage'

Add a block quote.

code_block

code_block(content: str, *, language: str | None = None) -> 'RichMessage'

Add a fenced code block, optionally tagged with a language.

math_block

math_block(expression: str) -> 'RichMessage'

Add a centered block-math expression.

rule

rule() -> 'RichMessage'

Add a horizontal rule.

table

table(header: list[InlineContent], rows: list[list[InlineContent]]) -> 'RichMessage'

Add a bordered, striped table from a header row and data rows.

Rows shorter than the header are padded with empty cells; extra cells beyond the header width are dropped.

collapsible

collapsible(summary: InlineContent, body: 'RichMessage', *, expanded: bool = False) -> 'RichMessage'

Add a collapsible section (details/summary) wrapping a nested message.

footer

footer(*content: InlineContent) -> 'RichMessage'

Add a footer line.

raw

raw(html_fragment: str) -> 'RichMessage'

Append a pre-built HTML fragment verbatim (escape hatch; not escaped).

to_html

to_html() -> str

Render the accumulated blocks to the rich-message HTML string.

bold

bold(*parts: InlineContent) -> Inline

Bold inline text.

code

code(content: str) -> Inline

Inline monospace code (literal text, not further formatted).

italic

italic(*parts: InlineContent) -> Inline

Italic inline text.

link(content: InlineContent, url: str) -> Inline

A hyperlink whose visible text is content and whose target is url.

mark

mark(*parts: InlineContent) -> Inline

Highlighted (marked) inline text.

math

math(expression: str) -> Inline

Inline math (a tg-math expression).

spoiler

spoiler(*parts: InlineContent) -> Inline

Spoiler-hidden inline text.

strike

strike(*parts: InlineContent) -> Inline

Strikethrough inline text.

sub

sub(*parts: InlineContent) -> Inline

Subscript inline text.

sup

sup(*parts: InlineContent) -> Inline

Superscript inline text.

text

text(*parts: InlineContent) -> Inline

Combine parts into one inline run (plain strings are escaped).

underline

underline(*parts: InlineContent) -> Inline

Underlined inline text.

markdown_to_rich

markdown_to_rich(text: str) -> str

Convert a Markdown string to rich-message HTML.