简体   繁体   English

如何在Facebook Messenger Bot的回调URL中获取回发按钮值?

[英]How to get Postback button value in the callback url in Facebook messenger bot?

I am trying to get "Start" button payload value in my call back url, its not happening. 我正在尝试在我的回叫网址中获取“开始”按钮有效负载值,但没有发生。 But i can get user typed text messages, and based on text messages I am able to send response as well. 但是我可以得到用户键入的文本消息,并且基于文本消息,我也能够发送响应。 Lets given an example if user typing PHP (as a text message) I can send short description about PHP and i am showing 2 button 1 is more info it contains url, and one more button continue to chat 让我们举个例子,如果用户输入PHP(作为文本消息),我可以发送有关PHP的简短说明,并且我正在显示2按钮1是更多信息,它包含url,还有一个按钮继续聊天 在此处输入图片说明

if user click Start button, i am not getting the value for the same in my callback url. 如果用户单击“开始”按钮,则无法在回调URL中获得相同的值。 Note i have selected all the events in my app settings 注意我已经在我的应用程序设置中选择了所有事件 在此处输入图片说明

And my callback code 还有我的回调代码

<?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);
}
?>

I am using php. 我正在使用php。

trying changing those lines 试图改变那些线

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

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

} }

With those 和那些

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

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

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM