0

I am trying to add to a Telegram bot a timer which runs and sends a message every x time. Getting always the error: x argument have not been supplied from the callback function, even though I am putting those arguments in the context argument when calling run_repeating.

The call to run_repeating:

context.job_queue.run_repeating(stupid_hello, 
        interval=30, 
        context={'bot': context.bot,'chat_id':update.message.chat_id}, 
        first=datetime.time(hour=8), 
        last=datetime.time(hour=22))

callback function:

def stupid_hello(bot, chat_id):
    bot.send_message(chat_id=chat_id ,text='Hello World')

And this is how I set the handler:

dp.add_handler(CommandHandler("start", start, pass_job_queue=True))

The run_repeating function is part of a "start" function.

--- EDIT ---

Adding code to reproduce it:

import logging
from re import sub
from typing import Set
import praw
from collections import Counter
from praw.models import MoreComments
import os
import datetime
from telegram import Update
from telegram.ext import Updater, CommandHandler, CallbackContext
import config

logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
                    level=logging.INFO)

logger = logging.getLogger(__name__)


def stupid_hello(bot, chat_id):
    bot.send_message(chat_id=chat_id ,text='Hello World')

def start(update: Update, context: CallbackContext):
    context.job_queue.run_repeating(stupid_hello, interval=30, 
        context={'bot': context.bot, 'chat_id':update.message.chat_id}, 
        first=datetime.time(hour=8), 
        last=datetime.time(hour=22))

def help(update, context):
    """Send a message when the command /help is issued."""
    update.message.reply_text('/start, /top_ten_satoshi')

def error(update, context):
    """Log Errors caused by Updates."""
    logger.warning('Update "%s" caused error "%s"', update, context.error)

def main():
    """Start the bot."""
    # Create the Updater and pass it your bot's token.
    # Make sure to set use_context=True to use the new context based callbacks
    # Post version 12 this will no longer be necessary
    updater = Updater(config.telegram_token, use_context=True)

    # Get the dispatcher to register handlers
    dp = updater.dispatcher

    # on different commands - answer in Telegram
    dp.add_handler(CommandHandler("start", start, pass_job_queue=True))
    dp.add_handler(CommandHandler("help", help))

    # log all errors
    dp.add_error_handler(error)

    # Start bot for local usage
    updater.start_polling()

    # Run the bot until you press Ctrl-C or the process receives SIGINT,
    # SIGTERM or SIGABRT. This should be used most of the time, since
    # start_polling() is non-blocking and will stop the bot gracefully.
    updater.idle()


if __name__ == '__main__':
    main()

If you want to reproduce it you will need to add a config.py with your own telegram bot token and send /start to the bot from telegram

Manu Artero
  • 9,238
  • 6
  • 58
  • 73

1 Answers1

0

I found the solution in Python Telegram Bot, unable to pass job queue?

I thought the problem was related with the run_repeating function when it was with the job_queue.

Hope this helps others having the same issue.

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

As you can see the context keyword argument in run_repeating is referenced in the callback function as context.job.context which is the part I was missing

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 12 '21 at 00:40