2017-06-05 85 views
2

我想在我的回調網址中獲得「開始」按鈕負載值,但沒有發生。但我可以得到用戶輸入的文本消息,並基於文本消息,我也能夠發送響應。讓我們給出一個例子,如果用戶鍵入PHP(作爲文本消息)我可以發送關於PHP簡短描述和我顯示2按鈕1是的詳細信息它包含的URL,和一個更按鈕繼續聊天enter image description here如何在Facebook messenger bot中的回調鏈接中獲取回發按鈕值?

如果用戶單擊開始按鈕,我沒有得到我的回調url中相同的值。請注意我已經選擇了所有的事件在我的應用程序設置enter image description here

我的回調代碼

<?php 
date_default_timezone_set('Asia/Calcutta'); 
$access_token = "SFt44EAAWnrB7cYxIBAKeTHDZA7PuFOveOLs3OgZBPLgjMN7k8hXZAtBHktERWlm4uZCkSDVRo9r7PhKUZC1celZA9117Xcc6FDUKZCEbxRpZCM80rVDlb4H7ZAJkDVKJ2iuIFkDBoeG37a60KZBkCEtTVlCFIG8YWsQtHjKa7xP0TCF1kzZAcAZDZD"; 
$verify_token = "hdb201744_token"; 
$hub_verify_token = null; 
if(isset($_REQUEST['hub_challenge'])) 
{ 
$challenge = $_REQUEST['hub_challenge']; 
$hub_verify_token = $_REQUEST['hub_verify_token']; 
} 
if ($hub_verify_token === $verify_token) 
{ 
echo $challenge; 
} 

$message_to_reply = ''; 
$input = json_decode(file_get_contents('php://input'), true); 
$sender = $input['entry'][0]['messaging'][0]['sender']['id']; 

$postback = $input['entry'][0]['messaging'][0]['postback']['payload']; 
if (!empty($postback)) { 
$message = $input['entry'][0]['messaging'][0]['postback']['payload']; 

} 
else 
{ 
$message = $input['entry'][0]['messaging'][0]['message']['text']; 

} 

$message = trim($message); 


if($message=='start') 
{ 
$message_to_reply = "Thanks to continue, You can use bot now"; 

} 


else 
{ 


function getDescription($keyword) 
{ 
$url='http://lookup.dbpedia.org/api/search.asmx/KeywordSearch?QueryString='.urlencode($keyword).'&MaxHits=1'; 
$xml=simplexml_load_file($url); 
return $xml->Result->Description; 
} 

$message_to_reply = getDescription($message); 



} 


$message_to_reply = trim($message_to_reply); 

//API Url 
$url = 'https://graph.facebook.com/v2.6/me/messages?access_token='.$access_token; 
//Initiate cURL. 
$ch = curl_init($url); 
//The JSON data. 
$jsonData = '{ 
"recipient":{ 
"id":"'.$sender.'" 
}, 
"message":{ 

"attachment":{ 
"type":"template", 
"payload":{ 
"template_type":"button", 
"sharable":true, 
"text":"'.$message_to_reply.'", 
"buttons":[ 
{ 
"type":"web_url", 
"url":"http://php.net/manual/en/intro-whatis.php", 
"title":"More Info" 
}, 
{ 
"type":"postback", 
"title":"Start", 
"payload":"start" 
} 
] 
} 
} 
} 
}'; 

//$abb = json_encode($jsonData); 
//print_r($abb); 
//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')); 
//curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded')); 
//Execute the request 
if(!empty($input['entry'][0]['messaging'][0]['message'])){ 
$result = curl_exec($ch); 
} 
?> 

我使用PHP。

+0

我得到了迴應,現在,是因爲我已刪除了最後2行,如果條件 – Vetrivel

回答

0

試圖改變這些線

$回發= $輸入[ '條目'] [0] [ '消息'] [0] [ '回傳'] [ '有效載荷'];

if(!empty($ postback)){ $ message = $ input ['entry'] [0] ['messaging'] [0] ['postback'] ['payload'];

}

有了這些

$回發= json_decode($輸入[ '條目'] [0] [ '消息'] [0] [ '回傳'],真) ;

if(!empty($ postback)){ $ message = $ postback ['payload'];

}