I have a diagflow agent that it does return a custom Payload (buttons which transition to another flow).Also I have a standalone client (Java client) that queries the Diagflow agent and return response back to the End User.
Users will interact with the Diagflow agent through Java client developed using google Java SDK. I need help in how can I invoke (simulate) user clicking the action button that is retuned by diagflow agent in my Java Client?
Type: payload
payload {
fields {
key: "richContent"
value {
list_value {
values {
list_value {
values {
struct_value {
fields {
key: "event"
value {
struct_value {
fields {
key: "name"
value {
string_value: "EN_TXT"
}
}
}
}
}
fields {
key: "icon"
value {
struct_value {
fields {
key: "color"
value {
string_value: "#FF9800"
}
}
fields {
key: "type"
value {
string_value: "chevron_right"
}
}
}
}
}
fields {
key: "link"
value {
string_value: ""
}
}
fields {
key: "text"
value {
string_value: "English"
}
}
fields {
key: "type"
value {
string_value: "button"
}
}
}
}
values {
struct_value {
fields {
key: "event"
value {
struct_value {
fields {
key: "name"
value {
string_value: "\331\220AR_TXT"
}
}
}
}
}
fields {
key: "icon"
value {
struct_value {
fields {
key: "color"
value {
string_value: "#FF9800"
}
}
fields {
key: "type"
value {
string_value: "chevron_right"
}
}
}
}
}
fields {
key: "link"
value {
string_value: ""
}
}
fields {
key: "text"
value {
string_value: "\330\271\330\261\330\250\331\212"
}
}
fields {
key: "type"
value {
string_value: "button"
}
}
}
}
}
}
}
}
}
}
I have researched the web but couldn't find a solution.
Related
I'm having a model which contains a property of type List<List<String>>, I tried to pass a model from the postman it receives null instead of the actual data.
Technology: JAVA SE 11 - Spring Book Maven Application - AWS Lambda
Sample Model
public class KeyParamInfo {
private String key;
private List<List<String>> referenceData;
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public List<List<String>> getReferenceData() {
return referenceData;
}
public void setReferenceData(List<List<String>> referenceData) {
this.referenceData = referenceData;
}
}
The respective Body Data in PostMan is
{
"key":"PROG",
"referenceData":[
[
"JAVA",
"KOTLIN"
],
[
"HTML",
"JS"
]
]
}
Java Code:
public class SampleApplication implements RequestHandler<KeyParamInfo, List<String>> {
public List<String> handleRequest(KeyParamInfo input, Context context) {
List<String> response = new ArrayList<String>();
try {
if(input != null) {
if(input.getReferenceData() != null) {
response.add("Reference Data Received");
}
else if(input.getKey() != null && !input.getKey().equals("")) {
response.add("Data Received with key " + input..getKey());
}
else {
response.add("Empty Data Object");
}
} else {
response.add("Invalid Data");
}
} catch (Exception e) {
response.add("Exception occured in Main method - " + e.getMessage());
}
return response;
}
}
Following is the response returns from the above code ["Data Received with key PROG"]
AWS Lambda Documentation:
https://docs.aws.amazon.com/lambda/index.html
In the application the referenceData receives the value null instead of the above nested list. Please assist how to pass the requested nested list into JAVA.
I am working on getting map type data stored in DynamoDB, the way I have tried so far:
use String getData() in my POJO, however, there is an issue that DynamoDBMappingException: could not unconvert attribute.
use Map<String, Object> getData() and annotated with #DynamoDBTypeConverted(converter = MapConverter.class) to retrieve the data, and also wrote a converter for it. This method also throws Exception DynamoDBMappingException: could not unconvert attribute, expected S in value.
So my question is:
is there a way to retrieve the standard JSON format data from DynamoDB by using mapper instead of DynamoJson without “N”, “S”, etc?
For instance:
"data": {
"cId":"777",
"cName":"NPR"
"isOwner":true
}
Once I get the standard JSON, I can use objectmapper to convert it to arbitrary data based on different use cases.
2. is it possible to use a map here? I found some folks create POJO for nested data, however, in my use case, the data fields vary from the different scenarios.
The converter I wrote:
public class MapConverter implements DynamoDBTypeConverter<String, Map> {
private final ObjectMapper objectMapper;
public MapConverter() {
objectMapper = new ObjectMapper();
objectMapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
}
#Override
public String convert(final Map map) {
if (map == null) {
return null;
}
try {
return objectMapper.writeValueAsString(map);
} catch (JsonProcessingException e) {
log.error("Hit exception while trying to convert map. {}.", map, e);
return null;
}
}
#Override
public Map unconvert(String serializedMap) {
if (serializedMap == null) {
return null;
}
try {
return objectMapper.readValue(serializedMap, Map.class);
} catch (IOException e) {
log.error("Hit exception while trying to unconvert map. {}.", serializedMap, e);
return null;
}
}
}
The data in the DynamoDB is like this:
{
"data": {
"M": {
"cId": {
"S": "777"
},
"cName": {
"S": "NPR"
},
"isOwner": {
"BOOL": true
}
}
}
}
and it may also look like
"data": {
"M": {
"globalEntityId": {
"S": "234"
},
"providerMetadata": {
"L": [
{
"M": {
"proj.123": {
"M": {
"entityId": {
"S": "s:123"
}
}
},
"PA": {
"M": {
"entityId": {
"S": "p:123"
}
}
}
}
}
]
}
}
}
I have a ArrayNode like this names "results":
[{
"subjectName": "Eclipse",
"code": "EC1",
"states": [
{
"subjectName": "LunorEx1",
"code":"E1"
},
{
"subjectName": "LunorEx2",
"code":"E2"
},
{
"subjectName": "Expus LunorEx3 ",
"code":"E6"
}]
},
{
"subjectName": "Lasena",
"code": "LS1",
"states": [
{
"subjectName": "SunorEx1",
"code":"S1"
},
{
"subjectName": "SunorEx2",
"code":"S2"
}]
}, {
"subjectName": "Gilesh",
"code": "GL2",
"states": [ ]
}]
this variable is public.
Using Java 8, I want to be able to check if exists for example a subjectName equals to "Eclipse" and code equals to "EC1", if this exists then I want to search inside its json states and find a state that has, for example, a subjectName equals to "LunorEx1" and code "E1", if all these are found I want to return true
private static boolean subjectValidation( String parentSubjectName, String parentCode, String childSubjectName, String childCode){
boolean valid = false;
try {
JsonNode subjectData = StreamSupport.stream(results.spliterator(), true)
.filter(c -> c.get("subjectName").asText().equals(parentSubjectName) &&
c.get("code").asText().equals(parentCode) )
.collect(Collectors.toList()).get(0);
valid = StreamSupport.stream(subjectData.get("states").spliterator(), true)
.anyMatch(k -> k.get("states").get("subjectName").asText().equals(childSubjectName) &&
k.get("states").get("code").asText().equals(childCode));
} catch (Exception e) {
}
return valid;
}
I want to optimize this cause I have several JSON arrays with the same structure and I do several similar checks ... . and to handle when the fist stream return nothing.
Can someone help me, to give some advice on how can I do this better?
I thought it could look like this:
private static boolean subjectValidation(String parentSubjectName, String parentCode, String childSubjectName,
String childCode) {
boolean valid = false;
try {
Optional<JsonNode> subjectData = StreamSupport.stream(results.spliterator(), true)
.filter(c -> exists(c, "subjectName", parentSubjectName) && exists(c, "code", parentCode))
.findFirst();
if (subjectData.isPresent()) {
valid = StreamSupport.stream(subjectData.get().get("states").spliterator(), true)
.anyMatch(k -> exists(k.get("states"), "subjectName", childSubjectName)
&& exists(k.get("states"), "code", childCode));
}
} catch (Exception e) {
}
return valid;
}
private static boolean exists(JsonNode node, String nodeName, String value) {
return node.get(nodeName).asText().equals(value);
}
I wanted to take into account that subjectName and code are not unique.
private static boolean subjectValidation(String parentSubjectName, String parentCode, String childSubjectName, String childCode) {
try {
return StreamSupport.stream(results.spliterator(), true)
.filter(c -> hasSubject(c, parentSubjectName) && hasCode(c, parentCode))
.flatmap(s -> StreamSupport.stream(s.get("states").spliterator(), true)
.map(k -> k.get("states"))
.anyMatch(k -> hasSubject(k, childSubjectName) && hasCode(k, childCode));
} catch (Exception e) {
return false;
}
}
private static boolean hasSubject(JsonNode node, String value) {
return fieldHasValue(node, "subjectName", value);
}
private static boolean hasCode(JsonNode node, String value) {
return fieldHasValue(node, "code", value);
}
private static boolean fieldHasValue(JsonNode node, String field, String value) {
return node.get(field).asText().equals(value);
}
There should probably be better exception handling but it should work.
If you want to even further generify this I would make a function which lets you make these checks on the fly simply based on setting variables for "code", "subjectname" and "states"
So I have some JSON I want to parse into an array list of objects (a). a would hold the href link and text, price, and image link.
This is the JSON:
{
"url":"http://www.ebay.com/sch/i.html?_sacat=0&LH_ItemCondition=3000&_nkw=macbook%20pro%2015%202012&rt=nc&LH_BIN=1&_trksid=p2045573.m1684",
"result":{
"extractorData":{
"url":"http://www.ebay.com/sch/i.html?_sacat=0&LH_ItemCondition=3000&_nkw=macbook%20pro%2015%202012&rt=nc&LH_BIN=1&_trksid=p2045573.m1684",
"resourceId":"73c12187d3f6d12410d3e7490cb91d9a",
"data":[
{
"group":[
{
"Img image":[
{
"src":"http://thumbs.ebaystatic.com/images/g/xMYAAOSwE7RXF7p5/s-l225.jpg",
"href":"http://www.ebay.com/itm/15-Apple-MacBook-Pro-RETINA-2-3GHz-Core-i7-8GB-256GB-Flash-MC975LL-A-Warranty-/121983722648?hash=item1c66cbe498:g:xMYAAOSwE7RXF7p5"
}
],
"Vip link":[
{
"text":"New listing 15\" Apple MacBook Pro RETINA -2.3GHz Core i7-8GB-256GB Flash-MC975LL/A+Warranty",
"href":"http://www.ebay.com/itm/15-Apple-MacBook-Pro-RETINA-2-3GHz-Core-i7-8GB-256GB-Flash-MC975LL-A-Warranty-/121983722648?hash=item1c66cbe498:g:xMYAAOSwE7RXF7p5"
}
],
"Bold price":[
{
"text":"$989.00"
},
{
"text":"$989.00"
}
],
"Lvformat value":[
{
"text":"Buy It Now"
}
]
},
{
"Img image":[
{
"src":"http://ir.ebaystatic.com/pictures/aw/pics/s_1x2.gif",
"href":"http://www.ebay.com/itm/MINT-15-Apple-MacBook-Pro-Retina-2-3ghz-i7-8gb-256gb-w-charger-Box-/262420422046?hash=item3d1979dd9e:g:Gl8AAOSw1DtXKhrP"
}
],
"Vip link":[
{
"text":"MINT 15\" Apple MacBook Pro Retina (2.3ghz i7, 8gb, 256gb) w/charger & Box",
"href":"http://www.ebay.com/itm/MINT-15-Apple-MacBook-Pro-Retina-2-3ghz-i7-8gb-256gb-w-charger-Box-/262420422046?hash=item3d1979dd9e:g:Gl8AAOSw1DtXKhrP"
}
],
"Bold price":[
{
"text":"$1,000.00"
},
{
"text":"$1,000.00"
}
],
"Lvformat value":[
{
"text":"Buy It Now"
}
]
}
]
}
]
},
}
}
I used http://pojo.sodhanalibrary.com/ to convert it into java classes. From there I have a group, ebayAPI, image_img, bold_price, vip_link, and data classes.
I'm lost on how I can iterate through the different items in the "group" to pull their price and links.
For instance, when I do this:
EbayAPI ebayAPI = gson.fromJson(json, EbayAPI.class);
Vip_link link = gson.fromJson(json, Vip_link.class);
return new String[] {
"From URL: " + ebayAPI.getUrl(),
"Vip Link: " + link.getText()
}
I get the result:
From URL: http://www.ebay.com/sch/i.html?_sacat=0&LH_ItemCondition=3000&_nkw=macbook%20pro%2015%202012&rt=nc&LH_BIN=1&_trksid=p2045573.m1684
Vip Link: null
Thanks!
EbayAPI
public class EbayAPI {
private Result result;
private String url;
public Result getResult ()
{
return result;
}
public void setResult (Result result)
{
this.result = result;
}
public String getUrl ()
{
return url;
}
public void setUrl (String url)
{
this.url = url;
}
#Override
public String toString()
{
return "ClassPojo [result = "+
//result +
", url = "+url+"]";
}
}
Vip Link
public class Vip_link
{
private String text;
private String href;
public String getText ()
{
return text;
}
public void setText (String text)
{
this.text = text;
}
public String getHref ()
{
return href;
}
public void setHref (String href)
{
this.href = href;
}
#Override
public String toString()
{
return "ClassPojo [text = "+text+", href = "+href+"]";
}
}
I am getting json response in this format.
{
"apiGroups":
{
"Affiliate":
{
"listingsAvailable":
{
"Beauty_Personal_Care":
{
"listingVersions":
{
"v1":
{
"get": "http://affiliate-feeds.snapdeal.com/feed/api/category/v1:586:821655440?expiresAt=1446085800024&signature=civtucyhsbufsjzjvqfa"
}
}
},
"Eyewear":
{
"listingVersions":
{
"v1":
{
"get": "http://affiliate-feeds.snapdeal.com/feed/api/category/v1:473:662748456716?expiresAt=1446085800024&signature=civtucyhsbufsjzjvqfa"
}
}
},
"Real_Estate":
{
"listingVersions":
{
"v1":
{
"get": "http://affiliate-feeds.snapdeal.com/feed/api/category/v1:897:673143570606?expiresAt=1446085800024&signature=civtucyhsbufsjzjvqfa"
}
}
},
"Jewellery":
{
"listingVersions":
{
"v1":
{
"get": "http://affiliate-feeds.snapdeal.com/feed/api/category/v1:6:315773046?expiresAt=1446085800024&signature=civtucyhsbufsjzjvqfa"
}
}
},
"Furniture":
{
"listingVersions":
{
"v1":
{
"get": "http://affiliate-feeds.snapdeal.com/feed/api/category/v1:580:1894930153?expiresAt=1446085800024&signature=civtucyhsbufsjzjvqfa"
}
}
},
"Tweens_Boys":
{
"listingVersions":
{
"v1":
{
"get": "http://affiliate-feeds.snapdeal.com/feed/api/category/v1:814:934253466?expiresAt=1446085800024&signature=civtucyhsbufsjzjvqfa"
}
}
},
"Automobiles":
{
"listingVersions":
{
"v1":
{
"get": "http://affiliate-feeds.snapdeal.com/feed/api/category/v1:1145:639299259208?expiresAt=1446085800024&signature=civtucyhsbufsjzjvqfa"
}
}
},
"Home_Improvement":
{
"listingVersions":
{
"v1":
{
"get": "http://affiliate-feeds.snapdeal.com/feed/api/category/v1:864:624389489778?expiresAt=1446085800024&signature=civtucyhsbufsjzjvqfa"
}
}
},
"The_Designer_Studio":
{
"listingVersions":
{
"v1":
{
"get": "http://affiliate-feeds.snapdeal.com/feed/api/category/v1:924:655684426383?expiresAt=1446085800024&signature=civtucyhsbufsjzjvqfa"
}
}
},
"Fashion_Jewellery":
{
"listingVersions":
{
"v1":
{
"get": "http://affiliate-feeds.snapdeal.com/feed/api/category/v1:1113:672114192240?expiresAt=1446085800024&signature=civtucyhsbufsjzjvqfa"
}
}
},
I need to get categories like beauty personal care, eye ware and their respective urls in get field.How can i loop through this and get the.So far i tried like this and dont no how to proceed next.Can anybody give me suggestions how to parse this json?
json = jParser.getJSONFromUrl(response);
JSONObject api = json.getJSONObject("apiGroups");
JSONObject affiliate = api.getJSONObject("Affiliate");
JSONObject list = affiliate.getJSONObject("listingsAvailable");
You can read the documentation about the JSONObject class in Android.
In this documentation, you will find the method keys that will "Returns an iterator of the String names in this object."
So you just have to call this method and use the iterator.
Iterator<String> keysIterator = jsonObject.keys();
String key;
while (keysIterator.hasNext()) {
key = keysIterator.next();
//use the key to retrieve the data form jsonObject
}
However, if you are the one generating this json, you may consider changing it a bit. The data in the listingsAvailable should probably be in an array.