Reputation: 23
I started building a bot with the Telethon library but couldn't find a clean way to separate the command from the text.
For example, with the python-telegram-bot
library, I can retrieve the first argument after the command like this:
def test(update, context):
arg_1 = context.args[0]
print(arg_1) # Prints the first word after the command
So... There's a way to make something like this using Telethon? (I'm using this piece of code to split text from command but I think this method is not really good):
txt = event.message.message.lower()
try:
txt.split("!raw ")[1]
text = event.message.message[4:]
except Exception as e:
text = ""
if text:
do_something()
Upvotes: 2
Views: 1535
Reputation: 7141
If you have a handler defined in the following way:
@client.on(events.NewMessage(pattern=r'!raw (\w+)'))
async def handler(event):
...
You can then access the event.pattern_match
:
arg = event.pattern_match.group(1)
print(arg) # first word
However, using .split()
is also okay:
parts = event.raw_text.split()
if len(parts) > 1:
arg = parts[1]
(And you can also build a better UX, by telling the user when they used the command wrong.)
Upvotes: 4