-
Notifications
You must be signed in to change notification settings - Fork 0
/
chatbot.js
46 lines (38 loc) · 1.09 KB
/
chatbot.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
const dialogflow = require('dialogflow')
const dialogflowConfig = require('./config/config')
const projectId = dialogflowConfig.project_id
const configuration = {
credentials: {
private_key: dialogflowConfig.private_key,
client_email: dialogflowConfig.client_email,
},
}
const sessionId = '997753'
const languageCode = 'en-US'
const sessionClient = new dialogflow.SessionsClient(configuration)
const sessionPath = sessionClient.sessionPath(projectId, sessionId)
const talkToChatbot = async (message) => {
console.log('message ' + message)
const botRequest = {
session: sessionPath,
queryInput: {
text: {
text: message,
languageCode: languageCode,
},
},
}
let response = await sessionClient
.detectIntent(botRequest)
.then((responses) => {
//console.log(JSON.stringify(responses))
const requiredResponse = responses[0].queryResult
// console.log(requiredResponse)
return requiredResponse
})
.catch((error) => {
console.log('ERROR: ' + error)
})
return response
}
module.exports = talkToChatbot