看到很紅的 PPAV 心動研究了一下....
注意事項
* 帳號需要粉絲專頁限定
* call back url 需要 https
* 正式上線需要提出申請,須5~7天審核
* 測試期間需要測試帳號增加至測試人員才會work
簡易步驟
1. 建立或使用既有粉絲團
2. 建立或使用既有應用程式
3. 新增產品 -> Webhooks
新訂閱內容 -> Page
輸入 1. 回傳網址
2. Vertify Token ( 供程式初始設定連接驗証 )
3. 訂閱欄位 勾 message_deliveries、messages、messaging_optins 和 messaging_postbacks。
4. 新增產品 -> Messager
權杖產生 -> 選擇粉絲專業 -> 取得產生Token ( 供程式回傳訊息使用 )
Webhooks -> select subscribe page
5. 回傳程式 (PHP)
$challenge = $_REQUEST['hub_challenge'];
$verify_token = $_REQUEST['hub_verify_token'];
if ($verify_token === 'MY_VERIFICATION_TOKEN') { // 步驟2 輸入的 Vertify Token
echo $challenge;
}
$input = json_decode(file_get_contents('php://input'), true);
$sender = $input['entry'][0]['messaging'][0]['sender']['id']; //傳送者 ID
$message = $input['entry'][0]['messaging'][0]['message']['text']; //傳送過來的訊息
//API Url
$url = 'https://graph.facebook.com/v2.8/me/messages?access_token=<?TOKEN?>; // 步驟4 產生的 Token
//Initiate cURL.
$ch = curl_init($url);
//The JSON data.
$jsonData = '{
"recipient":{
"id":"'.$sender.'"
},
"message":{
"text":"your input is: '.$message.'! \n Sorry. I Donot know!"
}
}'; //回傳的訊息
//Encode the array into JSON.
$jsonDataEncoded = $jsonData;
//Tell cURL that we want to send a POST request.
curl_setopt($ch, CURLOPT_POST, 1);
//Attach our encoded JSON string to the POST fields.
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);
//Set the content type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
//Execute the request
if(!empty($input['entry'][0]['messaging'][0]['message'])){
$result = curl_exec($ch);
}