Search code examples
pythontelegramtelegram-botpython-telegram-bot

How can I schedule a task within my telegram application to message some chat every 5 minutes?


I read the python-telegram-bot documentation about the job_queue here

It is written that

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()

Yet in my short example

from telegram.ext import Application

application = Application.builder().token("TOKEN").build()

job_queue = application.job_queue

The job_queue is None, so I can't execute run_repeating on the queue. I can't seem to find a working example on the current version. I know my application is correct because when I added handlers to connect to telegram it was working. I managed to communicate back and forth in the chat.

I also know that I can use the following in the task to send the message to the chat

await context.bot.send_message(chat_id=update.message.chat_id, text="Testing evaluation")

Can someone give me a working example on how I can schedule a tasks to send the message to some chat at an interval of time such as 60 seconds.

python-telegram-bot version I am working with is 20.4
python version is 3.9


Solution

  • Please have a look at

    All of these references include one important note: In order to use the JobQueue, you'll have to install PTB with an optional dependency: pip install "python-telegram-bot[job-queue]" - See also the readmes section on dependencies in PTB.

    Note that PTB issues warnings about missing optional dependencies wherever possible. I'm pretty sure that you'll find a note on that if you carefully read your console output ;)


    Disclaimer: I'm currently the maintainer of python-telegram-bot.