【发布时间】:2017-11-05 22:31:59
【问题描述】:
我试图在我的回调 url 中获取“开始”按钮的有效负载值,但它没有发生。但是我可以获得用户输入的短信,并且基于短信我也可以发送响应。举个例子,如果用户输入 PHP(作为文本消息)我可以发送关于 PHP 的简短描述,我显示 2 个按钮 1 是 more info 它包含 url,还有一个按钮 continue聊天
如果用户单击“开始”按钮,我不会在我的回调 url 中获得相同的值。请注意,我已在我的应用设置中选择了所有事件
还有我的回调代码
<?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。
【问题讨论】:
-
我现在得到了回复,因为如果条件我已经删除了最后两行
-
你删除了最后 2 行的哪个条件?
标签: php callback bots webhooks facebook-messenger