知道用户对电报机器人的回答

know the answere of user to the telegram bot

我是电报机器人的新程序员,我有一个 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"]);

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);
}

所以我想当用户写 /new 我的机器人发送给用户写你的名字然后用户发送给我我发送给用户输入你的姓氏机器人我怎么知道回答哪个?我不知道我应该在 switch 块中默认写什么来知道用户发给我的名字或姓氏

您可以使用发送 /new 命令的用户的 user_id 创建一个名为 {user_id}.json 的文件,并在其中写入具有以下结构的 json 对象:

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

并且当用户向机器人发送他的名字时,机器人会检查 json 对象的 first_name 键是否为空字符串,如果是,则将其替换为文本用户发送的消息并询问姓氏。当用户发送姓氏时,您可以检查名字是否为空(不会),这样您就知道用户已经发送了名字,因此您可以将姓氏存储在各自的钥匙。

代码:

<?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);
}

如果此回答对您有帮助,请考虑mark it as accepted ;)