Formatting
Telegrinder provides a built-in HTML formatting toolkit. The current entrypoint is HTML plus helper functions from telegrinder.tools.formatting.
This toolkit is designed for building Telegram-ready HTML safely and compositionally instead of concatenating raw tags by hand.
Basic usage
The result is an HTML string object. To send it correctly, use:
For example:
Reference: examples/formatting.py
Available helpers
Core formatting helpers:
bold(...)italic(...)underline(...)strike(...)spoiler(...)code_inline(...)monospace(...)pre_code(...)blockquote(...)date_time(...)link(...)mention(...)tg_emoji(...)escape(...)
Notes:
monospaceis an alias ofcode_inlineblockquote(..., expandable=True)creates an expandable quotepre_code(..., lang="python")adds a language class for code blocks
Helper examples
Escaping
Use escape(...) whenever user-controlled text is inserted into formatted output.
If you use helpers like bold(...) or mention(...), they already operate on safely escaped content, but explicit escape(...) is still a good habit when mixing plain strings and external input.
Template formatting
HTML also supports template-based formatting through Python template strings.
Supported format specifiers map to built-in helper names:
bolditalicunderlinestrikespoilercodemonospaceblockquoteexpandable_blockquote
You can combine them with +:
This is especially convenient when you want interpolation and formatting in one place.
Date and time formatting
Telegram supports special date/time entities, and telegrinder wraps them with date_time(...).
You can pass either a datetime or a Unix timestamp.
Deep links
The formatting package also exports a large set of Telegram deep-link helpers, for example:
tg_bot_start_linktg_bot_startgroup_linktg_public_message_linktg_private_message_linktg_share_linktg_mention_link
These helpers return links, and you can combine them with link(...) or embed them into your own formatted output.
Parse mode constants
If you need parse mode constants explicitly, you can use:
Available values include:
ParseMode.HTMLParseMode.MARKDOWNV2
For the HTML formatter path, HTML.PARSE_MODE is usually the most direct choice.