phptelegram-botphp-telegram-bot

know the answere of user to the telegram bot


i am a new programmer in the telegram bot i have a php code for my bot

const TOKEN = "https://api.telegram.org/bot<TOKEN>";
const USER_NAME = "@testbot";
const NAME = "Test bot";
$jata = json_decode(file_get_contents("php://input"), true);

define("CHAT_ID", $jata["message"]["chat"]["id"]);
define("MESSAGE", $jata["message"]["text"]);

switch (MESSAGE) {
    case "/start":
        SendMessage("welcome to bot");
    break;
    case "/new":
        SendMessage("write your name: ");
    break;
    default:
        // ????????????????????????????????????????
}

function SendMessage($msg)
{
    $ch = curl_init();
    curl_setopt_array($ch, array(
        CURLOPT_URL => TOKEN . "/sendMessage",
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_POST => true,
        CURLOPT_POSTFIELDS => array(
            "chat_id" => CHAT_ID,
            "text" => $msg
        )
    ));
    curl_exec($ch);
    curl_close($ch);
}

so i want when the user write /new my bot send to user write your first-name and then user sent to me i send to user enter your last-name bot how can i know the answered to which one? i dont know what i should write in default in switch block to know the user sent me first-name or last-name


Solution

  • You can create a file called {user_id}.json with the user_id of the user who sent the /new command and write in it a json object with this structure:

    {
      "first_name": "",
      "last_name": "",
    }
    

    and when the user send to the bot his first-name, the bot check if the first_name key of the json object is an empty string, if so replace it with the text of the message sent by the user and ask for the last-name. When the user send the last-name, you can check if the first-name is empty (it will not be) and so you know that the user has already sent the first-name, so you can store the last-name in the respective key.

    Code:

    <?php
    const TOKEN = "https://api.telegram.org/bot<TOKEN>";
    const USER_NAME = "@testbot";
    const NAME = "Test bot";
    $jata = json_decode(file_get_contents("php://input"), true);
    
    define("CHAT_ID", $jata["message"]["chat"]["id"]);
    define("MESSAGE", $jata["message"]["text"]);
    
    $user_id = $jata["message"]["user"]["id"];
    
    
    switch (MESSAGE) {
        case "/start":
            SendMessage("welcome to bot");
        break;
        case "/new":
            SendMessage("write your name: ");
            // Create a json object
            $json = [
                "first_name" => "",
                "last_name" => "",
            ];
            // Store the json object in a file called {user_id}.json
            file_put_contents("$user_id.json", json_encode($json));
        break;
        default:
            // Check if the file with the user's data exists and the message text is not empty (the message text is empty with media messages like photos, videos...)
            if (file_exists("$user_id.json") && !empty(MESSAGE)){
                // Read the user's data and decode it in an array
                $data = json_decode(file_get_contents("$user_id.json"), true);
                // Check if the first_name key in the array is an empty string
                if ($data['first_name'] == ""){
                    // If yes replace the first_name key of the object with the message content
                    $data['first_name'] = MESSAGE;
                    SendMessage("write your surname: ");
                }else{
                    // If not the user has already sent the first-name and he just sent the last_name
                    $data['last_name'] = MESSAGE;
                    $first_name = $data['first_name'];
                    $last_name = $data['last_name'];
                    SendMessage("Your name is $first_name $last_name");
                }
                // In any case, store the array with the user's data to the file
                file_put_contents("$user_id.json", json_encode($data));
            }
    }
    
    
    // To get the first_name from a user by his user_id
    if (file_exists("$user_id.json")){
        $user_info = json_decode(file_get_contents("$user_id.json"), true);
    
        $first_name = $user_info['first_name'];
        $last_name = $user_info['last_name'];
    }
    
    function SendMessage($msg)
    {
        $ch = curl_init();
        curl_setopt_array($ch, array(
            CURLOPT_URL => TOKEN . "/sendMessage",
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_POST => true,
            CURLOPT_POSTFIELDS => array(
                "chat_id" => CHAT_ID,
                "text" => $msg
            )
        ));
        curl_exec($ch);
        curl_close($ch);
    }