Python调用telegram.ext.JobQueue报context缺失错误的解决方法
问题描述
我计划在代码中使用telegram.ext.JobQueue实现每30秒执行一次news()函数,该函数会通过Telegram为我发送document(文档),初始代码如下:
def start(update: Update, context: CallbackContext): update.message.reply_text( "Hello, Welcome to my Bot. Please write /help to see the commands available.") context.job_queue.run_repeating(news, interval=30, first=10, context=update.message.chat_id) def news(update: Update, context: CallbackContext): s = io.StringIO() csv.writer(s).writerows(newlist) s.seek(0) buf = io.BytesIO() buf.write(s.getvalue().encode()) buf.seek(0) buf.name = f'items.csv' context.bot.send_document(chat_id=update.message.chat_id, document=buf)
触发错误的代码行:
context.job_queue.run_repeating(news, interval=30, first=10, context=update.message.chat_id)
在Telegram bot中触发/start指令时得到如下报错:
No error handlers are registered, logging exception. Traceback (most recent call last): File "/Users/arman/anaconda3/lib/python3.8/site-packages/telegram/ext/dispatcher.py", line 555, in process_update handler.handle_update(update, self, check, context) File "/Users/arman/anaconda3/lib/python3.8/site-packages/telegram/ext/handler.py", line 198, in handle_update return self.callback(update, context) File "/Users/arman/Downloads/test3.py", line 23, in start context.job_queue.run_repeating(news, interval=10, first=10, context=update.message.chat_id) File "/Users/arman/anaconda3/lib/python3.8/site-packages/telegram/ext/jobqueue.py", line 293, in run_repeating j = self.scheduler.add_job( File "/Users/arman/anaconda3/lib/python3.8/site-packages/apscheduler/schedulers/base.py", line 434, in add_job job = Job(self, **job_kwargs) File "/Users/arman/anaconda3/lib/python3.8/site-packages/apscheduler/job.py", line 49, in __init__ self._modify(id=id or uuid4().hex, **kwargs) File "/Users/arman/anaconda3/lib/python3.8/site-packages/apscheduler/job.py", line 180, in _modify check_callable_args(func, args, kwargs) File "/Users/arman/anaconda3/lib/python3.8/site-packages/apscheduler/util.py", line 401, in check_callable_args raise ValueError('The following arguments have not been supplied: %s' % ValueError: The following arguments have not been supplied: context
问题原因
报错核心是news函数的签名不符合JobQueue定时回调的规范:
- 响应用户消息/指令的回调,需要接收
(update, context)两个入参,这类回调由用户发送的Update事件触发,能拿到对应的update对象 - JobQueue触发的定时任务回调只接收一个
context: CallbackContext类型的参数——定时任务是调度器到点自动执行,没有对应的用户触发事件,根本不存在update参数。你通过run_repeating的context参数传入的chat_id,会挂载在context.job.context属性上供回调使用。
你给news定义了两个入参,调度器执行时只会传入一个context对象,参数数量不匹配,就抛出了缺参数的错误。
修复方法
修改news函数的签名和内部取值逻辑即可,start函数里的run_repeating调用不需要改动,你传context=update.message.chat_id的写法是正确的:
def news(context: CallbackContext): # 从job.context中取出之前传入的chat_id chat_id = context.job.context s = io.StringIO() csv.writer(s).writerows(newlist) s.seek(0) buf = io.BytesIO() buf.write(s.getvalue().encode()) buf.seek(0) buf.name = f'items.csv' context.bot.send_document(chat_id=chat_id, document=buf)
补充说明:如果你用的是python-telegram-bot v20+异步版本,回调需要加
async关键字,调用发送消息方法时要加await,从你贴的报错路径判断当前用的是v13.x同步版本,不需要做这部分调整。
内容的提问来源于stack exchange,提问作者user13622589
相关产品推荐
相关产品推荐

