如何在Facebook messenger bot的回调网址中获取回传按钮值?

时间:2017-06-05 07:42:55

标签: php callback bots webhooks facebook-messenger

我正在尝试在我的回调网址中获取“开始”按钮有效负载值,它没有发生。但我可以获得用户输入的短信,并根据短信我也能发送回复。让我们举一个例子,如果用户输入PHP(作为短信),我可以发送关于PHP的简短描述,我显示2按钮1是更多信息它包含网址,还有一个按钮继续聊天 enter image description here

如果用户点击“开始”按钮,我的回调网址中没有得到相同的值。请注意,我已在应用设置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。

1 个答案:

答案 0 :(得分:0)

尝试更改这些行

  

$ postback = $ input ['entry'] [0] ['messaging'] [0] ['postback'] ['payload'];

     

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

     

}

用那些

  

$回发= json_decode($输入[ '条目'] [0] [ '消息'] [0] [ '回传'],TRUE);

     

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

     

}