-
Notifications
You must be signed in to change notification settings - Fork 5
Extensions – JobQueue
The extension class telegram.ext.JobQueue allows you to perform tasks with a delay or even periodically. For example to send regular updates to your subscribers.
The JobQueue will see some big improvements with the release of v5.0, which requires changes that are not backwards compatible. See this article
The Updater will create a job queue for you:
>>> from telegram.ext import Updater
>>> u = Updater('TOKEN')
>>> j = u.job_queueThe job queue uses functions for tasks, so we define one and add it to the queue. Usually, when the first job is added to the queue, it will start automatically. We can prevent this by setting prevent_autostart=True:
>>> def job1(bot):
... bot.sendMessage(chat_id='@examplechannel', text='One message every minute')
>>> j.put(job1, 60, next_t=0, prevent_autostart=True)You can also have a job that will be executed only once, with a delay:
>>> def job2(bot):
... bot.sendMessage(chat_id='@examplechannel', text='A single message with 30s delay')
>>> j.put(job2, 30, repeat=False)Now, because we didn't prevent the auto start this time, the queue will start ticking. It runs in a seperate thread, so it is non-blocking. When we stop the Updater, the related queue will be stopped as well:
>>> u.stop()We can also stop the job queue by itself:
>>> j.stop()To be more detailed:
>>> j.put(run, interval, repeat=True, next_t=None, prevent_autostart=False)As explained in the docs you can define several parameters:
- run (function) – A function that takes the parameter bot
- interval (float) – The interval in seconds in which run should be executed
- repeat (Optional[bool]) – If False, job will only be executed once
- next_t (Optional[float]) – Time in seconds in which run should be executed first. Defaults to interval
- prevent_autostart (Optional[bool]) – If True, the job queue will not be started automatically if it is not running.
Wiki of python-telegram-bot © Copyright 2015-2022 – Licensed by Creative Commons
- Types of Handlers
- Advanced Filters
- Storing data
- Making your bot persistent
- Adding Defaults
- Exception Handling
- Job Queue
- Arbitrary
callback_data - Avoiding flood limits
- Frequently requested design patterns
- Code snippets
- Performance Optimizations
- Webhooks
- Telegram Passport
- Bots built with PTB
- Automated Bot Tests