Answer the question
In order to leave comments, you need to log in
Flask + VK API. Creation of a survey bot. How to validate server requests in Flask?
Good evening! I decided to create a survey bot for VK in Python, I used the flask microframework. Code:
Accordingly, every time there is a request to the server (i.e. when a user sends a message to the group's PM), I call the "processing" function and further along the knurled one. But I need that after the user has sent a certain character ('1', let's say) I have already called a new function (the beginning of the survey). In this function, the first question would be asked, then the user would answer it, then the second question would be asked, etc.
How can this be implemented? I know how to run a function when a certain character is entered. And how can I make the message in this function appear only when the user replied to the previous one?
Output example:
B - bot,
(B): Type "1" to start the poll
(P): 1
(B): How do you feel about stackoverflow?
(P): Positive
(B): How do you feel about python?
(P): OK
(B): Thank you for taking the survey!
Answer the question
In order to leave comments, you need to log in
Well, so what prevents after entering 1 to generate a poll session ID and work with it?
Are we going to store the survey results in some database?
Store user sessions in the same place, the logic is correct: We check if there is a user with id vk in our database, if not, we create a record, if there is, we check the id of the question that the user should currently answer, write down the answer, ask the next question, write down the id new question to the user.
The structure is the simplest of three columns - id with autoincrement, id vk, question id.
With this approach, it is easy to implement the ability to return to the previous question, not to lose data when the python is down.
You need to use a database. The simplest structure that came to mind is two tables in MySQL. The first stores the status, question number and user ID, and the second stores all user answers, question number, user ID and the answer to this question.
You can get by with one thing - to check immediately in the second database how many questions the user has passed, but with large data there will be problems.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question