Answer the question
In order to leave comments, you need to log in
Telegram bot in PHP. How to save user's response state?
I’m just starting to get acquainted with the Telegram API for bots (before that, all the experience of developing bots was reduced to writing the simplest useful things for myself, like using the /stat command to send some summary information collected by a PHP script from various sources), so I would like to clarify the following. Suppose we are writing a simple bot in PHP without using third-party libraries and classes. Is it possible to implement the functionality of "polling" a user with saving response states without using MySQL, saving to files, etc.? If exaggerated, then the problem statement is as follows:
- We have a host with Apache + PHP installed, SSL support is configured, the bot is launched and configured via setWebhook.
- Implement a simple user survey, for example, ask him for his name, year of birth and zodiac sign, and then display some picture in response to him (for example, his name on a beautiful background with a zodiac sign overlay).
And here, even at the "theoretical stage", I had the following question. In fact, we need to ask the user for three input parameters param1 (name), param2 (year), param3 (zodiac). The "survey" of the user is considered completed if he correctly entered all three parameters, only after that the result is given - a picture (well, or some other actions are performed with the entered data). What are the options for tracking the current state of a user in a survey from a script? The Telegram server "pulls" the script every time the user sends a message and at the same time is absolutely unaware that the user's next message refers to any stage of the script script execution.
For example, the user gave the /poll command and thereby began participating in the survey. After that, the bot asks him the first question, a la enter param1. If the data entered by the user is validated, then the bot goes to the next step and asks the second question, enter param2, etc. If the parameter is entered incorrectly, for example, the year 3027 is specified, then the bot must "request" the user, i.e. request re-entry of data. So, the question is - what is the best way to keep track of the status (at what stage is he, entering the name, entering the year, entering the zodiac sign) of the user in this survey?
If we had MySQL, then it would be possible to store the user id and some state value in the database and, based on this, build the logic of the bot. Those. if state=1, for example, then the user is participating in the survey and answering the first question, if state=2, then answering the second, and so on. The same applies to storing states in files. At least that's the most logical thing that came to my mind. But maybe there is some more optimal solution (for example, you can somehow pass some state parameter to the Telegram server, which will come along with the next user message) or something else like that?
How would you implement this in the described starting conditions? (Let me remind you that the task is of an "educational" nature, there are probably some libraries, classes, etc. that use the database and allow you to implement everything described in one line of code, but I'm much more interested in understanding the principle of how to save the "user state" and determine on what step of the "scenario" it is without using them, i.e. how it is implemented in most bots that use interactive dialogs with the user).
Answer the question
In order to leave comments, you need to log in
The sendMessage()
method of sending a message does not offer any parameter to be "thrown" to the client for subsequent requests. Except keyboards.
Probably, you can try to give Inline Keyboard for subsequent answers containing his previous answers (up to 64 bytes) in their parameters.
For example, the first question is to ask something long like first name and last name, so that he enters it in text. And the subsequent answers are accepted only through the prepared buttons.
But it all smells like a rusty bike. You 'd better think about how to store state on the server . Let the temporary in-memory storage be Memcached or Redis.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question