Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dialogflow pass parameters through NodeJS

How can i pass parameters through NodeJS request? I would like to pass, for example, the name from my code and dialogflow automatically answer with a response which contains the parameters i passed, as "Hi $name".

Mine actual request:

const request = {
  session: sessionPath,
  queryInput: {
    text: {
      text: query,
      languageCode: languageCode,
    },
  },
};

EDIT 4 [index.js], this is new right code After the answer

const projectId    = 'your-project-id';
const sessionId    = 'session-id';

const query        = 'your-query';
const languageCode = 'your-language';

const j = require("./structjson.js");   //download It from the answer 


const dialogflow    = require('dialogflow');
const sessionClient = new dialogflow.SessionsClient({keyFilename: './THIS-IS-RIGHT.json'});

// Define session path
const sessionPath = sessionClient.sessionPath(projectId, sessionId);


async function request() {
    const contextClient = new dialogflow.ContextsClient({ keyFilename: './your-path-to-file.json' });

    const contextData = {
        name: contextClient.contextPath(projectId, sessionId, 'your-context'),
        parameters: j.jsonToStructProto({ name: 'John' }),
        lifespanCount: 1
    };//An example for the name

    const context = await contextClient.createContext({
        parent: sessionPath,
        context: contextData
    });

    const request = {
        session: sessionPath,
        queryInput: {
            text: {
                text: query,
                languageCode
            }
        },
        queryParams: {
            contexts: context // You may want to add the other contexts here
        }
    };

    const result = await sessionClient.detectIntent(request);
    console.log(result);
}

request();
like image 264
Gray Avatar asked May 27 '18 14:05

Gray


1 Answers

You can pass parameters when sending an event instead of text.

You will need to convert a javascript object to proto struct. There is a package pb-util that will handle the encoding/decoding

const { struct } = require('pb-util');  

const request = {
    session: sessionPath,
    queryInput: {
        event: {
            name: eventName,
            parameters: struct.encode({ name: 'John' }),
            languageCode
        }
    }
};

After that you will need to create a parameter with the following syntax on your intent. #eventName.name

enter image description here


Another way to do it, is creating a context, using dialogflow.ContextsClient & client.createContext() and add the parameters to the context, and then send that context with the queryInput request.

async function request() {
    const contextClient = new dialogflow.ContextsClient({ keyFilename: '..' });
    const sessionClient = new dialogflow.SessionsClient({ keyFilename: '..' });

    const contextData = {
        name: contextClient.contextPath('[PROJECT]', '[SESSION]', '[YOUR-CONTEXT]'),
        parameters: struct.encode({ name: 'John' }),
        lifespanCount: 1
    };

    const context = await contextClient.createContext({ 
        parent: sessionPath, 
        context: contextData 
    });

    const request = {
        session: sessionPath,
        queryInput: {
            text: {
                text: query,
                languageCode
            }
        },
        queryParams: {
            contexts: context // You may want to add the other contexts here
        }
    };

    const result = await sessionClient.detectIntent(request);
    console.log(result);
}

And now you will need to create a parameter, which value is: #your-context.name

like image 152
Marcos Casagrande Avatar answered Sep 25 '22 04:09

Marcos Casagrande