我正在尝试使用 PHP 代码为电报做 BOT。
基本上用户必须在插入之间进行选择:1)姓名 2)姓氏 3)地址
选择姓氏,他必须写下他的姓氏,我想将其存储到变量中,但是,如果我使用,$update = file_get_contents('php://input')我总是读“姓氏”(这是用户的第一个输入)
所以我认为我的问题是:如何file_get_contents('php://input')在我的程序的特定“时刻”中更改内容?
请阅读我的代码以获取更多详细信息,非常感谢!!!
<?php
$botToken = "MYTOKEN"; //token
$website = "https://api.telegram.org/bot".$botToken;
$update = file_get_contents('php://input'); //updates from telegram (JSON format)
$update = json_decode($update, TRUE);
$chatId = $update['message']['from']['id'];
$text = $update['message']['text'];
switch($text)
{
case "name";
sendMessage ($chatId,"You write name");
break;
case "surname";
sendMessage ($chatId,"You write surname");
sendMessage ($chatId,"Insert your surname");
/* here is my problem: what i have to do to "read" what the
user write now? i want to store his surname into a new
variable*/
break;
case "address";
sendMessage ($chatId,"You write address");
break;
default;
sendMessage ($chatId,"You don't write a valid command");
break;
}
function sendMessage($chatId,$text)
{
$url = $GLOBALS[website]."/sendMessage?
chat_id=$chatId&text=".urlencode($text);
file_get_contents($url);
}
?>