我可以在一个命令上调用多个消息处理程序吗?
Can I have multiple message handlers called on one command?
我想为电报机器人做贡献,它的功能分为多个模块。这个机器人显示了我大学 类 的日程安排。我想添加我自己的模块来显示其他事件。此外,我希望对除我之外的其他模块的干预最少。
当天的日程安排显示为 /today
命令,并且已经定义了它的处理程序。
我想添加我自己的处理程序,该处理程序还会向用户发送带有我的事件的消息。
问题是,我可以像这样声明两个不同的消息处理程序吗:
# module1.py
@bot.message_handler(commands=['today'])
def show_classes():
...
# my_module.py
@bot.message_handler(commands=['today'])
def show_events():
...
这种方法行得通吗?如果不是,正确的做法是什么?
终于到了我的电脑。不,这行不通。检查消息处理程序过滤器直到第一次匹配。使用这样的代码,只有 test()
会在 /con
到达时被调用。
@bot.message_handler(commands=['con'])
def test(message: Message):
bot.send_message(message.chat.id,"test1")
@bot.message_handler(commands=['con'])
def test2(message: Message):
bot.send_message(message.chat.id,"test2")
我想为电报机器人做贡献,它的功能分为多个模块。这个机器人显示了我大学 类 的日程安排。我想添加我自己的模块来显示其他事件。此外,我希望对除我之外的其他模块的干预最少。
当天的日程安排显示为 /today
命令,并且已经定义了它的处理程序。
我想添加我自己的处理程序,该处理程序还会向用户发送带有我的事件的消息。
问题是,我可以像这样声明两个不同的消息处理程序吗:
# module1.py
@bot.message_handler(commands=['today'])
def show_classes():
...
# my_module.py
@bot.message_handler(commands=['today'])
def show_events():
...
这种方法行得通吗?如果不是,正确的做法是什么?
终于到了我的电脑。不,这行不通。检查消息处理程序过滤器直到第一次匹配。使用这样的代码,只有 test()
会在 /con
到达时被调用。
@bot.message_handler(commands=['con'])
def test(message: Message):
bot.send_message(message.chat.id,"test1")
@bot.message_handler(commands=['con'])
def test2(message: Message):
bot.send_message(message.chat.id,"test2")