-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
113 lines (91 loc) · 3.18 KB
/
server.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
101
102
103
104
105
106
107
108
109
110
111
112
113
const http = require('http');
const chalk = require('chalk');
const qs = require('querystring');
const VoiceResponse = require('twilio').twiml.VoiceResponse;
const TIE = require('@artificialsolutions/tie-api-client');
const {
TENEO_ENGINE_URL,
WEBHOOK_FOR_TWILIO,
FIRST_INPUT_FOR_TENEO,
LANGUAGE_STT,
LANGUAGE_TTS,
PORT
} = process.env;
const port = PORT || 1337;
const teneoApi = TIE.init(TENEO_ENGINE_URL);
const firstInput = FIRST_INPUT_FOR_TENEO || '';
const language_STT = LANGUAGE_STT || 'en-GB';
const language_TTS = LANGUAGE_TTS || 'en-GB';
/***
* VERY BASIC SESSION HANDLER
***/
var keyPair = new Map();
function getSession (userId) {
var sessionId = '';
sessionId = keyPair.get(userId);
if (typeof(sessionId) == 'undefined')
sessionId = null;
return sessionId;
}
function setSession (userId, sessionId) {
keyPair.set(userId,sessionId);
}
/***
* LISTEN FOR INPUTS FROM TWILIO, SEND TO TENEO AND RESPOND
***/
var server = http.createServer((req, res) => {
var body = '';
req.on('data', function (data) {
body += data;
});
req.on('end', function () {
var post = qs.parse(body);
var textToSend = '';
if (post.CallStatus == 'ringing') { // If first input of call, send default input to Teneo (blank here)
textToSend = firstInput;
} else if (post.CallStatus = 'in-progress' && post.SpeechResult) { // Spoken responses
textToSend = post.SpeechResult;
} else { // Unrecognized, send blank
textToSend = '';
}
var callId = post.CallSid;
var phoneNumber = post.Caller;
var teneoSessionId = getSession(callId);
teneoApi.sendInput(teneoSessionId, {text: textToSend, channel: 'twilio', phoneNumber: phoneNumber}).then(teneoResponse => {
setSession(callId, teneoResponse.sessionId);
const twiml = new VoiceResponse();
var response = null;
var customVocabulary = ''; // If the output parameter 'twilio_customVocabulary' exists, it will be used for custom vocabulary understanding. This should be a string separated list of words to recognize
if (teneoResponse.output.parameters.twilio_customVocabulary) {
customVocabulary = teneoResponse.output.parameters.twilio_customVocabulary;
}
if (teneoResponse.output.parameters.twilio_endCall == 'true') { // If the output parameter 'twilio_endcall' exists, the call will be ended
response = twiml.hangup();
} else {
console.log("Custom vocab: "+teneoResponse.output.parameters.twilio_customVocabulary);
response = twiml.gather({
language: language_STT,
hints: customVocabulary,
action: WEBHOOK_FOR_TWILIO,
input: 'speech',
speechTimeout: 1
});
/*
response.say({
voice: 'woman',
language: language_TTS
}, teneoResponse.output.text);
*/
response.say(teneoResponse.output.text);
}
console.log(chalk.yellow('Caller ID: '+callId));
if (textToSend)
console.log(chalk.green('Captured Input: '+textToSend));
if (teneoResponse.output.text)
console.log(chalk.blue('Spoken Output: '+teneoResponse.output.text));
res.writeHead(200, { 'Content-Type': 'text/xml' });
res.end(twiml.toString());
});
});
}).listen(port);
console.log(chalk.bold('Twilio will send messages to this server on: '+WEBHOOK_FOR_TWILIO+':'+port));