Utils#

util file#

class telebot.util.AsyncTask(target, *args, **kwargs)#

Bases: object

wait()#
class telebot.util.CustomRequestResponse(json_text, status_code=200, reason='')#

Bases: object

json()#
telebot.util.OrEvent(*events)#
class telebot.util.ThreadPool(telebot, num_threads=2)#

Bases: object

clear_exceptions()#
close()#
on_exception(worker_thread, exc_info)#
put(func, *args, **kwargs)#
raise_exceptions()#
class telebot.util.WorkerThread(exception_callback=None, queue=None, name=None)#

Bases: threading.Thread

clear_exceptions()#
count = 0#
put(task, *args, **kwargs)#
raise_exceptions()#
run()#

Method representing the thread’s activity.

You may override this method in a subclass. The standard run() method invokes the callable object passed to the object’s constructor as the target argument, if any, with sequential and keyword arguments taken from the args and kwargs arguments, respectively.

stop()#
telebot.util.antiflood(function, *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

  • args

  • kwargs

Returns

None

telebot.util.async_dec()#
telebot.util.chunks(lst, n)#

Yield successive n-sized chunks from lst.

telebot.util.deprecated(warn: bool = True, alternative: Optional[Callable] = None)#

Use this decorator to mark functions as deprecated. When the function is used, an info (or warning if warn is True) is logged.

Parameters
  • warn – If True a warning is logged else an info

  • alternative – The new function to use instead

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 – String to extract the arguments from a command

Returns

the arguments if text is a command (according to is_command), else 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 – String to extract the command from

Returns

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

telebot.util.generate_random_token()#
telebot.util.is_bytes(var)#
telebot.util.is_command(text: str) bool#

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

Parameters

text – Text to check.

Returns

True if text is a command, else False.

telebot.util.is_dict(var)#
telebot.util.is_pil_image(var)#
telebot.util.is_string(var)#
telebot.util.or_clear(self)#
telebot.util.or_set(self)#
telebot.util.orify(e, changed_callback)#
telebot.util.per_thread(key, construct_value, reset=False)#
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) telebot.types.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:

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
}
Parameters
  • values – a dict containing all buttons to create in this format: {text: kwargs} {str:}

  • row_width – int row width

Returns

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 – The text to split

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

Returns

The splitted text as a list of strings.

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 – The text to split

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

Returns

The splitted text as a list of strings.

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’)

Parameters
  • user – the user (not the user_id)

  • include_id – include the user_id

Returns

HTML user link

telebot.util.webhook_google_functions(bot, request)#

A webhook endpoint for Google Cloud Functions FaaS.