Dialogflow easy way for authorization

后端 未结 5 926
孤独总比滥情好
孤独总比滥情好 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)
    

提交回复
热议问题