GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — Extensions JobQueue Extensions JobQueue
MoHamMad edited this page
The extension class
In addition to the tutorial below, there is also the Warning Since v20, you must install PTB with the optional requirement pip install python-telegram-bot[job-queue] The To use the from telegram.ext import Application application = Application.builder().token('TOKEN').build() job_queue = application.job_queue Just know that unless you have a good reason to do so, you should not instantiate Tasks in the job queue are encapsulated by the
You can use the following methods to create jobs with different frequency and time: Add your first job to the queue by defining a callback function and adding it to the job queue. For this tutorial, you can replace from telegram.ext import ContextTypes, Application async def callback_minute(context: ContextTypes.DEFAULT_TYPE): await context.bot.send_message(chat_id='@examplechannel', text='One message every minute') application = Application.builder().token('TOKEN').build() job_queue = application.job_queue job_minute = job_queue.run_repeating(callback_minute, interval=60, first=10) application.run_polling() The You can also add a job that will be executed only once, with a delay: from telegram.ext import ContextTypes, Application async def callback_30(context: ContextTypes.DEFAULT_TYPE): await context.bot.send_message(chat_id='@examplechannel', text='A single message with 30s delay') application = Application.builder().token('TOKEN').build() job_queue = application.job_queue job_queue.run_once(callback_30, 30) application.run_polling() In thirty seconds, you should receive the message from If you are tired of receiving a message every minute, you can temporarily disable a job or even completely remove it from the queue: job_minute.enabled = False # Temporarily disable this job job_minute.schedule_removal() # Remove this job completely Caution
You might want to add jobs in response to certain user input, and there is a convenient way to do that. The from telegram import Update from telegram.ext import CommandHandler, Application, ContextTypes async def callback_alarm(context: ContextTypes.DEFAULT_TYPE): # Beep the person who called this alarm: await context.bot.send_message(chat_id=context.job.chat_id, text=f'BEEP {context.job.data}!') async def callback_timer(update: Update, context: ContextTypes.DEFAULT_TYPE): chat_id = update.message.chat_id name = update.effective_chat.full_name await context.bot.send_message(chat_id=chat_id, text='Setting a timer for 1 minute!') # Set the alarm: context.job_queue.run_once(callback_alarm, 60, data=name, chat_id=chat_id) application = Application.builder().token('TOKEN').build() timer_handler = CommandHandler('timer', callback_timer) application.add_handler(timer_handler) application.run_polling() By placing the All good things must come to an end, so when you stop the Application, the related job queue will be stopped as well. PTBs Persistence Setup currently does not support serialization of jobs.
However, the current backend of the Wiki of
Clone this wiki locally |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |