U
U
undisman2016-11-07 01:17:30
Messengers
undisman, 2016-11-07 01:17:30

How to carry out some kind of session with a user when using a Telegram bot?

Hi all! Comrades, a very important question to understand in writing a Telegram bot. I write in PHP. And a question - how to carry out a certain similarity of session with the user? Those. I calmly determine the user by chat-id. But it is not at all clear how to store the system state (history) of communication with the user. Let's say I have 3 questions that I want to ask a user: what is your name, your age and your city. The bot sends the user "What is your name?", the user drives in his name, but how do I understand that this is the answer to my question about age, and not to some other one? As far as I understand, the system does not remember what the bot wrote (what question it asked). Thank you very much in advance!

Answer the question

In order to leave comments, you need to log in

3 answer(s)
T
T_y_l_e_r, 2016-11-07
@T_y_l_e_r

Everything is simple there
Create a table and store the history by user id, or rather the last question asked and the key true, false - answered, not answered
This is as an option

N
nllm, 2016-11-07
@nllm

There have been many similar questions. See:
How to consistently get telegram bots consistent values ​​from the user?
How to accept user response in Telegram Bot?
How to fulfill a condition in PHP?

D
djnightart, 2017-02-06
@djnightart

I do this:
When you press / start, create a clean file with the name (chat_id) or if there is already one, clear it!
The user is asked the question: What is your name?
We write the username to the file.
Then your city? - We add the city to the file through the ^ sign, for example!
and so on. And to find out where the user is, we use the simplest construction:

$data = explode("^", file_get_cintents($chat_id) );
if($data[0]){
    if($data[1]){
        if($data[2]){
         // исполняем код, если 3 значение есть
        }else{
         // исполняем код, если 2 значение есть
        }
   }else{
   // исполняем код, если 1 значение есть
   }
}else{
// Файл пустой, начит пишем в него имя!
}

Everything is really very simple!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question