0

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..

Lucy
  • 1,812
  • 15
  • 55
  • 93
  • I've never used Messenger API but, have you tried to create the elements in the same order/names? Maybe the API is waiting the names in the same order provided in the docs...and, you're sending the access_token within the request? – Elmer Dantas Feb 09 '17 at 08:31
  • I tried that still getting the same error – Lucy Feb 09 '17 at 08:55
  • So, I suggest you log the errors from your response. I think `400 bad request` is not the whole error message..probably you have more information about the error that might help you. Also, try to convert your `root1` to JSON before posting as was said [here](http://stackoverflow.com/questions/36634453/facebook-messenger-api-send-structured-message?rq=1) – Elmer Dantas Feb 09 '17 at 09:07

1 Answers1

0

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

Lucy
  • 1,812
  • 15
  • 55
  • 93