API Reference#
You can import as from telegram_text import ...
following elements:
telegram_text.bases#
- telegram_text.bases.NEW_LINE = '\n'#
New line character constant.
- telegram_text.bases.SPACE = ' '#
Space character constant.
AbstractElement#
- class telegram_text.bases.AbstractElement#
Bases:
abc.ABC
The interface every component implements.
- abstract __add__(other: Union[str, telegram_text.bases.Element]) telegram_text.bases.Chain #
Return a new
Chain
object when sum<Element object> + <Element object>
. You can also use it withstr
object, then this method will wrap astr
withPlainText
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.
Element#
- class telegram_text.bases.Element#
Bases:
telegram_text.bases.AbstractElement
,abc.ABC
Base class every component must inherit.
- __add__(other: Union[str, telegram_text.bases.Element]) telegram_text.bases.Chain #
Return a new
Chain
object when sum<Element object> + <Element object>
. You can also use it withstr
object, then this method will wrap astr
withPlainText
object.
- __eq__(other: object) bool #
Equality function to write
<Element object> == <Element object>
.
- __str__() str #
Call
to_markdown()
function.
- abstract to_html() str #
Format the element to html according to Telegram specification.
- abstract to_markdown() str #
Format the element to Markdown/MarkdownV2 format according to Telegram specification with escaping if necessary.
- abstract to_plain_text() str #
Format the element to plain text without escaping, tags or special characters.
Text#
- class telegram_text.bases.Text(text: Union[str, telegram_text.bases.Element])#
Bases:
telegram_text.bases.Element
Basic text component. We don’t recommend you use this outside the code of this module because this class doesn’t include escaping logic, it means Telegram API may reject you.
- to_plain_text() str #
Format the element to plain text without escaping, tags or special characters.
- to_markdown() str #
Return original text without escaping.
- to_html() str #
Format the element to html according to Telegram specification.
PlainText#
- class telegram_text.bases.PlainText(text: Union[str, telegram_text.bases.Element])#
Bases:
telegram_text.bases.Text
Basic text element you can safely use. This element implements escaping logic.
- escaping_chars = ('_', '*', '[', ']', '(', ')', '~', '`', '>', '#', '+', '-', '=', '|', '{', '}', '.', '!')#
Tuple of character that will be escaped according to Telegram specification.
- to_markdown() str #
Format the element to Markdown/MarkdownV2 format according to Telegram specification with escaping if necessary.
- to_html() str #
Format the element to html according to Telegram specification.
- to_plain_text() str #
Format the element to plain text without escaping, tags or special characters.
Chain#
- class telegram_text.bases.Chain(*elements: telegram_text.bases.Element, sep: str = ' ')#
Bases:
telegram_text.bases.Element
Combination between a few elements.
- Parameters
elements (Tuple[Element]) – Any objects with
AbstractElement
interface.sep (str) – The separator between elements.
SPACE
by default.
- __contains__(item: telegram_text.bases.Element) bool #
Magic method to write
if <Element object> in <Chain object>
.
- to_plain_text() str #
Format the element to plain text without escaping, tags or special characters.
- to_markdown() str #
Format the element to Markdown/MarkdownV2 format according to Telegram specification with escaping if necessary.
- to_html() str #
Format the element to html according to Telegram specification.
telegram_text.styles#
Style#
- class telegram_text.styles.Style(text: Union[str, telegram_text.bases.Element])#
Bases:
telegram_text.bases.Element
,abc.ABC
Class that inherits each style. it isolates a part of logic how to render styles.
- Parameters
text (Union[str, telegram_text.bases.Element]) – Text or Element to which the style will be applied.
- to_plain_text() str #
Format the element to plain text without escaping, tags or special characters.
- to_markdown() str #
Format the element to Markdown/MarkdownV2 format according to Telegram specification with escaping if necessary.
- to_html() str #
Format the element to html according to Telegram specification.
Bold#
- class telegram_text.styles.Bold(text: Union[str, telegram_text.bases.Element])#
Bases:
telegram_text.styles.Style
Bold text. Example: bold text.
- to_html() str #
Format the element to html according to Telegram specification.
- to_markdown() str #
Format the element to Markdown/MarkdownV2 format according to Telegram specification with escaping if necessary.
- to_plain_text() str #
Format the element to plain text without escaping, tags or special characters.
Italic#
- class telegram_text.styles.Italic(text: Union[str, telegram_text.bases.Element])#
Bases:
telegram_text.styles.Style
Italic text. Example: italic text.
- to_html() str #
Format the element to html according to Telegram specification.
- to_markdown() str #
Format the element to Markdown/MarkdownV2 format according to Telegram specification with escaping if necessary.
- to_plain_text() str #
Format the element to plain text without escaping, tags or special characters.
Underline#
- class telegram_text.styles.Underline(text: Union[str, telegram_text.bases.Element])#
Bases:
telegram_text.styles.Style
Underline text. Example: underline text.
- to_html() str #
Format the element to html according to Telegram specification.
- to_markdown() str #
Format the element to Markdown/MarkdownV2 format according to Telegram specification with escaping if necessary.
- to_plain_text() str #
Format the element to plain text without escaping, tags or special characters.
Strikethrough#
- class telegram_text.styles.Strikethrough(text: Union[str, telegram_text.bases.Element])#
Bases:
telegram_text.styles.Style
Strikethrough text. Example: strikethrough text.
- to_html() str #
Format the element to html according to Telegram specification.
- to_markdown() str #
Format the element to Markdown/MarkdownV2 format according to Telegram specification with escaping if necessary.
- to_plain_text() str #
Format the element to plain text without escaping, tags or special characters.
Spoiler#
- class telegram_text.styles.Spoiler(text: Union[str, telegram_text.bases.Element])#
Bases:
telegram_text.styles.Style
Spoiler text. We can’t provide an example because it’s a very specific for Telegram messenger formatting.
- to_html() str #
Format the element to html according to Telegram specification.
- to_markdown() str #
Format the element to Markdown/MarkdownV2 format according to Telegram specification with escaping if necessary.
- to_plain_text() str #
Format the element to plain text without escaping, tags or special characters.
InlineCode#
- class telegram_text.styles.InlineCode(text: Union[str, telegram_text.bases.Element])#
Bases:
telegram_text.styles.Style
Inline code text. Example:
inline code
.- to_html() str #
Format the element to html according to Telegram specification.
- to_markdown() str #
Format the element to Markdown/MarkdownV2 format according to Telegram specification with escaping if necessary.
- to_plain_text() str #
Format the element to plain text without escaping, tags or special characters.
Code#
- class telegram_text.styles.Code(text: str, language: Optional[str] = None)#
Bases:
telegram_text.styles.Style
Code block for many lines of text. Telegram doesn’t support frontend language-specific highlights, but according to documentation, it provides an opportunity to specify a language. Perhaps, the Telegram team will add support for this in the future.
- Parameters
text (str) – Text of code block.
language (Optional[str]) – Leave it empty if you don’t want to specify a language.
- to_markdown() str #
Format the element to Markdown/MarkdownV2 format according to Telegram specification with escaping if necessary.
- to_html() str #
Format the element to html according to Telegram specification.
- to_plain_text() str #
Format the element to plain text without escaping, tags or special characters.
telegram_text.elements#
All classes in this file provide similar signatures.
They all have text: str
and
style: Callable[[Union[str, Element]], Element])
arguments.
style
is a factory that will be applied to the element.
Link#
- class telegram_text.elements.Link(text: str, url: str, style: Callable[[Union[str, telegram_text.bases.Element]], telegram_text.bases.Element] = <class 'telegram_text.bases.PlainText'>)#
Bases:
telegram_text.bases.Element
Inline link element.
- Parameters
text (str) – Text of link.
url (str) – Web address on the internet.
style (Callable[[Union[str, Element]], Element]) – Style factory which will be applied to the element.
telegram_text.bases.PlainText
by default.
- to_plain_text() str #
Format the element to plain text without escaping, tags or special characters.
- to_markdown() str #
Format the element to Markdown/MarkdownV2 format according to Telegram specification with escaping if necessary.
- to_html() str #
Format the element to html according to Telegram specification.
InlineUser#
- class telegram_text.elements.InlineUser(text: str, user_id: int, style: Callable[[Union[str, telegram_text.bases.Element]], telegram_text.bases.Element] = <class 'telegram_text.bases.Text'>)#
Bases:
telegram_text.elements.Link
Inline link to user with specific for Telegram url (
tg://user?id={}
).- Parameters
text (str) – Text of link.
user_id (int) – Identifier of user.
style (Callable[[Union[str, Element]], Element]) – Style factory which will be applied to the element.
telegram_text.bases.PlainText
by default.
- to_html() str #
Format the element to html according to Telegram specification.
- to_markdown() str #
Format the element to Markdown/MarkdownV2 format according to Telegram specification with escaping if necessary.
- to_plain_text() str #
Format the element to plain text without escaping, tags or special characters.
User#
- class telegram_text.elements.User(text: str, style: Callable[[Union[str, telegram_text.bases.Element]], telegram_text.bases.Element] = <class 'telegram_text.bases.PlainText'>)#
Bases:
telegram_text.elements._Reference
Link to user by nickname.
- Parameters
text (str) – Nickname of user.
style (Callable[[Union[str, Element]], Element]) – Style factory which will be applied to the element.
telegram_text.bases.PlainText
by default.
- to_html() str #
Format the element to html according to Telegram specification.
- to_markdown() str #
Format the element to Markdown/MarkdownV2 format according to Telegram specification with escaping if necessary.
- to_plain_text() str #
Format the element to plain text without escaping, tags or special characters.
Hashtag#
- class telegram_text.elements.Hashtag(text: str, style: Callable[[Union[str, telegram_text.bases.Element]], telegram_text.bases.Element] = <class 'telegram_text.bases.PlainText'>)#
Bases:
telegram_text.elements._Reference
Hashtag link element.
- Parameters
text (str) – Hashtag name.
style (Callable[[Union[str, Element]], Element]) – Style factory which will be applied to the element.
telegram_text.bases.PlainText
by default.
- to_html() str #
Format the element to html according to Telegram specification.
- to_markdown() str #
Format the element to Markdown/MarkdownV2 format according to Telegram specification with escaping if necessary.
- to_plain_text() str #
Format the element to plain text without escaping, tags or special characters.
telegram_text.markdown#
Telegram markdown and official markdown standards are very different. Telegram markdown has new elements and hasn’t some main old elements from the official markdown format. This file is a compromise that adds missing elements to Telegram.
UnorderedList#
- class telegram_text.markdown.UnorderedList(*elements: telegram_text.bases.Element, style: Callable[[str], telegram_text.bases.Element] = <class 'telegram_text.styles.Bold'>, sep: str = '\n')#
Bases:
telegram_text.bases.Chain
Unordered (bullet) list.
Example:
First element.
Second element.
Third element.
- Parameters
elements (Tuple[Element]) – Elements for the list. Every element is a single point.
style (Callable[[str], Element]) – Style factory that will be applied to every point (only special characters of list, not passed elements).
telegram_text.styles.Bold
by default.sep (str) – Separator between every point.
telegram_text.bases.NEW_LINE
by default.
- to_html() str #
Format the element to html according to Telegram specification.
- to_markdown() str #
Format the element to Markdown/MarkdownV2 format according to Telegram specification with escaping if necessary.
- to_plain_text() str #
Format the element to plain text without escaping, tags or special characters.
OrderedList#
- class telegram_text.markdown.OrderedList(*elements: telegram_text.bases.Element, style: Callable[[str], telegram_text.bases.Element] = <class 'telegram_text.bases.PlainText'>, sep: str = '\n')#
Bases:
telegram_text.bases.Chain
Ordered (numbered) list.
Example:
First element.
Second element.
Third element.
- Parameters
elements (Tuple[Element]) – Elements for the list. Every element is a single point.
style (Callable[[str], Element]) – Style factory that will be applied to every point (only special characters of list, not passed elements).
telegram_text.bases.PlainText
by default.sep (str) – Separator between every point.
telegram_text.bases.NEW_LINE
by default.
- to_html() str #
Format the element to html according to Telegram specification.
- to_markdown() str #
Format the element to Markdown/MarkdownV2 format according to Telegram specification with escaping if necessary.
- to_plain_text() str #
Format the element to plain text without escaping, tags or special characters.
telegram_text.custom#
This file contains unofficial, but very useful markup elements.
TOMLSection#
- class telegram_text.custom.TOMLSection(text: str, *elements: telegram_text.bases.Element, style: Callable[[str], telegram_text.bases.Element] = <class 'telegram_text.styles.InlineCode'>)#
Bases:
telegram_text.bases.Chain
Section element in
.toml
format.- Parameters
text (str) – Section name.
elements (Tuple[Element]) – Sections elements. Separator is
telegram_text.bases.NEW_LINE
.style (Callable[[str], Element]) – Style factory that will be applied to name.
telegram_text.styles.InlineCode
by default.
- to_html() str #
Format the element to html according to Telegram specification.
- to_markdown() str #
Format the element to Markdown/MarkdownV2 format according to Telegram specification with escaping if necessary.
- to_plain_text() str #
Format the element to plain text without escaping, tags or special characters.