Google cloud dialogflow intent detection nodejs example not working

拜拜、爱过 提交于 2019-12-13 20:17:48

问题


I am trying to implement a very simple dialogflow agent integration with nodejs.

Here is what I did so far

  • I followed the code from Intent detection
  • I added the service account private key file .json to my server.
  • I added the environment variable GOOGLE_APPLICATION_CREDENTIALS with the path to my .json private key file.

Here is the code I am trying to run right now:

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

Then I get this error in the console when I run this file

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.' }

回答1:


i also faced a similar issue for my angular bot.

What i did was, instead of using using the google_credentials from the json file, i created an object with private_key,client_email {these values can be taken from the service account private key file .json}, and passed the object while setting up the session client.

var config = {
  credentials: {
    private_key: "YOUR_PRIVATE_KEY",
    client_email: "YOUR_CLIENT_EMAIL"
  }
}

const sessionClient = new dialogflow.SessionsClient(config);

note: do copy the full private_key string from .json. It will start as "-----BEGIN PRIVATE KEY-----\n......" .

Also, in GCP go to the project->IAM then try setting role for the service as DIALOGLOW API ADMIN. Check if this works.



来源:https://stackoverflow.com/questions/52397180/google-cloud-dialogflow-intent-detection-nodejs-example-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!