Telegram bot定期发送消息

问题描述 投票:0回答:1

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

代码:

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个回答
0
投票

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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.