Answer the question
In order to leave comments, you need to log in
How to parse a request into several variables in a telegram bot?
Essence of the question: The user sends a few words to the telegram bot. It is necessary to parse this query into several variables, compose an sql query from them and return the result to the user.
I can't find information anywhere. Please give advice on how to implement this.
Answer the question
In order to leave comments, you need to log in
Request example? It must follow some specific form, I hope?
If the query simply consists of a few words separated by spaces (optionally, a quoted string counts as one word), then look into shlex . But apart from how to "split the string by spaces and escape quotes" shlex can do little.
If you need something like a command line (with features like --keys and optional parameters), look into argparse . Here you have data type conversion (i.e. "this parameter must be a number"), and usage information, and much more.
If something even more tricky, then look towards regular expressions. They allow you to recognize rather intricate patterns, but a lot will have to be done manually.
If you need an analysis of natural speech ... this is a chore.
So I would look at argparse first. It is sharpened for command line arguments, but chatbots are not far from it.
You can use the split() method.
For example:
responce = '#word1 #word2 #word3'
words = [word.strip() for word in responce.split('#') if word]
print(words)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question