0
votes

I am trying to get my bot to send a message to the user periodically but I'm getting the error below. What am I doing wrong?

code:

import telegram.ext
from telegram.ext import Updater
from telegram.ext import CommandHandler

def callback_minute(update: telegram.Update, context: telegram.ext.CallbackContext):
    context.bot.send_message(chat_id= update.effective_chat.id, 
                             text='One message every minute')

def main():
    u = Updater('TOKEN', use_context=True)
    j = u.job_queue
    job_minute = j.run_repeating(callback_minute, interval=60, first=0)
    u.start_polling()

main()

Error:

TypeError: callback_minute() missing 1 required positional argument: 'context'
1

1 Answers

1
votes

The transition guide to version 12.0 has a small section about job callbacks. It especifies only context (CallbackContent object) as a parameter for the callback function, which includes bot and job.

def callback_minute(context: telegram.ext.CallbackContext):
    context.bot.send_message(chat_id=SOMECHATID, text='One message every minute')

As you can see, you need to especify a chat_id in SOMECHATID.

The wiki has a small tutorial. If you take a close look you will see that the job callback only uses context, the other function callback is to handle the /timer command invoked by someone, hence the use of update and context.