Utilities

bot.utils.cut_text(text, max_length)[source]

Cut text to max_length characters and add ellipsis if text is longer.

Parameters:
  • text (str) – Input text.

  • max_length (int) – Maximum length of the text.

Return type:

str

Returns:

Cut text with ellipsis if it is longer than max_length.

Example:

>>> cut_text("Hello, world!", 5)
'Hello...'
>>> cut_text("Hello, world!", 12)
'Hello, world!'
bot.utils.escape_html(text)[source]

Escape HTML special characters for Telegram HTML mode.

Parameters:

text (str) – Raw text possibly containing &, < and >.

Return type:

str

Returns:

A safe string for Telegram parse_mode=HTML.

Example:

>>> escape_html('<b>Hi & welcome</b>')
'&lt;b&gt;Hi &amp; welcome&lt;/b&gt;'
bot.utils.remove_html_tags(text)[source]

Remove HTML tags from a string.

Parameters:

text (str) – Input text with possible tags like <b>.

Return type:

str

Returns:

Plain text with tags removed.

Example:

>>> remove_html_tags('<b>Hello</b> world')
'Hello world'