Dialogflow pass parameters through NodeJS

淺唱寂寞╮ 提交于 2019-12-06 02:20:48

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


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!