2017-06-05 87 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'];

}