迴應我在PHP中有着相對簡單的Facebook Messenger的機器人爲研究目的:Fecebook Messenger的機器人並不總是以用戶
$access_token = "xxxxxxx";
$challenge = $_REQUEST['hub_challenge'];
$verify_token = $_REQUEST['hub_verify_token'];
if ($verify_token === 'MY_VERIFICATION_TOKEN') {
echo $challenge;
}
$input = json_decode(file_get_contents('php://input'), true);
$sender = $input['entry'][0]['messaging'][0]['sender']['id'];
$message = $input['entry'][0]['messaging'][0]['message']['text'];
$url = 'https://graph.facebook.com/v2.6/me/messages?access_token='.$access_token.';
$ch = curl_init($url);
if($message=="hi")
{
$jsonData = '{
"recipient":{
"id":"'.$sender.'"
},
"message":{
"text":"hello!"
}
}';
}
$jsonDataEncoded = $jsonData;
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$result = curl_exec($ch);
和我的cron作業是一樣的開發者指南
curl -ik -X POST "https://graph.facebook.com/v2.6/me/subscribed_apps?access_token=xxxxxx"
所以基本上,除了連接和一個響應之外什麼也沒有。 當我親自發送「hi」(作爲頁面所有者和應用程序所有者)時,我的機器人總是響應正確,但是當其他人試圖說hi-bot有時會迴應,有時候不會(通常不會)
此外,當我訪問我的腳本的URL,它給我的錯誤:
{"error":{"message":"(#100) The parameter recipient is required","type":"OAuthException","code":100,"fbtrace_id":"DvrO1UEw5BJ"}}
請幫我設置這種權利。
「*爲研究目的*」不錯*免責聲明*;) – Marcus