python - 是否可以使用斜线制作文本消息 = 命令?电报 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
解决方案
KeyboardButtons
只是发送带有它们显示的文本的消息的快捷方式。你不能让他们发送不同的文本。您可以做的就是简单地处理非命令消息。如果您收到带有文本 /// 的消息yes
,Y
只需运行您要运行的相同代码即可Yes
。YES
/yes
编辑:使用python-telegram-bot
,您可以使用 and 来执行此操作MessageHandler
,例如Filters.regex
:
MessageHandler(Filters.regex('^yes|Y|Yes|YES$'), self._yes)
推荐阅读
- azure - Using If-Condition ADF V2
- qt - 如何将 Mimedata 保存到文件中
- javascript - 如何在 d3.js 中获取没有子元素的元素的工具提示
- sql - 使用通配符访问 SQL IN 函数
- automated-tests - 我想使用我的 Robotframework 上传文件,但我的文件上传填充没有 ID
- angular - Angular OnPush 不更新模板
- c# - 如何让我的应用程序运行或等到我的“任务”完成?
- r - R:如何计算最近 2 年
- jdbc - 尽管用户存在且凭据正常,但 Ping-Connection-Pool 向 DB 抛出拒绝访问
- javascript - 如何在reactjs中获取下拉输入的默认值