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
Related
I've been trying to figure out why constructing a org.json.JSONObject from a string json would not work for very long.
Here is the json string (pretty)
{
"status": 200,
"message": "200 Request is valid",
"job": {
"Id": 1,
"Jobtitle": "Test Job",
"Description": "Asthma",
"Medicalcondition": "Asthma",
"Language": "English",
"Racereligion": "Chinese",
"Agegender": "50, Female",
"Hourlyprice": 42,
"Type": "caregiver",
"Date": "2019-05-12T00:00:00+08:00",
"Starttime": "2018-07-20T12:15:00+08:00",
"Endtime": "2018-07-20T18:15:00+08:00",
"Address": "1 Cluny Road",
"Latitude": 1.3152057,
"Longitude": 103.8162553,
"Creator": "xiurobert"
}
}
and minified (the one that I'm attempting to construct a JSONObject from)
String json = "{\"status\":200,\"message\":\"200 Request is valid\",\"job\":{\"Id\":1,\"Jobtitle\":\"Test Job\",\"Description\":\"Asthma\",\"Medicalcondition\":\"Asthma\",\"Language\":\"English\",\"Racereligion\":\"Chinese\",\"Agegender\":\"50, Female\",\"Hourlyprice\":42,\"Type\":\"caregiver\",\"Date\":\"2019-05-12T00:00:00+08:00\",\"Starttime\":\"2018-07-20T12:15:00+08:00\",\"Endtime\":\"2018-07-20T18:15:00+08:00\",\"Address\":\"1 Cluny Road\",\"Latitude\":1.3152057,\"Longitude\":103.8162553,\"Creator\":\"xiurobert\"}}";
Constructor
JSONObject jsonObject = new JSONObject(json)
However, this resulted in a java.lang.RuntimeException: Cannot evaluate org.json.JSONObject.toString()
I've even tried using gson to parse the string first and then output it back to the new JSONObject but it seems like it didn't work.
Any reasons why?
This is working perfectly
String json = "{\"status\":200,\"message\":\"200 Request is valid\",\"job\":{\"Id\":1,\"Jobtitle\":\"Test Job\",\"Description\":\"Asthma\",\"Medicalcondition\":\"Asthma\",\"Language\":\"English\",\"Racereligion\":\"Chinese\",\"Agegender\":\"50, Female\",\"Hourlyprice\":42,\"Type\":\"caregiver\",\"Date\":\"2019-05-12T00:00:00+08:00\",\"Starttime\":\"2018-07-20T12:15:00+08:00\",\"Endtime\":\"2018-07-20T18:15:00+08:00\",\"Address\":\"1 Cluny Road\",\"Latitude\":1.3152057,\"Longitude\":103.8162553,\"Creator\":\"xiurobert\"}}";
try
{
JSONObject jsonObject = new JSONObject(json);
Log.i("TAG", "a: "+jsonObject.toString());
} catch (JSONException e)
{
e.printStackTrace();
}
where are you getting error
In my case i have used org.json.JSONObject and org.json.JSONArray. Then i changed the imports to org.json.simple.JSONObject and org.json.simple.JSONArray and my code worked without any issues.
I am using Rest Assured for API testing, how do I send array objects in a POST? For a plain string I know I can do something like this
JSONObject json = new JSONObject();
json.put("firstname", "John"));
json.put("lastname", "James");
request.body(json.toJSONString());
request.post("/my/post/url/end/point");
How do I send an object like this using the JSONObject and Rest Assured?
{
"price": "234",
"phoneNumber": "09022334422",
"owner": [{
"digits": "1122334455",
"myname": "Abisoye Haminat",
"code": "058",
"default": "true"
}]
}
To send an array, you can use JSONArray:
JSONObject jsonObjectToPost = new JSONObject();
JSONArray array = new JSONArray();
JSONObject arrayItem = new JSONObject();
arrayItem.put("code","058");
arrayItem.put("default", "true");
array.put(arrayItem.toString());
jsonObjectToPost.put("owner", array.toString())
I'm new to JSON and have below file. I have to save the array "steps" in java and need to loop the objects "duration" , "status" and "Keyword".
"steps": [
{
"result": {
"duration": 7128811788,
"status": "passed"
},
"line": 5,
"name": "The Browser is Launched and Smart Business URL is loaded",
"match": {
"location": "Common_Login.the_Browser_is_Launched_and_Smart_Business_URL_is_loaded()"
},
"keyword": "Given "
},]
I tried below but didn't worked.
JSONParser parser = new JSONParser();
Object obj = parser.parse(new FileReader("./target/JSON/Output.json"));
JSONArray jsonArray = (JSONArray) obj;
System.out.println(jsonArray);
for (int i = 0; i < jsonArray.size(); i++) {
JSONObject jsonObjectRow = (JSONObject) jsonArray.get(i);
name = (String) jsonObjectRow.get("duration");
id = (String) jsonObjectRow.get("status");
uri = (String) jsonObjectRow.get("name");
status = (String) jsonObjectRow.get("location");
}
Refer here.
There are so many libraries build in for doing this task. But look at the question above.
If you want to use the built-in class from Java you can have a look here:
https://stackoverflow.com/a/17399110/3977134
You are missing the part from the parseJson function in the referenced answer.
For simplicity I'd suggest to you to use org.json which is just one of many good libraries to use for JSON parsing in Java. If you are interested see here:
Parse JSON with org.json
Parsing JSON in Java Using org.json
In my android project, I have the string text which got the following data:
[
{
"admin": true,
"created_at": "2012-10-16T07:26:49Z",
"email": "asdf#gmail.com",
"id": 28,
"language": "fr",
"name": "Marc",
"profile_pic_content_type": null,
"profile_pic_file_name": null,
"profile_pic_file_size": null,
"profile_pic_updated_at": null,
"provider": null
},
{
"admin": false,
"created_at": "2013-04-02T18:47:36Z",
"email": "asdf2#gmail.com",
"id": 263,
"language": "en",
"name": "Marcus",
"profile_pic_content_type": null,
"profile_pic_file_name": null,
"profile_pic_file_size": null,
"profile_pic_updated_at": null,
"provider": null
}
]
I converted it into a json object thanks to this:
JSONObject jsonObj = new JSONObject(text);
I want to parse that Json object, and setting it inside a ListView, but even with the official documentation I can't succeed in doing so.
After parsing, I want to keep only the first part of the array, and delete every field excepting the email, language and name, to get this in the end:
[
{
"email": "asdf#gmail.com",
"language": "fr",
"name": "Marc"
}
]
You're dealing with a JSONArray - the [ ] - that contains two separate JSONObject. The way you extract values from this structure is simply to go piece by piece, first getting the nested objects from the array and then extracting their internal values. You can then repackage it as you wish. For example:
int numObject = jsonArray.length();
JSONArray repackArray = new JSONArray();
for(int i = 0; i < numObject; i++){
JSONObject nested = jsonArray.getJsonObject(i);
//get values you need
String email = nested.getString("email");
String language = nested.getString("language");
String name = nested.getString("name");
//add values to new object
JSONObject repack = new JSONObject();
repack.put("email", email);
repack.put("language", language);
repack.name("name", name);
//add to new array
repackArray.put(repack);
}
Alternatively if put doesn't work for you, you can always create your own String in JSON format and then simply create a new JSONObject using that string as an argument in the constructor. I assumed you were working with a JSONArray in the above example. If you're starting with a JSONObject the process is the same. Just get the JSONArray out of the object first before unpacking.
I am new to JSON. I'm working with the json processing library. Given a JSON such as:
{
"data": [
{
"name": "John Doe",
"id": "2980311"
},
{
"name": "Jane Doe",
"id": "10221412"
},
{
"name": "George Doe",
"id": "111623489"
}
],
"paging": {
"blah" : "blah"
}
}
How can I get the id values. I've started by trying to get an array to work with:
String URL = "https://graph.facebook.com/username/friends?access_token=";
String token = "";
String[] response = loadStrings(URL+token);
if(response != null) {
JSONObject result = new JSONObject(response);
JSONArray data = result.getJSONArray("data");
}
And this prints out:
JSONObject["data"] not found.
JSONObject["data"] is not a JSONArray.
But it's definitely getting the data. If I print out response I see the data.
What am I missing?
I'm not seeing a JSONObject constructor in that library that accepts String[] (the type of your response variable). There's JSONObject(String) and there's JSONObject(Object). If you pass a String[] into JSONObject, it'll match the latter, which doesn't (immediately) look to me like it'll do what you want.
I suspect you want to get a single String from the URL, which you'd then pass into JSONObject(String), at which point things should start working correctly.
try with this..
GraphObject responseGraphObject = response.getGraphObject();
JSONObject json = responseGraphObject.getInnerJSONObject();
JSONObject sys = json.getJSONObject("data");
Log.e("urlimage json", sys.getString("url"));