Utils#

util file#

telebot.util.antiflood(function: Callable, *args, **kwargs)#

Use this function inside loops in order to avoid getting TooManyRequests error. Example:

from telebot.util import antiflood
for chat_id in chat_id_list:
msg = antiflood(bot.send_message, chat_id, text)
Parameters
  • function (Callable) – The function to call

  • args (tuple) – The arguments to pass to the function

  • kwargs (dict) – The keyword arguments to pass to the function

Returns

None

telebot.util.chunks(lst, n)#

Yield successive n-sized chunks from lst.

telebot.util.content_type_media = ['text', 'audio', 'animation', 'document', 'photo', 'sticker', 'video', 'video_note', 'voice', 'contact', 'dice', 'poll', 'venue', 'location']#

Contains all media content types.

telebot.util.content_type_service = ['new_chat_members', 'left_chat_member', 'new_chat_title', 'new_chat_photo', 'delete_chat_photo', 'group_chat_created', 'supergroup_chat_created', 'channel_chat_created', 'migrate_to_chat_id', 'migrate_from_chat_id', 'pinned_message', 'proximity_alert_triggered', 'video_chat_scheduled', 'video_chat_started', 'video_chat_ended', 'video_chat_participants_invited', 'message_auto_delete_timer_changed']#

Contains all service content types such as User joined the group.

telebot.util.escape(text: str) str#

Replaces the following chars in text (’&’ with ‘&amp;’, ‘<’ with ‘&lt;’ and ‘>’ with ‘&gt;’).

Parameters

text – the text to escape

Returns

the escaped text

telebot.util.extract_arguments(text: str) str#

Returns the argument after the command.

Examples:#
extract_arguments("/get name"): 'name'
extract_arguments("/get"): ''
extract_arguments("/get@botName name"): 'name'
Parameters

text (str) – String to extract the arguments from a command

Returns

the arguments if text is a command (according to is_command), else None.

Return type

str or None

telebot.util.extract_command(text: str) Optional[str]#

Extracts the command from text (minus the ‘/’) if text is a command (see is_command). If text is not a command, this function returns None.

Examples:#
extract_command('/help'): 'help'
extract_command('/help@BotName'): 'help'
extract_command('/search black eyed peas'): 'search'
extract_command('Good day to you'): None
Parameters

text (str) – String to extract the command from

Returns

the command if text is a command (according to is_command), else None.

Return type

str or None

telebot.util.generate_random_token() str#

Generates a random token consisting of letters and digits, 16 characters long.

Returns

a random token

Return type

str

telebot.util.is_bytes(var) bool#

Returns True if the given object is a bytes object.

Parameters

var (object) – object to be checked

Returns

True if the given object is a bytes object.

Return type

bool

telebot.util.is_command(text: str) bool#

Checks if text is a command. Telegram chat commands start with the ‘/’ character.

Parameters

text (str) – Text to check.

Returns

True if text is a command, else False.

Return type

bool

telebot.util.is_dict(var) bool#

Returns True if the given object is a dictionary.

Parameters

var (object) – object to be checked

Returns

True if the given object is a dictionary.

Return type

bool

telebot.util.is_pil_image(var) bool#

Returns True if the given object is a PIL.Image.Image object.

Parameters

var (object) – object to be checked

Returns

True if the given object is a PIL.Image.Image object.

Return type

bool

telebot.util.is_string(var) bool#

Returns True if the given object is a string.

telebot.util.parse_web_app_data(token: str, raw_init_data: str)#

Parses web app data.

Parameters
  • token (str) – The bot token

  • raw_init_data (str) – The raw init data

Returns

The parsed init data

telebot.util.pil_image_to_file(image, extension='JPEG', quality='web_low')#
telebot.util.quick_markup(values: Dict[str, Dict[str, Any]], row_width: int = 2) InlineKeyboardMarkup#

Returns a reply markup from a dict in this format: {‘text’: kwargs} This is useful to avoid always typing ‘btn1 = InlineKeyboardButton(…)’ ‘btn2 = InlineKeyboardButton(…)’

Example:

Using quick_markup:#
quick_markup({
    'Twitter': {'url': 'https://twitter.com'},
    'Facebook': {'url': 'https://facebook.com'},
    'Back': {'callback_data': 'whatever'}
}, row_width=2):
# returns an InlineKeyboardMarkup with two buttons in a row, one leading to Twitter, the other to facebook
# and a back button below

# kwargs can be:
{
    'url': None,
    'callback_data': None,
    'switch_inline_query': None,
    'switch_inline_query_current_chat': None,
    'callback_game': None,
    'pay': None,
    'login_url': None,
    'web_app': None
}
Parameters
  • values (dict) – a dict containing all buttons to create in this format: {text: kwargs} {str:}

  • row_width (int) – int row width

Returns

InlineKeyboardMarkup

Return type

types.InlineKeyboardMarkup

telebot.util.smart_split(text: str, chars_per_string: int = 4096) List[str]#

Splits one string into multiple strings, with a maximum amount of chars_per_string characters per string. This is very useful for splitting one giant message into multiples. If chars_per_string > 4096: chars_per_string = 4096. Splits by ‘n’, ‘. ‘ or ‘ ‘ in exactly this priority.

Parameters
  • text (str) – The text to split

  • chars_per_string (int) – The number of maximum characters per part the text is split to.

Returns

The splitted text as a list of strings.

Return type

list of str

telebot.util.split_string(text: str, chars_per_string: int) List[str]#

Splits one string into multiple strings, with a maximum amount of chars_per_string characters per string. This is very useful for splitting one giant message into multiples.

Parameters
  • text (str) – The text to split

  • chars_per_string (int) – The number of characters per line the text is split into.

Returns

The splitted text as a list of strings.

Return type

list of str

telebot.util.update_types = ['message', 'edited_message', 'channel_post', 'edited_channel_post', 'inline_query', 'chosen_inline_result', 'callback_query', 'shipping_query', 'pre_checkout_query', 'poll', 'poll_answer', 'my_chat_member', 'chat_member', 'chat_join_request']#

All update types, should be used for allowed_updates parameter in polling.

Returns an HTML user link. This is useful for reports. Attention: Don’t forget to set parse_mode to ‘HTML’!

Example:#
bot.send_message(your_user_id, user_link(message.from_user) + ' started the bot!', parse_mode='HTML')

Note

You can use formatting.* for all other formatting options(bold, italic, links, and etc.) This method is kept for backward compatibility, and it is recommended to use formatting.* for more options.

Parameters
  • user (telebot.types.User) – the user (not the user_id)

  • include_id (bool) – include the user_id

Returns

HTML user link

Return type

str

telebot.util.validate_web_app_data(token: str, raw_init_data: str)#

Validates web app data.

Parameters
  • token (str) – The bot token

  • raw_init_data (str) – The raw init data

Returns

The parsed init data

telebot.util.webhook_google_functions(bot, request)#

A webhook endpoint for Google Cloud Functions FaaS.

Parameters
Returns

The response object