Module API Usage#

Any element from this module inherent following interface:

class telegram_text.bases.AbstractElement

Bases: ABC

The interface every component implements.

abstract __add__(other: str | Element) Chain

Return a new Chain object when sum <Element object> + <Element object>. You can also use it with str object, then this method will wrap a str with PlainText object.

abstract to_plain_text() str

Format the element to plain text without escaping, tags or special characters.

abstract to_markdown() str

Format the element to Markdown/MarkdownV2 format according to Telegram specification with escaping if necessary.

abstract to_html() str

Format the element to html according to Telegram specification.

Let’s discuss with examples every function.

__add__#

This is a magic method that let us sum elements e.g.

>>> text = Bold("Hello") + Italic("world") + "!!!"
>>> text
<Chain: <Bold: 'Hello'> + <Italic: 'world'> + <PlainText: '!!!'>, sep=' '>

Note

You can sum any element object with str, then str will be wrapped with telegram_text.bases.PlainText, as in this case with "!!!".

As a result, we get a telegram_text.bases.Chain object.

telegram_text.bases.Chain#

Chain is a combination of a few elements. We get this element when summing a few elements. This element has the same interface and it let us use following functions:

to_plain_text()#

Format the element to plain text without escaping, tags or special characters. It’s a good method for debugging or logging, but don’t use it with Telegram API because it doesn’t have escaping, so Telegram API may reject you.

Note

If you have some element with styles and you want to send it to Telegram without styling just use this construction:

>>> PlainText(text).to_markdown()
'Hello world \\!\\!\\!'

It’ll add an escaping.

to_markdown()#

This function formats the element to Markdown/MarkdownV2 format according to Telegram specification with escaping if necessary.

>>> text.to_markdown()
'*Hello* _world_ \\!\\!\\!'

to_html()#

This function formats the element to html according to Telegram specification.

>>> text.to_html()
'<b>Hello</b> <i>world</i> !!!'

To discover all available elements see API Reference.