我创建了一个 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
。
来源:https://ptb.readthedocs.io/en/latest/telegram.ext.commandhandler.html
此行为对于所有 pass_*
处理程序参数都是相同的。
关于Python-telegram-bot - 命令行处理程序未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49345529/