【问题标题】:Google cloud dialogflow intent detection nodejs example not working谷歌云对话流意图检测nodejs示例不起作用
【发布时间】:2019-02-23 02:47:06
【问题描述】:

我正在尝试用 nodejs 实现一个非常简单的对话流代理集成。

这是我到目前为止所做的

  • 我按照Intent detection的代码
  • 我将服务帐户私钥文件.json 添加到我的服务器。
  • 我添加了环境变量GOOGLE_APPLICATION_CREDENTIALS 以及我的.json 私钥文件的路径。

这是我现在尝试运行的代码:

require('dotenv').config()
const projectId = 'gg-chatbot-216808'; 
const sessionId = 'quickstart-session-id';
const query = 'hello';
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,
        },
    },
};

// This prints the private key path correctly.
console.log(process.env.GOOGLE_APPLICATION_CREDENTIALS);

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

然后我在运行此文件时在控制台中收到此错误

Auth error:Error: invalid_user: Robot is disabled.
ERROR: { Error: 14 UNAVAILABLE: Getting metadata from plugin failed with error: invalid_user: Robot is disabled.
    at Object.exports.createStatusError (/var/www/html/google_auth/node_modules/grpc/src/common.js:87:15)
    at Object.onReceiveStatus (/var/www/html/google_auth/node_modules/grpc/src/client_interceptors.js:1188:28)
    at InterceptingListener._callNext (/var/www/html/google_auth/node_modules/grpc/src/client_interceptors.js:564:42)
    at InterceptingListener.onReceiveStatus (/var/www/html/google_auth/node_modules/grpc/src/client_interceptors.js:614:8)
    at callback (/var/www/html/google_auth/node_modules/grpc/src/client_interceptors.js:841:24)
  code: 14,
  metadata: Metadata { _internal_repr: {} },
  details: 'Getting metadata from plugin failed with error: invalid_user: Robot is disabled.' }

【问题讨论】:

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


【解决方案1】:

如果还没有解决,解决办法是在sessionClient中提供“fileKey”。

const sessionClient = new dialogflow.SessionsClient({
fileKey:" path of your credentials.json file"
});

let filePath = process.env.GOOGLE_APPLICATION_CREDENTIALS ="Location of credentials file".

const sessionClient = new dialogflow.SessionsClient({
fileKey:filePath
});

如果没有系统环境变量设置为 GOOGLE_APPLICATION_CREDENTIALS,这甚至可以工作。

希望这有帮助。

【讨论】:

    【解决方案2】:

    我的 Angular 机器人也遇到了类似的问题。

    我所做的是,我没有使用 json 文件中的 google_credentials,而是使用 private_key,client_email 创建了一个对象 {这些值可以从服务帐户私钥文件 .json 中获取} ,并在设置会话客户端时传递对象。

    var config = {
      credentials: {
        private_key: "YOUR_PRIVATE_KEY",
        client_email: "YOUR_CLIENT_EMAIL"
      }
    }
    
    const sessionClient = new dialogflow.SessionsClient(config);
    

    注意:一定要从 .json 复制完整的 private_key 字符串。它将以"-----BEGIN PRIVATE KEY-----\n......" 开头。

    另外,在 GCP 中转到项目->IAM,然后尝试将服务的角色设置为 DIALOGLOW API ADMIN。检查这是否有效。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多