简体   繁体   English

python-telegram-bot 仅在命令后处理消息

[英]python-telegram-bot handle message only after command

I'm trying to let bot send the message that i gave him by first sending /send_m command then it will ask for the message after that i write the message it will send it to other users, the problem is that message handler is running send function even if I didn't give it /send_m command how can i fix this issue?我试图让机器人通过首先发送/send_m命令来发送我给他的消息,然后它会在我写下将发送给其他用户的消息之后询问消息,问题是消息处理程序正在运行send function 即使我没有给它/send_m命令我该如何解决这个问题?

dispatcher = updater.dispatcher
dispatcher.add_handler(CommandHandler('send', send_m))
dispatcher.add_handler(MessageHandler(Filters.text,send))
updater.start_polling()

I'm handling messages and commands like this.我正在处理这样的消息和命令。

我已经声明了一个布尔变量,当我得到命令时,它被设置为True并声明结束函数以在函数调用时将布尔值设置为False ,并且我为处理的每条消息放置 if 语句以检查发送命令是否已调用。

dispatcher.add_handler(MessageHandler(Filters.text,send & ~Filters.command, send))

You may notice that MessageHandler is used instead of CommandHandler.您可能会注意到使用 MessageHandler 而不是 CommandHandler。 This is a more general handler that selects messages based off flags that you supply.这是一个更通用的处理程序,它根据您提供的标志选择消息。 In this case, it handles messages that contain text but aren't commands在这种情况下,它处理包含文本但不是命令的消息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM