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

9wbgstp7  于 2023-05-27  发布在  PHP
关注(0)|答案(1)|浏览(146)

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

所以我想当用户写/新我的机器人发送给用户写你的名字,然后用户发送给我我发送给用户输入你的姓氏机器人我怎么知道答案是哪一个?我不知道我应该写在默认开关块知道用户给我的名字或姓氏

mwg9r5ms

mwg9r5ms1#

您可以使用发送/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);
}

相关问题