-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlex-voice-application.js
196 lines (138 loc) · 4.21 KB
/
lex-voice-application.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
'use strict';
let fs = require('fs');
let request = require('request');
require("dotenv").config();
const express = require('express');
let bodyParser = require('body-parser');
const app = express();
let Nexmo = require('nexmo');
const apiKey = process.env.API_KEY;
const apiSecret = process.env.API_SECRET;
const appId = process.env.APP_ID;
const awsKey = process.env.AWS_KEY;
const awsSecret = process.env.AWS_SECRET;
const lexReferenceConnection = process.env.LEX_REFERENCE_CONNECTION;
const botName = process.env.BOT_NAME;
const publishAlias = process.env.BOT_ALIAS;
const port = process.env.PORT || 8000;
const privateKey = require('fs').readFileSync('private.key');
const nexmo = new Nexmo({
apiKey: apiKey,
apiSecret: apiSecret,
applicationId: appId,
privateKey: privateKey
});
//-----------
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
//-----------
app.get('/lambda', (req, res) => {
res.status(200).send('Ok');
});
//-----
app.get('/answer', (req, res) => {
const hostName = `${req.hostname}`;
const uuid = req.query.uuid;
const callerNumber = req.query.from;
res.json(
[
{
"action": "talk",
"voiceName": "Joanna",
"text": "Please wait"
}
,
{
"action": "connect",
"endpoint": [
{
"content-type": "audio/l16;rate=8000",
"headers": {
"aws_key": awsKey,
"aws_secret": awsSecret,
"client_id": uuid + "_" + callerNumber, // Set to any argument as needed by your application logic
"webhook_url": "https://" + hostName + "/analytics", // Will receive transcripts and sentiment analysis (if latter is enabled)
"sensitivity": 3 // Voice activity detection, possible values 0 (most sensitive) to 3 (least sensitive)
},
"type": "websocket",
"uri": "wss://" + lexReferenceConnection + "/bot/" + botName + "/alias/" + publishAlias + "/user/" + req.query.uuid + "_" + req.query.from + "/content"
}
],
"from": "12995550101",
"eventType": "synchronous",
"eventMethod": "POST",
"eventUrl": ["https://" + hostName + "/ws_event?orig_uuid=" + uuid]
}
]
);
});
//-----------
app.get('/event', (req, res) => {
res.status(200).send('Ok');
});
//------------
app.post('/event', (req, res) => {
res.status(200).send('Ok');
});
//-----------
app.post('/ws_event', (req, res) => {
if (req.body.status == "answered") {
console.log (">>> Websocket answered");
const wsUuid = req.body.uuid;
// Get Lex to say its welcome greeting right from the start
setTimeout(() => {
console.log (">>> Send greeting TTS");
nexmo.calls.talk.start(wsUuid, {text: 'Good morning', voiceName: 'Joanna', loop: 1}, (err, res) => {
if (err) { console.error('>>> TTS to bot websocket ' + wsUuid + ' error:', err); }
else {console.log ('>>> TTS to bot websocket ' + wsUuid + ' ok!')}
});
}, 500);
}
// if (req.body.status == "answered") {
// console.log (">>> Websocket answered");
// res.json(
// [
// {
// "action": "talk",
// "voiceName": "Joanna",
// "text": "Hello"
// }
// ]
// );
// }
// else {
// res.json([]);
// }
res.status(200).send('Ok');
});
//-----------
app.post('/recordings', (req, res) => {
res.status(200).send('Ok');
});
//-----------
app.post('/analytics', (req, res) => {
console.log(">>> POST body content:", req.body);
res.status(200).send('Ok');
});
//------------
app.use ('/', express.static(__dirname));
app.get('/:name', function (req, res, next) {
let options = {
root: __dirname,
dotfiles: 'deny',
headers: {
'x-timestamp': Date.now(),
'x-sent': true
}
};
let fileName = req.params.name;
res.sendFile(fileName, options, function (err) {
if (err) {
next(err);
} else {
console.log('Sent:', fileName);
}
});
});
//--------
app.listen(port, () => console.log(`Server application listening on port ${port}!`));