Facebook Messenger API:发送结构化消息(Java)
Facebook Messenger API: Send Structured Message(Java)
在使用 Facebook Messenger 使用基于官方文档的通用模板发送结构化消息时 here。我正在使用 Java 构造 JSON 对象。每当我将 JSON 发送到 Facebook 时,我都会收到“400-错误请求”的响应。我尝试使用在线工具进行比较,生成的 java 与文档中提供的 JSON 相比,除了变量名之外没有其他不同。无法理解我在构建 JSON 时哪里出错了。
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 属性 时,我们必须将域名列入白名单,否则信使平台将 return 400 错误。因为我不需要 messenger_extensions 属性,所以我删除了整个默认操作部分,现在 messenger API 是 returning 200代码。如果您想将您的域列入白名单,您可以按照以下 link.
https://developers.facebook.com/docs/messenger-platform/thread-settings/domain-whitelisting
在使用 Facebook Messenger 使用基于官方文档的通用模板发送结构化消息时 here。我正在使用 Java 构造 JSON 对象。每当我将 JSON 发送到 Facebook 时,我都会收到“400-错误请求”的响应。我尝试使用在线工具进行比较,生成的 java 与文档中提供的 JSON 相比,除了变量名之外没有其他不同。无法理解我在构建 JSON 时哪里出错了。
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 属性 时,我们必须将域名列入白名单,否则信使平台将 return 400 错误。因为我不需要 messenger_extensions 属性,所以我删除了整个默认操作部分,现在 messenger API 是 returning 200代码。如果您想将您的域列入白名单,您可以按照以下 link.
https://developers.facebook.com/docs/messenger-platform/thread-settings/domain-whitelisting