-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
94 lines (79 loc) · 2.76 KB
/
app.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
// Environment Variables
// **********
// CALLER_ID - The caller ID used for outbound connections
// DEST_TYPE - The type of connection to connect the hotline to
// DEST_NUMBER - The PSTN number to connect. This is used when DEST_TYPE is set to 'phone'
// DEST_EXT - The Vonage Business Cloud extension number to connect. This is used when DEST_TYPE is set to 'vbc'
// DEST_SIP - The SIP URI to connect. This is used when DEST_TYPE is set to 'sip'
// FALLBACK_TYPE - The type of connection to connect the hotline to
// FALLBACK_NUMBER - The PSTN number to connect. This is used when FALLBACK_TYPE is set to 'phone'
// FALLBACK_EXT - The Vonage Business Cloud extension number to connect. This is used when FALLBACK_TYPE is set to 'vbc'
// FALLBACK_SIP - The SIP URI to connect. This is used when FALLBACK_TYPE is set to 'sip'
// VOICE_NAME - Nexmo voice to use for text-to-spech
// PORT - The port the application should listen on
require('dotenv').config()
const express = require('express')
const bodyParser = require('body-parser');
const app = express()
const port = process.env.PORT || 3000
const callerId = process.env.CALLER_ID
const voiceName = process.env.VOICE_NAME || "Amy";
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.get('/', (req, res) => {
res.redirect('/answer')
})
app.post('/event', (req, res) => {
var ncco = []
if(['timeout','unanswered'].includes(req.body.status)){
var endpoints = []
switch(process.env.FALLBACK_TYPE) {
case 'phone':
endpoints.push({type: 'phone', number: process.env.FALLBACK_NUMBER})
break
case 'vbc':
endpoints.push({type: 'vbc', extension: process.env.FALLBACK_EXT})
break
case 'sip':
endpoints.push({type: 'sip', uri: process.env.FALLBACK_SIP})
break
}
ncco.push({
action: "connect",
from: callerId,
endpoint: endpoints
})
} else {
ncco.push({})
}
res.json(ncco)
})
app.get('/answer', (req, res) => {
var ncco = [{
action: "talk",
voiceName: voiceName,
text: "Thank you for calling the Smart IVR Fallback Demo! Stay on the line while we connect you."
}]
endpoints = []
switch(process.env.DEST_TYPE) {
case 'phone':
endpoints.push({type: 'phone', number: process.env.DEST_NUMBER})
break
case 'vbc':
endpoints.push({ type: 'vbc', extension: process.env.DEST_EXT})
break
case 'sip':
endpoints.push({type: 'sip', uri: process.env.DEST_SIP})
break
}
ncco.push({
action: 'connect',
timeout: 3,
from: callerId,
eventType: "synchronous",
eventUrl: [`${req.protocol}://${req.get('host')}/event`],
endpoint: endpoints
})
res.json(ncco)
})
app.listen(port, () => console.log(`Example app listening on port ${port}!`))