问题
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"
回答1:
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()));
}
}
回答2:
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.
来源:https://stackoverflow.com/questions/56248301/java-how-to-extract-parameters-from-google-dialogflow-v2-response