-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathorchestrationLayer.js
executable file
·62 lines (55 loc) · 2.32 KB
/
orchestrationLayer.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
let server = new require("./server.js");
let nlp = new require("./nlp.js");
//Handle the post request
let processPostRequest = (body, path, callback) => {
console.log('Process ' + path);
if (path == '/api/v1/watson/converse') {
try {
if (body.constructor !== Object) {
body = JSON.parse(body)
}
console.log("Connect to Watson and send transcript");
nlp.getConverseResult(body['fm-question'], body['fm-conversation'], (speech, instructions, conversationPayload) => {
console.log("Watson returned a result");
console.log("Speech: " + speech + " Instructions: " + instructions + " Conversation Payload: " + conversationPayload);
let avatarResponse = {
'answer': speech,
'instructions': instructions
};
callback(JSON.stringify({
"answer": JSON.stringify(avatarResponse),
"matchedContext": "",
conversationPayload: JSON.stringify(conversationPayload)
}));
});
} catch (e) {
console.log(e.toString());
callback("{}");
}
} else if (path == '/v2/{session=projects/*/agent/sessions/*}:detectIntent') {
waitForDialogFlow(body, callback);
}
}
async function waitForDialogFlow(body, callback) {
console.log("Connect to Dialogflow and send transcript");
await nlp.getDialogFlowResult(body['fm-question'], body['fm-conversation'], (speech, instructions, conversationPayload) => {
// console.log("Dialogflow returned a result");
// console.log("Speech: " + speech + " Instructions: " + instructions + " Conversation Payload: " + conversationPayload);
let avatarResponse = {
'answer': speech,
'instructions': instructions
};
callback(JSON.stringify({
"answer": JSON.stringify(avatarResponse),
"matchedContext": "",
conversationPayload: JSON.stringify(conversationPayload)
}));
});
}
let startServer = (port) => {
server.createServer(port, processPostRequest)
}
module.exports = {
startServer: startServer,
processPostRequest: processPostRequest
};