是否有可能使文本消息=命令与斜杠?电报python机器人



我创建了telegram bot,现在它通过带有斜线的命令处理程序使用命令来激活不同的功能,但问题是-是否有可能让他理解文本=/命令,例如("yes, Y, yes, YES"=/是的)。

我想保留我的命令处理程序,但想让键盘按钮带有不带斜杠的命令。

self._keyboard: List[List[Union[str, KeyboardButton]]] = [
['/yes', '/no', '/sure'],
['/absolutely', '/allright'],

handles = [
CommandHandler('yes', self._yes),
CommandHandler('no', self._no),
CommandHandler('sure', self._sure),
CommandHandler('absolutely', self._absolutely),
CommandHandler('allright', self._allright),
#New added string: (Big Thanks to the user CallMeStag)
MessageHandler(Filters.regex('^yes$'), self._yes),
]

#I can start bot, bot not every function working, under is error which i got:
AttributeError: MessageHandler object has no attribute command

KeyboardButtons只是发送带有它们显示的文本的消息的快捷方式。你不能让他们发不同的短信。可以做的就是简单地处理非命令消息。如果您收到一条文本为yes/Y/Yes/YES的消息,只需运行与/yes相同的代码。

编辑使用python-telegram-bot,您可以使用MessageHandler和例如Filters.regex:

MessageHandler(Filters.regex('^yes|Y|Yes|YES$'), self._yes)

最新更新