Search code examples
pythonpython-telegram-bot

Python Telegram bot daily_routine callback not triggered from JobQueue


I have the following bot:

class Bot:
    def __init__(self, handlers: list[BaseHandler], daily_routines):
        self.application = Application.builder().token(BOT_TOKEN).build()
        for handler in handlers:
            self.application.add_handler(handler)

        self.job_queue = JobQueue()
        self.job_queue.set_application(self.application)

        for routine in daily_routines:
            self.job_queue.run_daily(routine, time=datetime.time(hour=15, minute=49,
                                                                 tzinfo=pytz.timezone('Asia/Jerusalem')))

    def __call__(self, *args, **kwargs):
        self.application.run_polling()


if __name__ == "__main__":
    _handlers = [
       ...
    ]

    bot = Bot(_handlers, [daily_routine])
    bot()

The daily_routine is defined as:

async def daily_routine(context: CallbackContext) -> None:
    job = context.job
    await context.bot.send_message(job.chat_id, text="HELLO")

The daily_routine callback isn't triggered on the specified time.

How can I make it work?


Solution

  • You don't start the JobQueue, so it won't process any jobs.

    I recommend to instead just use the JobQueue instance that the ApplicationBuilder provides for you, i.e. self.application.job_queue. Application.run_polling already takes care of starting and stopping that instance.


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