-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
62 lines (54 loc) · 1.21 KB
/
index.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
const app = require('express')();
const bodyParser = require('body-parser');
app.use(bodyParser.json());
app.get('/webhooks/answer', (req, res) => {
res.json(mainMenu(req));
});
app.post('/webhooks/events', (req, res) => {
console.log(req.body);
res.sendStatus(204);
});
app.post('/webhooks/dtmf', (req, res) => {
let actions = [];
let ncco = [];
switch (req.body.dtmf.digits) {
case '1':
actions.push({
action: 'talk',
text: `It is ${new Intl.DateTimeFormat(undefined, {
dateStyle: 'full',
timeStyle: 'long',
}).format(Date.now())}`,
});
break;
case '2':
actions.push({
action: 'stream',
streamUrl: [
'https://nexmo-community.github.io/ncco-examples/assets/voice_api_audio_streaming.mp3',
],
});
}
ncco = actions.concat(mainMenu(req));
console.log(ncco);
res.json(ncco);
});
function mainMenu (req) {
return [
{
action: 'talk',
bargeIn: true,
text:
'Welcome. Press 1 to hear the current date or 2 to play audio. Press any other key to hear these options again.',
},
{
action: 'input',
type: [ 'dtmf' ],
dtmf: {
maxDigits: 1,
},
eventUrl: [ `${req.protocol}://${req.get('host')}/webhooks/dtmf` ],
},
];
}
app.listen(3000);