-
Notifications
You must be signed in to change notification settings - Fork 2
/
handleIntent.js
53 lines (47 loc) · 1.41 KB
/
handleIntent.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
require('dotenv').config()
const projectId = process.env.GCLOUD_PROJECT; //https://dialogflow.com/docs/agents#settings
const sessionId = 'new-session'; // should create new session for each new user
const languageCode = 'en';
// Instantiate a DialogFlow client.
const dialogflow = require('dialogflow');
const sessionClient = new dialogflow.SessionsClient();
// Define session path
const sessionPath = sessionClient.sessionPath(projectId, sessionId);
// Send request and log result
module.exports = (query, callback) => {
// The text query request.
const request = {
session: sessionPath,
queryInput: {
text: {
text: query,
languageCode: languageCode,
},
},
};
return sessionClient
.detectIntent(request)
.then(responses => {
console.log('Detected intent');
console.log('responses', responses)
const result = responses[0].queryResult;
console.log(` Query: ${result.queryText}`);
console.log(` Response: ${result.fulfillmentText}`);
if (result.intent) {
console.log(` Intent: ${result.intent.displayName}`);
return callback(null, result)
}
else if(result.action) {
console.log(` Action matched without intent: ${result.action}`)
return callback(null, result)
}
else {
console.log(` No intent matched.`);
return callback(new Error('No intent matched'))
}
})
.catch(err => {
console.error('ERROR:', err);
return callback(err)
});
}