首页 > 解决方案 > 是否可以使用斜线制作文本消息 = 命令?电报 python 机器人

问题描述

我创建了电报机器人,现在它通过带有斜杠的命令处理程序使用命令来激活不同的功能,但问题是 - 是否有可能让他理解 text = /command,例如(“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

标签: pythonpython-3.xtelegramtelegram-botpython-telegram-bot

解决方案


KeyboardButtons只是发送带有它们显示的文本的消息的快捷方式。你不能让他们发送不同的文本。您可以做的就是简单地处理非命令消息。如果您收到带有文本 /// 的消息yesY只需运行您要运行的相同代码即可YesYES/yes

编辑:使用python-telegram-bot,您可以使用 and 来执行此操作MessageHandler,例如Filters.regex

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

推荐阅读