Telegram 机器人定期发送消息
Telegram bot send messages periodically
我试图让我的机器人定期向用户发送消息,但我收到以下错误。我做错了什么?
代码:
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'
transition guide to version 12.0 有一小节是关于作业回调的。它只指定context
(CallbackContent对象)作为回调函数的参数,其中包括bot
和job
.
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
命令,因此使用 update
和 context
.
我试图让我的机器人定期向用户发送消息,但我收到以下错误。我做错了什么?
代码:
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'
transition guide to version 12.0 有一小节是关于作业回调的。它只指定context
(CallbackContent对象)作为回调函数的参数,其中包括bot
和job
.
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
命令,因此使用 update
和 context
.