0

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 {
      values {
        string_value: "pizza"
      }
    }
    , ristorante=string_value: ""
    }

I would like to get just the string value "pizza" and not the entire FieldMap. I have already seen this topic, but it didn't help me, because I don't know what protobuf is and seems a bit complicated.

Is there a simple way to get a parameter's value?

Orion
  • 1
  • 4

2 Answers2

0

I see two possibilities:

  1. 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:

Collection colletion = <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.

  1. 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 values
import org.json.*;
JSONObject obj = new JSONObject(jsonString);
JSONArray jsonnames = obj.names(); 

Method names() will let you know the string names you want to access.

rsantiago
  • 2,054
  • 8
  • 17
  • Thank you for the answer, i've already to use "printToString" method, but i get "Cannot resolve method 'printToString' in 'JsonFormat". Obviously i've imported com.google.protobuf.util.JsonFormat; Maybe these options in gradle file are useful for you to understand why: `android { compileSdkVersion 30 buildToolsVersion "29.0.2" compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 }` – Orion Nov 05 '20 at 17:48
  • I'm not quite sure if the jar/package that includes the class `com.google.protobuf.util.JsonFormat` can be a different one because as far as I can see `printToString` is documented in other [posts](https://stackoverflow.com/a/44653021/9457843). Can you try either 1) Using `JsonFormat.printer().print()` or 2) Or this another class to check if `printToString` is available: `com.googlecode.protobuf.format.JsonFormat.printToString()` – rsantiago Nov 06 '20 at 18:07
0

If you use Dialogflowv2

public String getParameter(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");
        }
    return null;
}

If you use GoogleActions

public String getParameter(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");
        }
        return null;
    }
Leonardo Machado
  • 63
  • 1
  • 3
  • 6