How to make http call on DialogFlow v2 using Javascript ajax call

后端 未结 6 419
时光说笑
时光说笑 2020-12-19 11:17

I found this example on the official site of DialogFlow using Node.js and it is working fine, but I dont know how do I integrate this into my web application.

Is it

6条回答
  •  执笔经年
    2020-12-19 11:41

    This way it works for me.

    const dialogflow = require('dialogflow');
    var projectId = ""; // your dialogflow project id when you click on settings of the DF agent
    var sessionId = ""; // session ID
    var config = {
    // use credentials or keyFilename i'm using keyFile
        // credentials: {
            // private_key: privateKey,
            // client_email: clientEmail,
        // }
            keyFilename: './google.json'
    };
    
    var sessionClient = new dialogflow.SessionsClient(config);
    
    async sendTextMessageToDialogFlow(textMessage) {
            // Define session path
            const sessionPath = sessionClient.sessionPath(projectId, sessionId);
            // The text query request.
            const request = {
                session: sessionPath,
                queryInput: {
                    text: {
                        text: textMessage,
                        languageCode: 'en'
                    }
                }
            }
            try {
                let [responses] = await sessionClient.detectIntent(request)
                console.log(responses);
            } catch (err) {
                console.error('DialogFlow.sendTextMessageToDialogFlow ERROR:', err);
                throw err
            }
        };
    

提交回复
热议问题