我为 Facebook 开发了一个非常简单的聊天机器人,我将 Facebook 发送的 JSON 文件保存到一个新文件 fb.txt 中。在这个 JSON 中,消息数组中的一个字段是从 Facebook 发送的文本,但情况似乎不再如此。
<?php
if (isset($_REQUEST['hub_challenge'])) {
$challenge = $_REQUEST['hub_challenge'];
$token = $_REQUEST['hub_verify_token'];
}
if($token == "macross93") {
echo $challenge;
}
$input = json_decode(file_get_contents('php://input'), true);
$userID = $input['entry'][0]['messaging'][0]['sender']['id'];
$message = $input['entry'][0]['messaging'][0]['message']['text'];
echo $userID." and ".$message;
file_put_contents("fb.txt", file_get_contents($userID));
$access_token="EAAElJTd6foABAGhCjYcL3ikrPMFkOZBihqb5jzVZCuCnJd8oL4jFZAZCV26EYiOXGLzkz6lKZAMDXBKDsFwWFVYBVfSguV2ZCZCgGNCOLCxWWHQYC8of3DJks4AmEAVFo2DB29TajZBGQDUYtQqOWaig3M2m5ioeOElY7CPXZB3eEgAZDZD";
$url="https://graph.facebook.com/v2.8/me/messages?access_token=EAAElJTd6foABAGhCjYcL3ikrPMFkOZBihqb5jzVZCuCnJd8oL4jFZAZCV26EYiOXGLzkz6lKZAMDXBKDsFwWFVYBVfSguV2ZCZCgGNCOLCxWWHQYC8of3DJks4AmEAVFo2DB29TajZBGQDUYtQqOWaig3M2m5ioeOElY7CPXZB3eEgAZDZD";
$jsonData = "{
'recipient': {
'id': $userID
},
'message': {
'text': 'finally, the bait is over'
}
}";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
if(!empty($input['entry'][0]['messaging'][0]['message'])) {
curl_exec($ch);
}
我现在收到的 JSON 是:
{
"object": "page",
"entry": [{
"id": "632423650282074",
"time": 1489785206037,
"messaging": [{
"sender": {
"id": "892399990863615"
},
"recipient": {
"id": "632423650282074"
},
"timestamp": 1489785206027,
"read": {
"watermark": 1489785205361,
"seq": 0
}
}]
}]
}
任何想法如何像我以前那样阅读消息?