PHP Messenger bot 通用模板点击
PHP messenger bot generic template click
我正在使用 PHP 和 curl 开发聊天机器人。目前,我使用通用模板发送 JSON,但我不知道如何检索用户选择。我想获取单击哪个按钮以供以后使用的信息。我试图检查整个响应,但它也失败了。我在控制台中没有看到任何错误。我没有使用任何框架。
$input = json_decode(file_get_contents('php://input'), true);
$sender = $input['entry'][0]['messaging'][0]['sender']['id'];
$message = $input['entry'][0]['messaging'][0]['message']['text'];
$message_to_reply = '';
if($message=="tshirt")
{
$jsonData = '{
"recipient":{
"id":"'.$sender.'"
},
"message":{
"attachment":{
"type":"template",
"payload":{
"template_type":"generic",
"elements":[
{
"title":"Object1",
"subtitle":"Description",
"buttons":[
{
"type":"postback",
"title":"L",
"payload":"L_1"
},{
"type":"postback",
"title":"XL",
"payload":"XL_1"
}
]
}
]
}
}
}
}';
}
if($message=="XL_1")
{
$jsonData = '{
"recipient":{
"id":"'.$sender.'"
},
"message":{
"text":"Added to basket"
}
}';
}
if($message=="L_1")
{
$jsonData = '{
"recipient":{
"id":"'.$sender.'"
},
"message":{
"text":"Added to basket"
}
}';
}
$url = 'https://graph.facebook.com/v2.6/me/messages?access_token='.$access_token;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
if(!empty($input['entry'][0]['messaging'][0]['message'])){
$result = curl_exec($ch);
}
我找到了解决方法。 JSON 通用模板请求与普通消息有点不同。我做的步骤:
添加回传变量
$postback = $input['entry'][0]['messaging'][0]['postback']['payload'];
更改服务器回复的 if 语句
if(!empty($input['entry'][0]['messaging'][0]['message']) | !empty($input['entry'][0]['messaging'][0]['postback']['payload'])){
我正在使用 PHP 和 curl 开发聊天机器人。目前,我使用通用模板发送 JSON,但我不知道如何检索用户选择。我想获取单击哪个按钮以供以后使用的信息。我试图检查整个响应,但它也失败了。我在控制台中没有看到任何错误。我没有使用任何框架。
$input = json_decode(file_get_contents('php://input'), true);
$sender = $input['entry'][0]['messaging'][0]['sender']['id'];
$message = $input['entry'][0]['messaging'][0]['message']['text'];
$message_to_reply = '';
if($message=="tshirt")
{
$jsonData = '{
"recipient":{
"id":"'.$sender.'"
},
"message":{
"attachment":{
"type":"template",
"payload":{
"template_type":"generic",
"elements":[
{
"title":"Object1",
"subtitle":"Description",
"buttons":[
{
"type":"postback",
"title":"L",
"payload":"L_1"
},{
"type":"postback",
"title":"XL",
"payload":"XL_1"
}
]
}
]
}
}
}
}';
}
if($message=="XL_1")
{
$jsonData = '{
"recipient":{
"id":"'.$sender.'"
},
"message":{
"text":"Added to basket"
}
}';
}
if($message=="L_1")
{
$jsonData = '{
"recipient":{
"id":"'.$sender.'"
},
"message":{
"text":"Added to basket"
}
}';
}
$url = 'https://graph.facebook.com/v2.6/me/messages?access_token='.$access_token;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
if(!empty($input['entry'][0]['messaging'][0]['message'])){
$result = curl_exec($ch);
}
我找到了解决方法。 JSON 通用模板请求与普通消息有点不同。我做的步骤:
添加回传变量
$postback = $input['entry'][0]['messaging'][0]['postback']['payload'];
更改服务器回复的 if 语句
if(!empty($input['entry'][0]['messaging'][0]['message']) | !empty($input['entry'][0]['messaging'][0]['postback']['payload'])){