Facebook Messenger API:发送结构化消息(Java)
问题描述:
使用Facebook Messenger使用基于官方文档的通用模板发送结构化消息here。使用Java构建JSON对象。每当我将JSON发送到Facebook,我都会得到一个响应“400-错误的请求”。我试着用一个在线工具进行比较,java生成的JSON与文档中提供的JSON相比,除了变量名之外别无其他。无法理解我在构建JSON时出错的地方。Facebook Messenger API:发送结构化消息(Java)
JSON从Java代码..
{
"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属性,我们必须白名单的域名否则信使平台将返回400错误。由于我不需要messenger_extensions属性,因此我删除了整个默认操作部分,现在messenger API返回了200个代码。如果你想将你的域名列入白名单,你可以按照下面的链接。
https://developers.facebook.com/docs/messenger-platform/thread-settings/domain-whitelisting
我从来没有使用Messenger的API,但是,你有没有尝试创建的相同顺序/名称的元素?也许API正在按照文档中提供的相同顺序等待名称......并且您正在请求中发送access_token? –
我试过仍然得到相同的错误 – Lucy
所以,我建议你记录你的回答中的错误。我认为'400错误请求'不是整个错误信息。可能你有更多关于可能对你有帮助的错误的信息。此外,尝试在发布之前将[root1]转换为JSON,如[据此]所述(http://stackoverflow.com/questions/36634453/facebook-messenger-api-send-structured-message?rq=1) –