In using Facebook messenger to send structured messages using generic template based on official documentation here. 'm using Java to construct the JSON object. Whenever I send the JSON to Facebook I get a response "400- bad request"I. I tried comparing using an online tool, the java generated JSON to that provided in the documentation and apart from the variable names nothing else is different. Can't understand where I'm going wrong in constructing the JSON.
JSON Generated from Java Code..
{
"message": {
"attachment": {
"payload": {
"elements": [
{
"buttons": [
{
"title": "show website",
"type": "web_url",
"url": "https://google.com"
},
{
"payload": "sample payload",
"title": "Hi There",
"type": "postback"
}
],
"default_action": {
"fallback_url": "https://www.google.com/",
"messenger_extensions": true,
"type": "web_url",
"url": "https://www.google.com/",
"webview_height_ratio": "tall"
},
"image_url": "https://s3-ap-southeast-1.amazonaws.com/primary-4495.png",
"subtitle": "Sample Sub Title",
"title": "Sample Title"
}
],
"template_type": "generic"
},
"type": "template"
}
},
"recipient": {
"id": "988459377921053"
}
}
Corresponding Java Code..
JSONObject root1 = new JSONObject();
JSONObject c01 = new JSONObject();
JSONObject c11 = new JSONObject();
JSONObject attachment = new JSONObject();
JSONObject payload = new JSONObject();
JSONArray arrayButton= new JSONArray();
JSONArray arrayelements= new JSONArray();
JSONObject elementsObj = new JSONObject();
JSONObject defaultAction = new JSONObject();
JSONObject buttons1 = new JSONObject();
JSONObject buttons2 = new JSONObject();
root1.put("recipient", c01);
c01.put("id", userId);
root1.put("message", c11);
c11.put("attachment", attachment);
attachment.put("type", "template");
attachment.put("payload", payload);
payload.put("template_type", "generic");
payload.put("elements", arrayelements);
arrayelements.put(elementsObj);
elementsObj.put("title", "Sample Title");
elementsObj.put("image_url", "https://s3-ap-southeast-1.amazonaws.com/primary-4495.png");
elementsObj.put("subtitle", "Sample Sub Title");
elementsObj.put("default_action", defaultAction);
defaultAction.put("type", "web_url");
defaultAction.put("url", "https://www.google.com/");
defaultAction.put("messenger_extensions", true);
defaultAction.put("webview_height_ratio", "tall");
defaultAction.put("fallback_url", "https://www.google.com/");
buttons1.put("type", "web_url");
buttons1.put("url", "https://google.com");
buttons1.put("title", "show website");
arrayButton.put(buttons1);
buttons2.put("type", "postback");
buttons2.put("title", "Hi There");
buttons2.put("payload", "sample payload");
arrayButton.put(buttons2);
elementsObj.put("buttons", arrayButton);
As you can see when comparing the above json with the sample one provided in the official documentation, only the order of elements is different. Stuck on this problem for the past 2 days..Please help..
Found my mistake!!
Whenever we try to use messenger_extensions property we have to whitelist the domain name otherwise messenger platform will return 400 error. Since I didn't need messenger_extensions property, I removed the entire default action section and now messenger API is returning 200 code. In case you want to whitelist your domain you can follow the below link.
https://developers.facebook.com/docs/messenger-platform/thread-settings/domain-whitelisting