如何运行dialogflow节点js?

时间:2019-07-12 07:14:13

标签: node.js session dialogflow

我仍然对Dialogflow nodejs上的文档感到困惑,我是否可以明确这一点? 我已阅读此文档-> https://cloud.google.com/dialogflow/docs/reference/rest/v2/projects.agent.sessions/detectIntent

如何测试我的代码并查看输入的查询结果? 我应该在此功能内通过Axios进行POST

async function runSample(projectId = 'your-project-id') {
  // A unique identifier for the given session
  const sessionId = uuid.v4();

  // Create a new session
  const sessionClient = new dialogflow.SessionsClient();
  const sessionPath = sessionClient.sessionPath(projectId, sessionId);

  // The text query request.
  const request = {
    session: sessionPath,
    queryInput: {
      text: {
        // The query to send to the dialogflow agent
        text: 'hello',
        // The language used by the client (en-US)
        languageCode: 'en-US',
      },
    },
  };

  // Send request and log result
  const responses = await sessionClient.detectIntent(request);
  console.log('Detected intent');
  const result = responses[0].queryResult;
  console.log(`  Query: ${result.queryText}`);
  console.log(`  Response: ${result.fulfillmentText}`);
  if (result.intent) {
    console.log(`  Intent: ${result.intent.displayName}`);
  } else {
    console.log(`  No intent matched.`);
  }
}

我仍然不清楚这一点,所以我们是否还需要在本地使用HTTP请求来检查这一点? 我已经遵循了Dialogflow nodejs示例,但是接下来呢?

它在Google上说我们必须发布到 POST https://dialogflow.googleapis.com/v2/ {session = projects / / agent / sessions / }:detectIntent

nd req主体位于该函数内部的request变量上, 但我仍然不清楚dialogflow nodejs上下一步是否可以运行该方法

2 个答案:

答案 0 :(得分:1)

您可以使用Express应用程序直接运行它。只需从您的快速应用程序路由中调用此函数,或者您可以在文件中调用给定的函数并使用node <filename.js>运行代码。确保使用process.env.GOOGLE_APPLICATION_CREDENTIALS = "<your_json_file_path>";

在路径名中添加google cloud凭据

答案 1 :(得分:0)

const sessionClient = new dialogflow.SessionsClient(
    // below can be used to set the variable in code itself, if below is not working you can run following in the terminal
    //export GOOGLE_APPLICATION_CREDENTIALS="/<path of the json file"  
    {
      keyFilename: "/<path of the json file"
      
    }
);