Formatting options

Markdown & HTML formatting functions.

Added in version 4.5.1.

telebot.formatting.apply_html_entities(text: str, entities: List | None, custom_subs: Dict[str, str] | None) str

Author: @sviat9440 Updaters: @badiboy, @EgorKhabarov Message: “Test parse _formatting_, [url](https://example.com), [text_mention](tg://user?id=123456) and mention @username”

Example:
apply_html_entities(text, entities)
>> "<b>Test</b> parse <i>formatting</i>, <a href="https://example.com">url</a>, <a href="tg://user?id=123456">text_mention</a> and mention @username"
Custom subs:

You can customize the substitutes. By default, there is no substitute for the entities: hashtag, bot_command, email. You can add or modify substitute an existing entity.

Example:
apply_html_entities(
    text,
    entities,
    {"bold": "<strong class="example">{text}</strong>", "italic": "<i class="example">{text}</i>", "mention": "<a href={url}>{text}</a>"},
)
>> "<strong class="example">Test</strong> parse <i class="example">formatting</i>, <a href="https://example.com">url</a> and <a href="tg://user?id=123456">text_mention</a> and mention <a href="https://t.me/username">@username</a>"
telebot.formatting.escape_html(content: str) str

Escapes HTML characters in a string of HTML.

Parameters:

content (str) – The string of HTML to escape.

Returns:

The escaped string.

Return type:

str

telebot.formatting.escape_markdown(content: str) str

Escapes Markdown characters in a string of Markdown.

Credits to: simonsmh

Parameters:

content (str) – The string of Markdown to escape.

Returns:

The escaped string.

Return type:

str

telebot.formatting.format_text(*args, separator='\n')

Formats a list of strings into a single string.

format_text( # just an example
    mbold('Hello'),
    mitalic('World')
)
Parameters:
  • args (str) – Strings to format.

  • separator (str) – The separator to use between each string.

Returns:

The formatted string.

Return type:

str

telebot.formatting.hbold(content: str, escape: bool | None = True) str

Returns an HTML-formatted bold string.

Parameters:
  • content (str) – The string to bold.

  • escape (bool) – True if you need to escape special characters. Defaults to True.

Returns:

The formatted string.

Return type:

str

telebot.formatting.hcite(content: str, escape: bool | None = True) str

Returns a html-formatted block-quotation string.

Parameters:
  • content (str) – The string to bold.

  • escape (bool) – True if you need to escape special characters. Defaults to True.

Returns:

The formatted string.

Return type:

str

telebot.formatting.hcode(content: str, escape: bool | None = True) str

Returns an HTML-formatted code string.

Parameters:
  • content (str) – The string to code.

  • escape (bool) – True if you need to escape special characters. Defaults to True.

Returns:

The formatted string.

Return type:

str

Hide url of an image.

Parameters:

url (str) – The url of the image.

Returns:

The hidden url.

Return type:

str

telebot.formatting.hitalic(content: str, escape: bool | None = True) str

Returns an HTML-formatted italic string.

Parameters:
  • content (str) – The string to italicize.

  • escape (bool) – True if you need to escape special characters. Defaults to True.

Returns:

The formatted string.

Return type:

str

Returns an HTML-formatted link string.

Parameters:
  • content (str) – The string to link.

  • url (str) – The URL to link to.

  • escape (bool) – True if you need to escape special characters. Defaults to True.

Returns:

The formatted string.

Return type:

str

telebot.formatting.hpre(content: str, escape: bool | None = True, language: str = '') str

Returns an HTML-formatted preformatted string.

Parameters:
  • content (str) – The string to preformatted.

  • escape (bool) – True if you need to escape special characters. Defaults to True.

Returns:

The formatted string.

Return type:

str

telebot.formatting.hspoiler(content: str, escape: bool | None = True) str

Returns an HTML-formatted spoiler string.

Parameters:
  • content (str) – The string to spoiler.

  • escape (bool) – True if you need to escape special characters. Defaults to True.

Returns:

The formatted string.

Return type:

str

telebot.formatting.hstrikethrough(content: str, escape: bool | None = True) str

Returns an HTML-formatted strikethrough string.

Parameters:
  • content (str) – The string to strikethrough.

  • escape (bool) – True if you need to escape special characters. Defaults to True.

Returns:

The formatted string.

Return type:

str

telebot.formatting.hunderline(content: str, escape: bool | None = True) str

Returns an HTML-formatted underline string.

Parameters:
  • content (str) – The string to underline.

  • escape (bool) – True if you need to escape special characters. Defaults to True.

Returns:

The formatted string.

Return type:

str

telebot.formatting.mbold(content: str, escape: bool | None = True) str

Returns a Markdown-formatted bold string.

Parameters:
  • content (str) – The string to bold.

  • escape (bool) – True if you need to escape special characters. Defaults to True.

Returns:

The formatted string.

Return type:

str

telebot.formatting.mcite(content: str, escape: bool | None = True) str

Returns a Markdown-formatted block-quotation string.

Parameters:
  • content (str) – The string to bold.

  • escape (bool) – True if you need to escape special characters. Defaults to True.

Returns:

The formatted string.

Return type:

str

telebot.formatting.mcode(content: str, language: str = '', escape: bool | None = True) str

Returns a Markdown-formatted code string.

Parameters:
  • content (str) – The string to code.

  • escape (bool) – True if you need to escape special characters. Defaults to True.

Returns:

The formatted string.

Return type:

str

telebot.formatting.mitalic(content: str, escape: bool | None = True) str

Returns a Markdown-formatted italic string.

Parameters:
  • content (str) – The string to italicize.

  • escape (bool) – True if you need to escape special characters. Defaults to True.

Returns:

The formatted string.

Return type:

str

Returns a Markdown-formatted link string.

Parameters:
  • content (str) – The string to link.

  • url (str) – The URL to link to.

  • escape (bool) – True if you need to escape special characters. Defaults to True.

Returns:

The formatted string.

Return type:

str

telebot.formatting.mspoiler(content: str, escape: bool | None = True) str

Returns a Markdown-formatted spoiler string.

Parameters:
  • content (str) – The string to spoiler.

  • escape (bool) – True if you need to escape special characters. Defaults to True.

Returns:

The formatted string.

Return type:

str

telebot.formatting.mstrikethrough(content: str, escape: bool | None = True) str

Returns a Markdown-formatted strikethrough string.

Parameters:
  • content (str) – The string to strikethrough.

  • escape (bool) – True if you need to escape special characters. Defaults to True.

Returns:

The formatted string.

Return type:

str

telebot.formatting.munderline(content: str, escape: bool | None = True) str

Returns a Markdown-formatted underline string.

Parameters:
  • content (str) – The string to underline.

  • escape (bool) – True if you need to escape special characters. Defaults to True.

Returns:

The formatted string.

Return type:

str