python-telegram-bot / python-telegram-bot

We have made you a wrapper you can't refuse
https://python-telegram-bot.org
GNU General Public License v3.0
26.3k stars 5.34k forks source link

[QUESTION] job queue is not passed to callback when use_context is True #2421

Closed OriYitzhaki closed 3 years ago

OriYitzhaki commented 3 years ago

Steps to reproduce

  1. Set that code:

    def callback_alarm(bot, job):
        bot.send_message(chat_id=job.context, text='Wait for another 10 Seconds')

    def callback_timer(bot, update, job_queue):
        bot.send_message(chat_id=update.message.chat_id,
                         text='Wait for 10 seconds')
        job_queue.run_repeating(callback_alarm, 10, context=update.message.chat_id)

    def Stop_timer(bot, update, job_queue):
        bot.send_message(chat_id=update.message.chat_id,
                         text='Stopped!')
        job_queue.stop()

    updater = Updater("Token")
    updater.dispatcher.add_handler(CommandHandler('start', callback_timer, pass_job_queue=True))
    updater.dispatcher.add_handler(CommandHandler('stop', Stop_timer, pass_job_queue=True))

    updater.start_polling()
  1. and send start.

Expected behaviour

Well... that just should and send messages.

Actual behaviour

TypeError: callback_timer() missing 1 required positional argument: 'job_queue' the last arg is not passed.

Configuration

Operating System: Windows 10.

Version of Python, python-telegram-bot & dependencies: python 3.8, python-telegram-bot Latest $ python -m telegram

Logs

Insert logs here (if necessary)

starry-shivam commented 3 years ago

You're using old handler signature, It should look like

def job_callback(context: CallbackContext):
    ...

Since from v12 onwards, only CallbackContext gets passed into job callbacks, job is available as context.job similarly job_queue can be accessed as context.job_queue.