Insert data step by step in telegram bot

1.4k views Asked by At

I am creating a telegram bot with php. I need to let user insert data step by step when they run the command /order. I do an example:

User: /order

Bot: I will help you to make an order

Bot: Insert the name

User: Ciccio (he has typed the name)

Bot: Ok, now insert your surname

User: Pasticcio (he has typed the surname)

and so on...

I thought I was in the correct way to reach my purpose... but not... something does not work... Here my code:

elseif(strcmp($text, "/order") === 0) <-Here the command
{
    $response =
        "I will help you.\n"
        ."\n"
        ."Insert your name:";

    $parameters = ['chat_id' => $chatId, "text" => $response, "parse_mode" => "Markdown"];
    $parameters["method"] = "sendMessage";
    echo json_encode($parameters);
    
    $action_parameters = ['chat_id' => $chatId, "action" => "typing"];
    $action_parameters["method"] = "sendChatAction";
    echo json_encode($action_parameters);

    $parameters2 = array('chat_id' => $chatId, "text" => "good! Now insert the surname");
    $parameters2["method"] = "sendMessage";
    echo json_encode($parameters2);
}

the code stop after I visualize the first message... it is like after I do my first echo json_encode($parameters); no code is runned anymore...

How can I reach my purpose?

Thank you

2

There are 2 answers

1
Sean Wei On

Do you use JSON response when received Webhook updates?

If yes, you need to make a request instead of print it to HTTP Response Body and log user input to your own database.

0
91DarioDev On

for any message your bot receives, you check the step of the user in your bot and you will send the right function (so the right question).

to do this you have to store users' steps. Please read my answer to this question.