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