我是电报机器人的新程序员,我为自己的机器人准备了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我的机器人发送给用户时写上你的名字,然后用户发送给我,我发送给用户输入你的姓氏机器人,我怎么知道答案是哪个?我不知道我应该在交换块中默认写什么,以便知道用户给我发送了名字还是姓氏
答案 0 :(得分:0)
您可以与发送{user_id}.json
命令的用户user_id
创建一个名为/new
的文件,并使用以下结构将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;)