1

我对这个工作队列的事情感到困惑。在回调函数中,我想访问用户消息并对其进行处理,但在文章中说回调只接受机器人和作业参数。与那些在手我无法访问的update.message.text。因此,例如,我想将以下函数重写为我无法弄清楚的回调函数:

def echo(bot,update):
    if tldextract.extract(update.message.text).registered_domain:
        bot.send_message(chat_id= update.message.chat_id, text="OK")

我在这里想念什么?

4

1 回答 1

2

创建作业时必须传递上下文。

您可以从页面底部附近的示例中阅读:

>>> from telegram.ext import CommandHandler
>>> def callback_alarm(bot, job):
...     bot.send_message(chat_id=job.context, text='BEEP')
...
>>> def callback_timer(bot, update, job_queue):
...     bot.send_message(chat_id=update.message.chat_id,
...                      text='Setting a timer for 1 minute!')
... 
...     job_queue.run_once(callback_alarm, 60, context=update.message.chat_id)
...
>>> timer_handler = CommandHandler('timer', callback_timer, pass_job_queue=True)
>>> u.dispatcher.add_handler(timer_handler)

context当您使用和函数时run_once,您可以将任何内容(包括电报对象、列表/字典等)传递到作业中。然后在你的回调函数中,你必须按照你所说的传递2个参数,然后通过访问获取你需要的数据。run_dailyrun_repeatingbotjobjob.context

于 2017-11-07T05:31:34.130 回答