我开始使用Telethon库构建一个机器人,但找不到一种清晰的方法来将命令与文本分开。
例如,使用python-telegram-bot库,我可以检索命令后的第一个参数,如下所示:
def test(update, context):
arg_1 = context.args[0]
print(arg_1) # Prints the first word after the command所以..。有没有办法用Telethon制作这样的东西?(我正在使用这段代码将文本从命令中分离出来,但我认为这种方法并不是很好):
txt = event.message.message.lower()
try:
txt.split("!raw ")[1]
text = event.message.message[4:]
except Exception as e:
text = ""
if text:
do_something()发布于 2021-10-03 13:14:23
如果您有一个按以下方式定义的处理程序:
@client.on(events.NewMessage(pattern=r'!raw (\w+)'))
async def handler(event):
...然后,您可以访问event.pattern_match
arg = event.pattern_match.group(1)
print(arg) # first word但是,使用.split()也是可以的:
parts = event.raw_text.split()
if len(parts) > 1:
arg = parts[1](您还可以通过告诉用户何时使用了错误的命令来构建更好的UX。)
https://stackoverflow.com/questions/69424963
复制相似问题