Dialogflow API: Retrieve extracted parameters from Detect intent response

by Raghavendra N   Last Updated July 12, 2019 14:26 PM - source

I'm using the Dialogflow PHP library to work with the Dialogflow API. The detect intent API request returns a complex Google\Protobuf\Struct object for the parameters. Is there an easy way to retrieve the extracted parameters returned in the Detect Intent response?

Here is the part of the code:

$response = $sessionsClient->detectIntent($session, $queryInput);
$queryResult = $response->getQueryResult();
$parameters = $queryResult->getParameters(); // ==> Returns Google\Protobuf\Struct object

These are the parameters returned as seen in the Dialogflow history tab:

"parameters": {
    "integer": "",
    "number": "",
    "currency": "{amount=400000, currency=USD}"
}

I would like retrieve the parameters in the same format(or array is fine).



Answers 3


Call serializeToJsonString() on the Struct object, or on any other protobuf object.

jdp
jdp
October 28, 2018 13:52 PM

I tried serializeToJsonString() but it didn't work. I extract the parameters like this:

...

$fields = $queryResult->getParameters()->getFields();

$parameters = [];
foreach($fields as $key => $field) {
    $parameters[$key] = self::get_field_value($field);
}

...

private static function get_field_value($field) 
{
    $kind = $field->getKind();
    if ($kind == "string_value")
        return $field->getStringValue();
    else if ($kind == "number_value")
        return $field->getNumberValue();
    else if ($kind == "bool_value")
        return $field->getBoolValue();
    else if ($kind == "null_value")
        return $field->getNullValue();
    else if ($kind == "list_value") {
        $list_values = $field->getListValue()->getValues();
        $values = [];
        foreach($list_values as $list_value)
            $values[] = self::get_field_value($list_value);

        return $values;    
    }
    else if ($kind == "struct_value")
        return $field->getStructValue();
}
pamit
pamit
November 12, 2018 03:32 AM

$queryResult = $response->getQueryResult();<br>
$jsonString = $queryResult->serializeToJsonString();
Roman
Roman
July 12, 2019 14:23 PM

Related Questions





set parameters in EventInput

Updated January 15, 2019 12:26 PM