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)¶ Bases:
objectThis 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.
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=5.0, clean=False, bootstrap_retries=0)¶ 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 (Optional[float]) – Passed to Bot.getUpdates
- 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 indefinitely0 - no retries (default)> 0 - retry up to X times
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)¶ 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 indefinitely0 - no retries (default)> 0 - retry up to X times - webhook_url (Optional[str]) – Explicitly specifiy the webhook url. Useful behind NAT, reverse proxy, etc. Default is derived from listen, port & url_path.
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