Javier Lam
Javier Lam

Reputation: 21

Python Telegram Bot, unable to pass job queue?

I copied this code from another thread as is, but was unable to get it to work...

from telegram.ext import Updater, CommandHandler, MessageHandler,    Filters, InlineQueryHandler


def sayhi(bot, job):
    job.context.message.reply_text("hi")

def time(bot, update,job_queue):
    job = job_queue.run_repeating(sayhi, 5, context=update)

def main():
    updater = Updater('BotKey')
    dp = updater.dispatcher
    dp.add_handler(MessageHandler(Filters.text , time,pass_job_queue=True))


    updater.start_polling()
    updater.idle()

if __name__ == '__main__':
    main()

The Error I was give in my command terminal is:

TypeError: time() missing 1 required positional argument: 'job_queue'

I found this strange as I thought I already had set pass_job_queue=True...

(also, I did change the BotKey to the required key. I can get my bot to reply to texts but cant get it to periodically send stuff...)

Upvotes: 0

Views: 2543

Answers (1)

Hoxha Alban
Hoxha Alban

Reputation: 1137

pass_job_queue was deprecated in version 12.0.0, Tutorial to switch version here

You need to use context based callbacks, like in this example.

Here's your code changed:

from telegram.ext import Updater, CommandHandler, MessageHandler,    Filters, InlineQueryHandler


def sayhi(context):
    context.bot.send_message(context.job.context, text="hi")

def time(update, context):
    context.job_queue.run_repeating(sayhi, 5, context=update.message.chat_id)

def main():
    updater = Updater('Token', use_context=True)
    dp = updater.dispatcher
    dp.add_handler(MessageHandler(Filters.text , time))

    updater.start_polling()
    updater.idle()

main()

Upvotes: 1

Related Questions