-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
dialogflow.js
69 lines (64 loc) · 1.76 KB
/
dialogflow.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
const dialogflow = require("dialogflow");
const config = require("./config");
const credentials = {
client_email: config.GOOGLE_CLIENT_EMAIL,
private_key: config.GOOGLE_PRIVATE_KEY,
};
const sessionClient = new dialogflow.SessionsClient({
projectId: config.GOOGLE_PROJECT_ID,
credentials,
});
/**
* Send a query to the dialogflow agent, and return the query result.
* @param {string} projectId The project to be used
*/
async function sendToDialogFlow(msg, session, source, params) {
let textToDialogFlow = msg;
try {
const sessionPath = sessionClient.sessionPath(
config.GOOGLE_PROJECT_ID,
session
);
const request = {
session: sessionPath,
queryInput: {
text: {
text: textToDialogFlow,
languageCode: config.DF_LANGUAGE_CODE,
},
},
queryParams: {
payload: {
data: params,
},
},
};
const responses = await sessionClient.detectIntent(request);
const result = responses[0].queryResult;
console.log("INTENT EMPAREJADO: ", result.intent.displayName);
let defaultResponses = [];
if (result.action !== "input.unknown") {
result.fulfillmentMessages.forEach((element) => {
if (element.platform === source) {
defaultResponses.push(element);
}
});
}
if (defaultResponses.length === 0) {
result.fulfillmentMessages.forEach((element) => {
if (element.platform === "PLATFORM_UNSPECIFIED") {
defaultResponses.push(element);
}
});
}
result.fulfillmentMessages = defaultResponses;
return result;
// console.log("se enviara el resultado: ", result);
} catch (e) {
console.log("error");
console.log(e);
}
}
module.exports = {
sendToDialogFlow,
};