簡體   English   中英

Facebook Messenger API:發送結構化消息(Java)

[英]Facebook Messenger API: Send Structured Message(Java)

在使用Facebook Messenger使用基於此處官方文檔的通用模板發送結構化消息時。 正在使用Java構造JSON對象。 每當我將JSON發送到Facebook時,都會收到響應“ 400-錯誤請求”。 我嘗試使用在線工具進行比較,將Java生成的JSON與文檔中提供的JSON進行比較,除了變量名外,沒有什么不同。 無法理解在構造JSON時哪里出錯了。

從Java代碼生成的JSON。

        {
"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"
}

}

對應的Java代碼

         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);

如您所見,將上述json與官方文檔中提供的示例進行比較時,只有元素的順序有所不同。 在過去2天內一直在解決此問題。.請幫助。

發現我的錯誤!

每當我們嘗試使用messenger_extensions屬性時,我們都必須將域名列入白名單,否則Messenger平台將返回400錯誤。 由於我不需要messenger_extensions屬性,因此刪除了整個默認操作部分,現在messenger API返回了200個代碼。 如果您想將自己的域名列入白名單,可以點擊以下鏈接。

https://developers.facebook.com/docs/messenger-platform/thread-settings/domain-whitelisting

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM