You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Telegram bot调用run_repeating报参数未提供错误问题咨询

问题原因

你当前使用的是python-telegram-bot开启use_context=True的版本,JobQueue的回调函数只能接收唯一的CallbackContext类型参数,你传入的自定义参数会挂载在该参数的job.context属性下,你定义的stupid_hello要求传入两个参数,和框架要求的回调参数规则不匹配,因此触发参数缺失错误。

补充说明:开启use_context=True后,CommandHandler的pass_job_queue参数默认已经是True,不需要显式声明,保留也不影响运行。

修正方案

方法1:按你原有的传参逻辑修改回调函数

直接修改stupid_hello的定义即可,不需要改run_repeating的传参:

def stupid_hello(context: CallbackContext):
    # 取出你自定义传入的上下文参数
    custom_context = context.job.context
    custom_context['bot'].send_message(chat_id=custom_context['chat_id'] ,text='Hello World')

方法2:简化参数传参(更推荐)

框架传入的CallbackContext本身已经携带bot属性,不需要你手动把bot塞进自定义上下文里,代码可以更精简:

修改run_repeating调用:

context.job_queue.run_repeating(stupid_hello, 
        interval=30, 
        # 只需要传chat_id即可
        context=update.message.chat_id, 
        first=datetime.time(hour=8), 
        last=datetime.time(hour=22))

对应修改回调函数:

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

内容的提问来源于stack exchange,提问作者Pedro Sanchez

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.26 19:24:03