How To Get Parameters From Dialogflow In Java (android Studio)
I need to get parameters from DialogFlow to my Android app. I tried using getQueryResult().getParameters().getFieldsMap() but the result is the following. {type=list_value {
Solution 1:
I see two possibilities:
- Try to access the Map values directly.
The getFieldsMap()
method returns a java.util.Map class. You can try to retrieve the values by getting first a collection of Values, then iterate:
Collectioncolletion= <Detect_Intent_Object>.getQueryResult().getParameters().getFieldsMap().values():
for (iterable_type iterable_element : collection)
From my humble point of view the bucle is necesary because there could be more than one parameter.
- Transform the protobuf response into a json and access the values.
Sample code:
import com.google.protobuf.util.JsonFormat;
String jsonString = JsonFormat.printToString(<Detect_Intent_Object>.getQueryResult().getParameters());
// Then use a json parser to obtain the valuesimport org.json.*;
JSONObject obj = newJSONObject(jsonString);
JSONArray jsonnames = obj.names();
Method names()
will let you know the string names you want to access.
Solution 2:
If you use Dialogflowv2
publicStringgetParameter(GoogleCloudDialogflowV2WebhookRequest request, String parameterName) {
try {
GoogleCloudDialogflowV2QueryResult queryResult = request.getQueryResult();
Map<String, Object> parameters = queryResult.getParameters();
String parameter = (String) parameters.get(parameterName);
if(parameter != null && !parameter.equals("")) {
return parameter;
}
} catch (ClassCastException e) {
logger.error("Error");
}
returnnull;
}
If you use GoogleActions
publicStringgetParameter(ActionRequest request, String parameterName) {
try {
Map<String, Object> parameters = request.getWebhookRequest().getQueryResult().getParameters();
String parameter = (String) parameters.get(parameterName);
if(parameter != null && !parameter.equals("")) {
return parameter;
}
} catch (ClassCastException e) {
logger.error("Error");
}
returnnull;
}
Post a Comment for "How To Get Parameters From Dialogflow In Java (android Studio)"