简体   繁体   English

Telegram bot 定期发送消息

[英]Telegram bot send messages periodically

I am trying to get my bot to send a message to the user periodically but I'm getting the error below.我试图让我的机器人定期向用户发送消息,但我收到以下错误。 What am I doing wrong?我究竟做错了什么?

code:代码:

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

Error:错误:

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

The transition guide to version 12.0 has a small section about job callbacks. 12.0 版的过渡指南有一小部分是关于作业回调的。 It especifies only context (CallbackContent object) as a parameter for the callback function, which includes bot and job .它仅指定context (CallbackContent 对象)作为回调 function 的参数,其中包括botjob

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

As you can see, you need to especify a chat_id in SOMECHATID .如您所见,您需要在SOMECHATID chat_id

The wiki has a small tutorial. wiki有一个小教程。 If you take a close look you will see that the job callback only uses context , the other function callback is to handle the /timer command invoked by someone, hence the use of update and context .如果仔细观察,您会发现作业回调仅使用context ,另一个 function 回调是处理某人调用的/timer命令,因此使用了updatecontext

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM