0

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

Javier Lam
  • 21
  • 3

1 Answers1

1

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()
Hoxha Alban
  • 1,042
  • 1
  • 8
  • 12
  • Thank you! this is definitely new to me haha. could I ask, when I am defining a reply to a command, I still seem to use the usual: def help_command(update, context): update.message.reply_text('If you need help you should Google it ') whereas in my main function, I just add this handler: dp.add_handler(CommandHandler("start", cmd.start_command)) is it bad practice that Im not using any sort of context? – Javier Lam May 22 '21 at 08:49
  • that's ok, the examples in the github repo are doing the same – Hoxha Alban May 22 '21 at 08:53
  • If you don't need the `context` argument, you don't have to use it. It's just passed along to *every* callback to unify the callback signatures, so you don't always have to check the handler to see which arguments the callback should accept. – CallMeStag May 22 '21 at 15:41