-
Notifications
You must be signed in to change notification settings - Fork 0
/
dialog.js
100 lines (88 loc) · 2.75 KB
/
dialog.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
const venom = require('venom-bot');
const express = require('express');
const http = require('http');
const {WebhookClient} = require('dialogflow-fulfillment');
const dialogflow = require('@google-cloud/dialogflow');
const app = express();
const port = process.env.PORT || 8000; //DEFINIR PORTAS
const server = http.createServer(app);
venom
.create({
headless: true,
session: 'dialogsession'})
.then((client) => start(client))
.catch((erro) => {
console.log(erro);
});
//webhook dialogflow
app.post('/webhook', function(request,response){
const agent = new WebhookClient ({ request, response});
let intentMap = new Map();
intentMap.set('nomedaintencao', nomedafuncao)
agent.handleRequest(intentMap);
});
function nomedafuncao (agent) {
}
const sessionClient = new dialogflow.SessionsClient({keyFilename: "//SEU-KEY-FILENAME"});
async function detectIntent(
projectId,
sessionId,
query,
contexts,
languageCode
) {
const sessionPath = sessionClient.projectAgentSessionPath(
projectId,
sessionId
);
// The next query request.
const request = {
session: sessionPath,
queryInput: {
text: {
text: query,
languageCode: languageCode,
},
},
};
if (contexts && contexts.length > 0) {
request.queryParams = {
contexts: contexts,
};
}
const responses = await sessionClient.detectIntent(request);
return responses[0];
}
async function executeQueries(projectId, sessionId, queries, languageCode) {
let context;
let intentResponse;
for (const query of queries) {
try {
console.log(`Pergunta: ${query}`);
intentResponse = await detectIntent(
projectId,
sessionId,
query,
context,
languageCode
);
console.log('Enviando Resposta');
console.log(intentResponse.queryResult.fulfillmentText);
return `${intentResponse.queryResult.fulfillmentText}`
} catch (error) {
console.log(error);
}
}
}
function start(client) {
client.onMessage(async (msg) => {
if (msg.type === "chat"){
//integração de texto dialogflow
let textoResposta = await executeQueries("//SEU-PROJECT-NAME", msg.from, [msg.body], 'pt-BR')
client.sendText(msg.from, "Ok\n\n" + textoResposta.replace(/\\n/g, '\n'));
}
});
}
server.listen(port, function() {
console.log('App running on *: ' + port);
});