Dialogflow通过NodeJS传递参数

时间:2018-05-27 14:27:14

标签: node.js dialogflow

如何通过NodeJS请求传递参数?我想通过,例如,我的代码中的名称和对话框流程自动回答一个包含我传递的参数的响应,如“Hi $ name”。

我的实际要求:

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

编辑4 [index.js],这是新答案后的正确代码

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();

1 个答案:

答案 0 :(得分:3)

您可以在发送event而非text时传递参数。

您需要将javascript对象转换为proto struct。有一个包pb-util将处理编码/解码

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

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

之后,您需要在意图上使用以下语法创建参数。 #eventName.name

enter image description here

另一种方法是使用dialogflow.ContextsClient&创建一个上下文。 client.createContext()并将参数添加到上下文中,然后使用queryInput请求发送该上下文。

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);
}

现在您需要创建一个参数,其值为:#your-context.name