pytdbot.utils

class pytdbot.utils.CallbackData(action, data=None)[source]

Bases: object

__init__(action, data=None)[source]
pytdbot.utils.bold(text, html=True, escape=True)[source]

Convert the given text to bold format

Parameters:
  • text (str) – The text to convert

  • html (bool, optional) – If True, returns HTML format, if False returns MarkdownV2. Default is True

  • escape (bool, optional) – Whether escape special characters to the given text or not. Default is True

Returns:

The formated text

Return type:

str

pytdbot.utils.callback_data(action, data=None)[source]

Create callback data for inline buttons

Parameters:
  • action (Any) – Action name, can be any type, but must be JSON serializable

  • data (Any, optional) – Callback data, can be any type, but must be JSON serializable

Returns:

Callback data encoded as bytes

Return type:

bytes

pytdbot.utils.code(text, html=True, escape=True)[source]

Convert the given text to code format

Parameters:
  • text (str) – The text to convert

  • html (bool, optional) – If True, returns HTML format, if False returns MarkdownV2. Default is True

  • escape (bool, optional) – Whether escape special characters to the given text or not. Default is True

Returns:

The formated text

Return type:

str

pytdbot.utils.create_extra_id(bytes_size=9)[source]
Parameters:

bytes_size (int)

pytdbot.utils.create_webapp_secret_key(bot_token)[source]

Create a secret key for Web App data validation

Return type:

bytes

Parameters:

bot_token (str)

Parameters:
bot_token (str):

Bot token

pytdbot.utils.dict_to_obj(dict_obj, client=None)[source]
pytdbot.utils.escape_html(text, quote=True)[source]

Escape HTML characters in the given text

Parameters:
  • text (str) – The text to escape

  • quote (bool, optional) – Whether to escape double quotes. Default is True

Returns:

The escaped text

Return type:

str

pytdbot.utils.escape_markdown(text, version=2)[source]

Escape Markdown characters in the given text

Parameters:
  • text (str) – The text to escape

  • version (int, optional) – The Markdown version to escape. Default is 2

Returns:

The escaped text

Return type:

str

Raises:

ValueError – If the given markdown version is not supported

pytdbot.utils.get_bot_id_from_token(token)[source]
Return type:

str

Parameters:

token (str)

pytdbot.utils.get_retry_after_time(error_message)[source]

Get the retry after time from flood wait error message

Parameters:

error_message (str) – The returned error message from TDLib

Return type:

int

Returns:

py:class:int

pytdbot.utils.get_running_loop()[source]

Get the current running event loop, or create a new one if none is running

Convert the given text to hyperlink format

Parameters:
  • text (str) – The hyperlink text

  • url (str) – The hyperlink url

  • html (bool, optional) – If True, returns HTML format, if False returns MarkdownV2. Default is True

  • escape (bool, optional) – Whether escape special characters to the given text or not. Default is True

Returns:

The formated text

Return type:

str

pytdbot.utils.italic(text, html=True, escape=True)[source]

Convert the given text to italic format

Parameters:
  • text (str) – The text to convert

  • html (bool, optional) – If True, returns HTML format, if False returns MarkdownV2. Default is True

  • escape (bool, optional) – Whether escape special characters to the given text or not. Default is True

Returns:

The formated text

Return type:

str

pytdbot.utils.json_dumps(obj, encode=False, null_terminated=False)[source]
Return type:

Union[str, bytes]

Parameters:
  • encode (bool)

  • null_terminated (bool)

pytdbot.utils.json_loads(obj)[source]
pytdbot.utils.load_callback_data(data)[source]

loads already created callback data by callback_data(). Returns empty CallbackData on error

Return type:

CallbackData

Parameters:

data (bytes)

pytdbot.utils.ltr(text)[source]

Add LTR (Left-to-Right) mark to the given text

Parameters:

text (str) – The text to convert

Returns:

The formated text

Return type:

str

pytdbot.utils.mention(text, user_id, html=True, escape=True)[source]

Convert the given text to inline mention format

Parameters:
  • text (str) – The text of inline mention

  • user_id (str) – The inline user id to mention

  • html (bool, optional) – If True, returns HTML format, if False returns MarkdownV2. Default is True

  • escape (bool, optional) – Whether escape special characters to the given text or not. Default is True

Returns:

The formated text

Return type:

str

pytdbot.utils.obj_to_dict(obj)[source]
pytdbot.utils.obj_to_json(obj, **kwargs)[source]
pytdbot.utils.parse_webapp_data(secret_key, init_data, max_data_age=60)[source]

Parse and validate init data received from Web App

Parameters:
secret_key (bytes):

Secret key for Web App data validation; can be created using pytdbot.utils.create_webapp_secret_key()

init_data (str):

Init data received from Web App

max_data_age (int, optional):

Maximum age of init data in seconds. Default is 60 seconds

Returns:

Parsed data

Return type:

dict

Raises:
  • pytdbot.exception.WebAppDataInvalid

  • pytdbot.exception.WebAppDataOutdated

  • pytdbot.exception.WebAppDataMismatch

Parameters:
  • secret_key (bytes)

  • init_data (str)

  • max_data_age (int)

pytdbot.utils.pre(text, html=True, escape=True)[source]

Convert the given text to pre format

Parameters:
  • text (str) – The text to convert

  • html (bool, optional) – If True, returns HTML format, if False returns MarkdownV2. Default is True

  • escape (bool, optional) – Whether escape special characters to the given text or not. Default is True

Returns:

The formated text

Return type:

str

pytdbot.utils.pre_code(text, language, html=True, escape=True)[source]

Convert the given text to pre-formatted fixed-width code block

Parameters:
  • text (str) – The text to convert

  • language (str) – The name of the programming language written in the given code block

  • html (bool, optional) – If True, returns HTML format, if False returns MarkdownV2. Default is True

  • escape (bool, optional) – Whether escape special characters to the given text or not. Default is True

Returns:

The formated text

Return type:

str

pytdbot.utils.quote(text, expandable=False, html=True, escape=True)[source]

Convert the given text to quote block

Parameters:
  • text (str) – The text to convert

  • expandable (bool, optional) – Wether the quote is expandable or not. Default is False

  • html (bool, optional) – If True, returns HTML format, if False returns MarkdownV2. Default is True

  • escape (bool, optional) – Whether escape special characters to the given text or not. Default is True

Returns:

The formated text

Return type:

str

pytdbot.utils.rtl(text)[source]

Add RTL (Right-to-Left) mark to the given text

Parameters:

text (str) – The text to convert

Returns:

The formated text

Return type:

str

pytdbot.utils.spoiler(text, html=True, escape=True)[source]

Convert the given text to spoiler format

Parameters:
  • text (str) – The text to convert

  • html (bool, optional) – If True, returns HTML format, if False returns MarkdownV2. Default is True

  • escape (bool, optional) – Whether escape special characters to the given text or not. Default is True

Returns:

The formated text

Return type:

str

pytdbot.utils.strikethrough(text, html=True, escape=True)[source]

Convert the given text to strikethrough format

Parameters:
  • text (str) – The text to convert

  • html (bool, optional) – If True, returns HTML format, if False returns MarkdownV2. Default is True

  • escape (bool, optional) – Whether escape special characters to the given text or not. Default is True

Returns:

The formated text

Return type:

str

pytdbot.utils.to_camel_case(input_str, delimiter='.', is_class=True)[source]
Return type:

str

Parameters:
  • input_str (str)

  • delimiter (str)

  • is_class (bool)

pytdbot.utils.underline(text, html=True, escape=True)[source]

Convert the given text to underline format

Parameters:
  • text (str) – The text to convert

  • html (bool, optional) – If True, returns HTML format, if False returns MarkdownV2. Default is True

  • escape (bool, optional) – Whether escape special characters to the given text or not. Default is True

Returns:

The formated text

Return type:

str