【问题标题】:nodejs-dialogflow library returning TypeError: dialogflow.SessionsClient is not a constructornodejs-dialogflow 库返回 TypeError: dialogflow.SessionsClient 不是构造函数
【发布时间】:2019-03-24 17:56:40
【问题描述】:

我正在尝试制作一个脚本,该脚本接受用户的输入,通过 Dialogflow 运行它,然后将其返回给用户。我从中获取输入的平台仅支持 Node.js。我通过 glitch.com 托管机器人,但我认为这不是导致问题的原因。在向GitHub repo. 提交错误报告之前,我想在这里查看一下

var bot = 'the platform i use to accept inputs and send outputs'

bot.on("message", async message => {

  console.log(message.content); // Log chat to console for debugging/testing

  if (message.content.indexOf(config.prefix) === 0) { // Message starts with your prefix

    let msg = message.content.slice(config.prefix.length); // slice of the prefix on the message

    let args = msg.split(" "); // break the message into part by spaces

    let cmd = args[0].toLowerCase(); // set the first word as the command in lowercase just in case

    args.shift(); // delete the first word from the args


    // You can find your project ID in your Dialogflow agent settings
    const projectId = process.env.PROJECT_ID; //https://dialogflow.com/docs/agents#settings
    const sessionId = 'quickstart-session-id';
    var query = msg;
    const languageCode = 'en-US';

    // Instantiate a DialogFlow client.
    const dialogflow = require('dialogflow');
    const sessionClient = new dialogflow.SessionsClient();

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

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

    // Send request and log result
    sessionClient
      .detectIntent(request)
      .then(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.error('ERROR:', err);
      });
  }
  return;
});

这是代码的相关部分。对于那些想知道的人,process.env.PROJECT_ID 是 glitch.com 用于任何私人的东西。因为我不希望随机的人获得我的项目 ID,所以我将其隐藏在其中,并在我未明确邀请的任何人面前隐藏它。

每次我执行此操作并尝试查询机器人时,它都会返回错误Uncaught Promise Error: TypeError: dialogflow.SessionsClient is not a constructor

如果有人可以指导我了解我所缺少的内容或问题所在,那就太好了!

【问题讨论】:

    标签: javascript node.js google-cloud-platform dialogflow-es


    【解决方案1】:

    我想通了。经过多次刷新,我决定查看 npm 文档。结果有些白痴将最早的版本列为4.0.3,将最新版本列为0.7.0。我需要明确告诉它使用 0.7.0 版本才能让它工作。谢天谢地!

    【讨论】:

      【解决方案2】:

      我的工作是通过重新安装 dialogflow 包来工作的

      npm 卸载对话框流

      npm install dialogflow --save

      【讨论】:

        【解决方案3】:

        根据@google-cloud/dialogflow - npm

        重要提示 2.0.0 版在 npm 上将 dialogflow 重命名为 @google-cloud/dialogflow,同时引入了 TypeScript 类型。

        所以要更新 dialogflow 以使用最新版本,首先卸载 dialogflow,然后使用以下命令安装:

        npm uninstall dialogflow

        npm i @google-cloud/dialogflow

        另外,如果您之前在代码中使用的是旧版本 1.2.0 的 dialogflow,请根据他们的示例进行以下更改或参考上面链接中的示例(在 require 中并获取 sessionPath):

        const dialogflow = require('@google-cloud/dialogflow');
        const sessionPath = sessionClient.projectAgentSessionPath(
            projectId,
            sessionId
          );
        

        在没有任何错误的情况下,它对我来说很好。

        【讨论】:

          【解决方案4】:

          将代码放入 try 和 catch 块中。在我的情况下,通过这样做,这个错误被消除了。

          【讨论】:

            猜你喜欢
            • 2020-06-12
            • 2018-05-24
            • 2019-08-14
            • 2018-03-06
            • 1970-01-01
            • 2017-03-28
            • 2018-06-27
            相关资源
            最近更新 更多