Skip to content

Utilities

from pytdbot import utils. These format text, pack callback payloads, validate Mini App initData, and build rich messages.

Text

HTML by default; pass html=False for MarkdownV2. Helpers escape special characters unless escape=False.

Helper Role
bold, italic, underline, strikethrough, spoiler Emphasis
code, pre, pre_code Code
hyperlink, mention, custom_emoji Links
quote, rtl, ltr Quote / direction
from pytdbot import utils

text = f"Hello {utils.bold(name)}{utils.hyperlink('site', url)}"
await message.reply_text(text, parse_mode="html")

mention takes (text, user_id). For the sender of a message, prefer message.mention.

Escape raw user text with escape_html or escape_markdown when you interpolate it yourself.

Turn a received FormattedText back into a string with get_formatted_text.

Callback data

An inline button’s data is bytes (max 64). Raw bytes are enough:

types.InlineKeyboardButtonTypeCallback(data=b"ok")

On press, update.text is that payload as UTF-8.

To pack a Python object, use callback_data (action, optional JSON-serializable data) and read it with load_callback_data or update.callback_data. See Keyboards.

Mini Apps

secret = utils.create_webapp_secret_key(bot_token)
data = utils.parse_webapp_data(secret, init_data)

create_webapp_secret_key takes the bot token. parse_webapp_data takes that secret, the Web App initData string, and optional max_data_age (default 60 seconds). It returns a dict, or raises WebAppDataInvalid / WebAppDataOutdated / WebAppDataMismatch.

Other

Helper Role
get_bot_id_from_token Bot id from a token string
get_message_sender_id Id from MessageSenderUser / MessageSenderChat
get_retry_after_time Parse a flood-wait message string. On an Error, use result.limited_seconds instead

Rich HTML builders (heading, paragraph, tg_button, …) live on Rich messages.