telegram.ext.updater module

This module contains the class Updater, which tries to make creating Telegram bots intuitive.

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

Bases: object

This class, which employs the Dispatcher class, 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.

Attributes:

Parameters:
  • token (Optional[str]) – The bot’s token given by the @BotFather
  • base_url (Optional[str]) –
  • workers (Optional[int]) – Amount of threads in the thread pool for functions decorated with @run_async
  • bot (Optional[Bot]) – A pre-initialized bot instance. If a pre-initizlied 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 (Optional[function]) – Takes signum, frame as positional arguments. This will be called when a signal is received, defaults are (SIGINT, SIGTERM, SIGABRT) setable with Updater.idle(stop_signals=(signals))
  • request_kwargs (Optional[dict]) – Keyword args to control the creation of a request object (ignored if bot argument is used).
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 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)
signal_handler(signum, frame)
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 (Optional[float]) – Time to wait between polling updates from Telegram in
  • Default is 0.0 (seconds.) –
  • timeout (Optional[float]) – Passed to Bot.getUpdates
  • network_delay – Deprecated. Will be honoured as read_latency for a while but will be removed in the future.
  • clean (Optional[bool]) – Whether to clean any pending updates on Telegram servers before actually starting to poll. 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
  • allowed_updates (Optional[list[str]]) – Passed to Bot.getUpdates
  • read_latency (Optional[float|int]) – 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).
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 (Optional[str]) – IP-Address to listen on
  • port (Optional[int]) – Port the bot should be listening on
  • url_path (Optional[str]) – Path inside url
  • cert (Optional[str]) – Path to the SSL certificate file
  • key (Optional[str]) – Path to the SSL key file
  • clean (Optional[bool]) – 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 (Optional[str]) – Explicitly specify the webhook url. Useful behind NAT, reverse proxy, etc. Default is derived from listen, port & url_path.
  • allowed_updates (Optional[list[str]]) – Passed to Bot.setWebhook
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