telegram.ext.Updater

class telegram.ext.Updater(token=None, base_url=None, workers=4, bot=None, user_sig_handler=None, request_kwargs=None)

Bases: object

This class, which employs the telegram.ext.Dispatcher, provides a frontend to telegram.Bot to the programmer, so they can focus on coding the bot. Its purpose is to receive the updates from Telegram and to deliver them to said dispatcher. It also runs in a separate thread, so the user can interact with the bot, for example on the command line. The dispatcher supports handlers for different kinds of data: Updates from Telegram, basic text commands and even arbitrary types. The updater can be started as a polling service or, for production, use a webhook to receive updates. This is achieved using the WebhookServer and WebhookHandler classes.

bot

telegram.Bot – The bot used with this Updater.

user_sig_handler

signal – signals the updater will respond to.

update_queue

Queue – Queue for the updates.

job_queue

telegram.ext.JobQueue – Jobqueue for the updater.

dispatcher

telegram.ext.Dispatcher – Dispatcher that handles the updates and dispatches them to the handlers.

running

bool – Indicates if the updater is running.

Parameters:
  • token (str, optional) – The bot’s token given by the @BotFather.
  • base_url (str, optional) – Base_url for the bot.
  • workers (int, optional) – Amount of threads in the thread pool for functions decorated with @run_async.
  • bot (telegram.Bot, optional) – A pre-initialized bot instance. If a pre-initialized bot is used, it is the user’s responsibility to create it using a Request instance with a large enough connection pool.
  • user_sig_handler (function, optional) – Takes signum, frame as positional arguments. This will be called when a signal is received, defaults are (SIGINT, SIGTERM, SIGABRT) setable with idle.
  • request_kwargs (dict, optional) – Keyword args to control the creation of a request object (ignored if bot argument is used).

Note

You must supply either a bot or a token argument.

Raises:ValueError – If both token and bot are passed or none of them.
idle(stop_signals=(2, 15, 6))

Blocks until one of the signals are received and stops the updater.

Parameters:stop_signals (iterable) – Iterable containing signals from the signal module that should be subscribed to. Updater.stop() will be called on receiving one of those signals. Defaults to (SIGINT, SIGTERM, SIGABRT).
start_polling(poll_interval=0.0, timeout=10, network_delay=None, clean=False, bootstrap_retries=0, read_latency=2.0, allowed_updates=None)

Starts polling updates from Telegram.

Parameters:
  • poll_interval (float, optional) – Time to wait between polling updates from Telegram in seconds. Default is 0.0.
  • timeout (float, optional) – Passed to telegram.Bot.get_updates.
  • clean (bool, optional) – Whether to clean any pending updates on Telegram servers before actually starting to poll. Default is False.
  • bootstrap_retries (int, optional) –

    Whether the bootstrapping phase of the Updater will retry on failures on the Telegram server.

    • < 0 - retry indefinitely
    • 0 - no retries (default)
    • > 0 - retry up to X times
  • allowed_updates (List[str], optional) – Passed to telegram.Bot.get_updates.
  • read_latency (float | int, optional) – Grace time in seconds for receiving the reply from server. Will be added to the timeout value and used as the read timeout from server (Default: 2).
  • network_delay – Deprecated. Will be honoured as read_latency for a while but will be removed in the future.
Returns:

The update queue that can be filled from the main thread.

Return type:

Queue

start_webhook(listen='127.0.0.1', port=80, url_path='', cert=None, key=None, clean=False, bootstrap_retries=0, webhook_url=None, allowed_updates=None)

Starts a small http server to listen for updates via webhook. If cert and key are not provided, the webhook will be started directly on http://listen:port/url_path, so SSL can be handled by another application. Else, the webhook will be started on https://listen:port/url_path

Parameters:
  • listen (str, optional) – IP-Address to listen on. Default 127.0.0.1.
  • port (int, optional) – Port the bot should be listening on. Default 80.
  • url_path (str, optional) – Path inside url.
  • cert (str, optional) – Path to the SSL certificate file.
  • key (str, optional) – Path to the SSL key file.
  • clean (bool, optional) – Whether to clean any pending updates on Telegram servers before actually starting the webhook. Default is False.
  • bootstrap_retries (Optional[int[) –

    Whether the bootstrapping phase of the Updater will retry on failures on the Telegram server.

    • < 0 - retry indefinitely
    • 0 - no retries (default)
    • > 0 - retry up to X times
  • webhook_url (str, optional) – Explicitly specify the webhook url. Useful behind NAT, reverse proxy, etc. Default is derived from listen, port & url_path.
  • allowed_updates (List[str], optional) – Passed to telegram.Bot.set_webhook.
Returns:

The update queue that can be filled from the main thread.

Return type:

Queue

stop()

Stops the polling/webhook thread, the dispatcher and the job queue.