【问题标题】:Telegram bot send messages periodicallyTelegram bot 定期发送消息
【发布时间】:2020-05-27 09:01:04
【问题描述】:

我正在尝试让我的机器人定期向用户发送消息,但出现以下错误。我做错了什么?

代码:

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

错误:

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

【问题讨论】:

    标签: python python-3.x telegram telegram-bot python-telegram-bot


    【解决方案1】:

    transition guide to version 12.0 有一小部分是关于工作回调的。它仅指定context(CallbackContent 对象)作为回调函数的参数,包括botjob

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

    如您所见,您需要在 SOMECHATID 中指定 chat_id

    wiki 有一个小教程。如果你仔细看你会发现作业回调只使用context,另一个函数回调是处理某人调用的/timer命令,因此使用updatecontext

    【讨论】:

      猜你喜欢
      • 2018-04-20
      • 1970-01-01
      • 1970-01-01
      • 2018-05-17
      • 2016-05-09
      • 2018-02-13
      • 1970-01-01
      • 1970-01-01
      • 2016-02-23
      相关资源
      最近更新 更多