电报机器人:收到'The following arguments have not been supplied'错误



我正试图向Telegram bot添加一个计时器,该计时器每x次运行并发送一条消息。总是得到错误:回调函数没有提供x参数,即使我在调用run_repeating时将这些参数放在上下文参数中。

run_repeating:的调用

context.job_queue.run_repeating(stupid_hello, 
interval=30, 
context={'bot': context.bot,'chat_id':update.message.chat_id}, 
first=datetime.time(hour=8), 
last=datetime.time(hour=22))

回调函数:

def stupid_hello(bot, chat_id):
bot.send_message(chat_id=chat_id ,text='Hello World')

这就是我设置处理程序的方式:

dp.add_handler(CommandHandler("start", start, pass_job_queue=True))

run_repeating函数是";"开始";作用

---编辑---

添加代码以复制:

import logging
from re import sub
from typing import Set
import praw
from collections import Counter
from praw.models import MoreComments
import os
import datetime
from telegram import Update
from telegram.ext import Updater, CommandHandler, CallbackContext
import config
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)
logger = logging.getLogger(__name__)

def stupid_hello(bot, chat_id):
bot.send_message(chat_id=chat_id ,text='Hello World')
def start(update: Update, context: CallbackContext):
context.job_queue.run_repeating(stupid_hello, interval=30, 
context={'bot': context.bot, 'chat_id':update.message.chat_id}, 
first=datetime.time(hour=8), 
last=datetime.time(hour=22))
def help(update, context):
"""Send a message when the command /help is issued."""
update.message.reply_text('/start, /top_ten_satoshi')
def error(update, context):
"""Log Errors caused by Updates."""
logger.warning('Update "%s" caused error "%s"', update, context.error)
def main():
"""Start the bot."""
# Create the Updater and pass it your bot's token.
# Make sure to set use_context=True to use the new context based callbacks
# Post version 12 this will no longer be necessary
updater = Updater(config.telegram_token, use_context=True)
# Get the dispatcher to register handlers
dp = updater.dispatcher
# on different commands - answer in Telegram
dp.add_handler(CommandHandler("start", start, pass_job_queue=True))
dp.add_handler(CommandHandler("help", help))
# log all errors
dp.add_error_handler(error)
# Start bot for local usage
updater.start_polling()
# Run the bot until you press Ctrl-C or the process receives SIGINT,
# SIGTERM or SIGABRT. This should be used most of the time, since
# start_polling() is non-blocking and will stop the bot gracefully.
updater.idle()

if __name__ == '__main__':
main()

如果你想复制它,你需要添加一个带有你自己的telegram bot令牌的config.py,并从telegram 发送/启动到bot

我在Python Telegram Bot中找到了解决方案,无法通过作业队列?

我认为这个问题与run_repeating函数有关,而它与job_queue有关。

希望这能帮助其他有同样问题的人。

from telegram.ext import Updater, CommandHandler, MessageHandler,    Filters, InlineQueryHandler

def sayhi(context):
context.bot.send_message(context.job.context, text="hi")
def time(update, context):
context.job_queue.run_repeating(sayhi, 5, context=update.message.chat_id)
def main():
updater = Updater('Token', use_context=True)
dp = updater.dispatcher
dp.add_handler(MessageHandler(Filters.text , time))
updater.start_polling()
updater.idle()
main()

正如你所看到的,run_repeating中的context关键字参数在回调函数中被引用为context.job.conf,这是我缺少的部分

相关内容

最新更新