gpt4 book ai didi

Python-telegram-bot - 命令行处理程序未被调用

转载 作者:太空宇宙 更新时间:2023-11-03 13:59:33 30 4
gpt4 key购买 nike

我创建了一个 Python Telegram 机器人并注册了两个命令处理程序。问题是 hello 命令处理程序不起作用。

我尝试将该处理程序的组更改为 group=2,但当我使用 /rtd 时,仍然不会调用 hello。无法找出问题所在。

def mybot():
print("dispatcher created.")
updater = Updater(token=my_token)
dispatcher = updater.dispatcher

dispatcher.add_error_handler(error_callback)
dispatcher.add_handler(CommandHandler('start', start))
dispatcher.add_handler(CommandHandler('rtd', hello, pass_args=True))
dispatcher.add_handler(MessageHandler(Filters.command, unknown))

print("handlers added.")
updater.start_polling()
updater.idle()
pass

def hello(bot, update, cmd):
print("hello handler", cmd)
pass

def start(bot, update):
bot.sendMessage(chat_id=update.message.chat_id, text="bot start!")
pass

def unknown(bot, update):
bot.send_message(chat_id=update.message.chat_id, text="unknown stuff.")
pass

def error_callback(bot, update, error):
try:
raise error
except TelegramError:
print("Telegram Error")

if __name__ == '__main__':
print("bot started.")
mybot()

最佳答案

这里的问题是您对 hello 函数原型(prototype)的参数命名错误。传递命令参数的参数名称​​必须args。您将其命名为 cmd

请参阅这方面的文档: Screenshot of pass_args parameter documentation

来源:https://ptb.readthedocs.io/en/latest/telegram.ext.commandhandler.html

此行为对于所有 pass_* 处理程序参数都是相同的。

关于Python-telegram-bot - 命令行处理程序未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49345529/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com