I am developing messenger bot using PHP and curl. Currently, I send JSON with the generic template but I don't know how to retrieve user choose. I want to get information which button was clicked for later use. I tried to check entire response but It failed too. I don't see any errors in the console. I am not using any framework.
$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);
}
I found how to fix that. JSON generic template request is a little bit diffrent than normal message. Steps which I did:
Add postback variable
$postback = $input['entry'][0]['messaging'][0]['postback']['payload'];
Change if statement for server reply
if(!empty($input['entry'][0]['messaging'][0]['message']) | !empty($input['entry'][0]['messaging'][0]['postback']['payload'])){