简体   繁体   English

将 Node js 与对话框流集成的问题

[英]Issue integrating Node js with dialogflow

I have created a Google Dialogflow agent, now I am trying to integrate it with a node js app so that I can create a custom UI for the chatbot.我已经创建了一个 Google Dialogflow 代理,现在我正在尝试将它与节点 js 应用程序集成,以便我可以为聊天机器人创建自定义 UI。

I have followed the instructions in the document... Enabled API for the project, generated the JSON key.我已按照文档中的说明进行操作...为项目启用 API,生成 JSON 密钥。

Below is my code:下面是我的代码:

const dialogflow = require('dialogflow');
const uuid = require('uuid');

/**
 * Send a query to the dialogflow agent, and return the query result.
 * @param {string} projectId The project to be used
 */
async function runSample(projectId = 'br-poaqqc') {
  // A unique identifier for the given session
  const sessionId = uuid.v4();

  // Create a new session
  const sessionClient = new dialogflow.SessionsClient({
    keyFilename: "./br-poaqqc-51d2d712d74f.json"
  });
  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: 'hi',
        // The language used by the client (en-US)
        languageCode: 'en-US',
      },
    },
  };
  // Send request and log result
  try {
    const responses = await sessionClient.detectIntent(request);
  } catch(err) {
      console.log('Error getting response',err)
  }
  console.log(responses);
  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.`);
  }
}

runSample();

There seems to be some certificate error.似乎有一些证书错误。 Below is the log:以下是日志:

Error getting response Error: SELF_SIGNED_CERT_IN_CHAIN undefined: Getting metadata from plugin failed with error: request to https://www.googleapis.com/oauth2/v4/token failed, reason: self signed certificate in certificate chain
    at Object.callErrorFromStatus (D:\PV\Codebase\chatbot-ui\node_modules\@grpc\grpc-js\build\src\call.js:30:26)
    at Object.onReceiveStatus (D:\PV\Codebase\chatbot-ui\node_modules\@grpc\grpc-js\build\src\client.js:175:52)
    at Object.onReceiveStatus (D:\PV\Codebase\chatbot-ui\node_modules\@grpc\grpc-js\build\src\client-interceptors.js:341:141)
    at Object.onReceiveStatus (D:\PV\Codebase\chatbot-ui\node_modules\@grpc\grpc-js\build\src\client-interceptors.js:304:181)
    at Http2CallStream.outputStatus (D:\PV\Codebase\chatbot-ui\node_modules\@grpc\grpc-js\build\src\call-stream.js:116:74)
    at Http2CallStream.maybeOutputStatus (D:\PV\Codebase\chatbot-ui\node_modules\@grpc\grpc-js\build\src\call-stream.js:155:22)
    at Http2CallStream.endCall (D:\PV\Codebase\chatbot-ui\node_modules\@grpc\grpc-js\build\src\call-stream.js:141:18)
    at Http2CallStream.cancelWithStatus (D:\PV\Codebase\chatbot-ui\node_modules\@grpc\grpc-js\build\src\call-stream.js:457:14)
    at D:\PV\Codebase\chatbot-ui\node_modules\@grpc\grpc-js\build\src\channel.js:225:36
    at processTicksAndRejections (internal/process/task_queues.js:85:5) {
  code: 'SELF_SIGNED_CERT_IN_CHAIN',
  details: 'Getting metadata from plugin failed with error: request to https://www.googleapis.com/oauth2/v4/token failed, reason: self signed certificate in certificate chain',
  metadata: Metadata { internalRepr: Map {}, options: {} }
}
(node:1632) UnhandledPromiseRejectionWarning: ReferenceError: responses is not defined
    at runSample (D:\PV\Codebase\chatbot-ui\app.js:35:15)
(node:1632) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:1632) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

The issue is lies around the sessionClient sending the request object to the dialogflow API.问题在于 sessionClient 将请求 object 发送到对话流 API。 Beating my head over it for the past couple of hours.在过去的几个小时里,我一直在努力。 :/ Any clue where the issue is? :/任何线索问题出在哪里? Thanks in advance: :)提前致谢: :)

const dialogflow = require('dialogflow');
const uuid = require('uuid');
const config = require('./config'); // your JSON file

if(!config.private_key) throw new Error('Private key required')
if(!config.client_email)throw new Error('Client email required')
if(!config.project_id) throw new Error('project required')

const credentials = {
    client_email: config.client_email,
    private_key: config.private_key,
};

const sessionClient = new dialogflow.SessionsClient(
    {
        projectId: config.project_id,
        credentials
    }
);

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

    const sessionPath = sessionClient.sessionPath(config.project_id, sessionId);
    // The text query request.
    const request = {
        session: sessionPath,
        queryInput: {
            text: {
                // The query to send to the dialogflow agent
                text: 'hi',
                // The language used by the client (en-US)
                languageCode: 'en-US',
            },
        },
    };
    // Send request and log result
    try {
        const responses = await sessionClient.detectIntent(request);
        console.log(responses);
        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.`);
        }
    } catch (err) {
        console.log('Error getting response', err)
    }

}

    
 runSample();

Don't seems any issue but i think issue was with try block.似乎没有任何问题,但我认为问题出在 try 块上。 Make sure you are with internet connection if trying from local and your firewall is not blocking googleapis如果从本地尝试并且您的防火墙没有阻止googleapis ,请确保您使用互联网连接

the issue was not to do with the code, the firewall was blocking the 'googleapis.com' domain.问题与代码无关,防火墙阻止了“googleapis.com”域。 I deployed the app in Heroku server and it worked fine.我在 Heroku 服务器中部署了该应用程序,它运行良好。 :) :)

For anyone who ponders around the same issue and ends up here, make sure its not a firewall issue.对于考虑相同问题并最终出现在这里的任何人,请确保它不是防火墙问题。 Cheers!干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM