7

I would like to know the best practice to parse the response of detectIntent of dialogflow v2.

The response of dialogflow v2 includes struct object which is defined in Protocol Buffer. For example, parameters in queryResult.

I know it is possible to parse it by using structToJson which is included in dialogflow v2 SDK for Node.js as sample code. So the my current code is looks like this.

const dialogflow = require("dialogflow");
const structjson = require("./dialogflow/structjson");

identify_intent(sentence, options){
    const session_path = this._sessions_client.sessionPath(this._project_id, options.session_id);

    // The text query request.
    const request = {
        session: session_path,
        queryInput: {
            text: {
                text: sentence,
                languageCode: this._language
            }
        }
    };

    // Send request and log result
    return this._sessions_client.detectIntent(request).then(responses => {
        let result = responses[0].queryResult;

        if (result.parameters){
            result.parameters = structjson.structProtoToJson(result.parameters);
        }

        return result;
    });
}

I'm parsing the response manually by using structProtoToJson() following the sample code but it is not practical since I have to do it not only for parameters but also fo fulfillment and other object as well which is formatted in struct.

I'm wondering what is the best practice to parse the response from detectIntent in Node.js app.

Kazuki
  • 71
  • 2
  • have solved the problem? I am facing the same issue. the result json is very hard to parse if you are using custom payload in response. – Qadir Hussain Sep 06 '18 at 07:21

1 Answers1

0

You don't need to do anything to convert the queryResult.parameters object into a usable form. It is a JavaScript object with the following structure:

{
  fields: {
    paramName1: { stringValue: 'value1', kind: 'stringValue' },
    paramName2: { stringValue: 'value2', kind: 'stringValue' }
  }
}

It will have one key/value pair for each parameter in the intent's parameter list.

Daniel Situnayake
  • 2,874
  • 2
  • 30
  • 38
  • I thought this format was just to keep the type of the fields regardless of language. So when we access to the data, I think we should convert it into following structure and structProtoToJson actually does it. { paramName1: "value1", paramName2: "value2" } – Kazuki Jun 22 '18 at 04:15
  • `{ paramName1: "value1", paramName2: "value2" }` – Kazuki Jun 22 '18 at 04:21
  • You are welcome to convert the object into any structure you like, but there is no obligation to do so :) – Daniel Situnayake Jun 22 '18 at 16:22