Dialogflow easy way for authorization

后端 未结 5 896
孤独总比滥情好
孤独总比滥情好 2020-12-02 00:05

Does exist an easy way to connect Dialogflow agent to node.js code? When I use this code with the correct projectID taken from the Dialogflow agent\'s settings

相关标签:
5条回答
  • 2020-12-02 00:38

    I have the same issue few months ago, check this, this is how i solve it. From your JSON that Google Cloud extract this lines.

    const dialogflow = require('dialogflow');
    const LANGUAGE_CODE = 'en-US'
    const projectId = 'projectid';
    const sessionId = 'sessionId';
    const query = 'text to check';
    
    
    let privateKey = "private key JSON";
    let clientEmail = "email acount from JSON";
    let config = {
    credentials: {
        private_key: privateKey,
        client_email: clientEmail
    }
    };
    
    sessionClient = new dialogflow.SessionsClient(config);
    
    async function sendTextMessageToDialogFlow(textMessage, sessionId) {
    // Define session path
    const sessionPath = this.sessionClient.sessionPath(projectId, sessionId);
    // The text query request.
    const request = {
        session: sessionPath,
        queryInput: {
            text: {
                text: textMessage,
                languageCode: LANGUAGE_CODE
            }
        }
    }
    try {
        let responses = await this.sessionClient.detectIntent(request)
        console.log('DialogFlow.sendTextMessageToDialogFlow: Detected intent', responses);
        return responses
    } catch (err) {
        console.error('DialogFlow.sendTextMessageToDialogFlow ERROR:', err);
        throw err
    }
    };
    
    
    sendTextMessageToDialogFlow(query, sessionId)
    
    0 讨论(0)
  • 2020-12-02 00:38

    Since the original question, the documentation for Dialogflow authentication has been improved. You should find all your answers here:

    Authentication and access contro

    0 讨论(0)
  • 2020-12-02 00:41

    It is not very well documented, but the easiest way to authenticate is using the JSON file provided on your google cloud platform console.

    const sessionClient = new dialogflow.SessionsClient({
        keyFilename: '/path/to/google.json'
    });
    const sessionPath = sessionClient.sessionPath(projectId, sessionId);
    

    This also works for all the other clients. ContextsClients, EntityTypesClient and so on.

    0 讨论(0)
  • 2020-12-02 00:46

    I follow the above solutions with little changes :

    // A unique identifier for the given session
    const sessionId = uuid.v4();
    
    // Create a new session
    
    const sessionClient = new dialogflow.SessionsClient({
        keyFilename: require("path").join('config/google-credential.json')
    });
    const sessionPath = sessionClient.sessionPath(process.env.DIALOGFLOW_PROJECTID, sessionId);
    
    0 讨论(0)
  • 2020-12-02 00:57

    I am writing the code, which worked for me. Please follow all the steps provided in Reference link 2 and for coding purpose you can use the snippet provided.

    I have also added the sample JSON of Google Cloud Oauth

    References:

    1. https://www.npmjs.com/package/dialogflow#samples
    2. https://medium.com/@tzahi/how-to-setup-dialogflow-v2-authentication-programmatically-with-node-js-b37fa4815d89

    //Downloaded JSON format
    
    {
      "type": "service_account",
      "project_id": "mybot",
      "private_key_id": "123456asd",
      "private_key": "YOURKEY",
      "client_email": "yourID@mybot.iam.gserviceaccount.com",
      "client_id": "098091234",
      "auth_uri": "https://accounts.google.com/o/oauth2/auth",
      "token_uri": "https://oauth2.googleapis.com/token",
      "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
      "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/yourID%40mybot.iam.gserviceaccount.com"
    }
    
    
    //------*********************---------------------------
    //
    const projectId = 'mybot';
    
    //https://dialogflow.com/docs/agents#settings
    // generate session id (currently hard coded)
    const sessionId = '981dbc33-7c54-5419-2cce-edf90efd2170';
    const query = 'hello';
    const languageCode = 'en-US';
    
    // Instantiate a DialogFlow client.
    const dialogflow = require('dialogflow');
    let privateKey = 'YourKey';
    
    // as per goolgle json
    let clientEmail = "yourID@mybot.iam.gserviceaccount.com";
    let config = {
      credentials: {
        private_key: privateKey,
        client_email: clientEmail
      }
    }
    const sessionClient = new dialogflow.SessionsClient(config);
    
    // 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);
      });

    0 讨论(0)
提交回复
热议问题