0

Does anyone know how to extract the parameters returned by the Google dialogflow v2 response? I am able to get the intents and confidence properly as there are high level methods available to do so. But it seems there is no method to get the parameters/list of params. The response contains a google protobuf Struct that has the params. Does anyone know how to extract the parameter names and values from it.

Here is a sample response

 query_text: "next friday"
parameters {
  fields {
    key: "appointmentDate"
    value {
      struct_value {
        fields {
          key: "date"
          value {
            string_value: "2019-05-31T12:00:00+10:00"
          }
        }
      }
    }
  }
}
all_required_params_present: true
fulfillment_messages {
  text {
    text: ""
  }
}
intent {
  name: "projects/dksjdkjsjksd-c824f/agent/intents/89a100c4973a"
  display_name: "captureDate"
}
intent_detection_confidence: 1.0
language_code: "en"
Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
  • you need to use [protobuf](https://github.com/protocolbuffers/protobuf/tree/master/java) to convert this into json – sid8491 May 22 '19 at 05:57
  • I guess I'm not following ... this response loos like an ordinary JSON piece of text. Where are we seeing any encoded data? – Kolban May 22 '19 at 14:24
  • @Kolban No it is not a json, it is a proto buffer which looks like json but is a more efficient way to represent data. – Juned Ahsan Jun 30 '19 at 00:36

2 Answers2

0

It would be something lilke this:

            for (Entry<String, Value> entry : queryResult.getParameters().getFieldsMap().entrySet()) {
                if (entry.getValue().getKindCase().getNumber() == Value.STRING_VALUE_FIELD_NUMBER) {

                    log.debug("FOUND PARAM. KEY:" + entry.getKey() + " STRING VALUE: "
                            + entry.getValue().getStringValue());

                } else if (entry.getValue().getKindCase().getNumber() == Value.STRUCT_VALUE_FIELD_NUMBER) {

                    log.debug("FOUND PARAM. KEY:" + entry.getKey() + " STRUCT VALUE: "
                            + entry.getValue().getStructValue());

                }

                else if (entry.getValue().getKindCase().getNumber() == Value.NUMBER_VALUE_FIELD_NUMBER) {

                    log.debug("FOUND PARAM. KEY:" + entry.getKey() + " NUMBER VALUE: "
                            + String.valueOf(entry.getValue().getNumberValue()));

                }

            }
Eduardo Hermida
  • 161
  • 1
  • 7
0

I was too focussed to parse and map the proto buffer to a Java bean. After spending hours and posting a question, a simple thought striked to my mind to find a way to convert the proto buffer to a json. And then it was all simple because I found this API

JsonFormat.printToString(protoMessage)

It sounds simple now but that is all because I changed my problem solving strategy from learning proto buffer and decoding it, to rather use a proto to json convertor and work with json format, which understand much better.

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136