-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
246 lines (207 loc) · 4.99 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
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
const express = require('express')
const { VoiceResponse, MessagingResponse } = require('twilio').twiml
const bodyParser = require('body-parser')
const log = require('debug')('answering-machine')
const request = require('superagent')
const phones = require('./phones')
const app = express()
const { onHangup, onRecorded, onText } = require('./handlers')
app.use(bodyParser.json())
app.use(bodyParser.urlencoded())
const RECORDING_STATUS_CALLBACK =
process.env.RECORDING_STATUS_CALLBACK || 'localhost:3000/recorded'
const SURGE_SUBDOMAIN = process.env.SURGE_SUBDOMAIN
const callsInProgress = {}
/*
* GET /health
*/
app.get('/health', (req, res) => {
log('GET /health')
res.json({ healthy: true })
})
/*
* GET /record
*
* Responds to twilio with twiml saying play this message, then record
* Stores information about the call in global callsInProgress
*/
app.post('/call', (req, res) => {
const {
CallerName,
FromCity,
FromZip,
FromState,
Caller,
Called,
CallSid
} = req.body
log('POST /call from phone: %s', Called)
const twiml = new VoiceResponse()
if (phones[Called].forwardTo) {
// Begin forward flow
console.log(`Forward flow with ${phones[Called].forwardTo}`)
const dial = twiml.dial({ action: '/call-complete', timeout: 10 })
const url = '/press-one'
dial.number({ url }, phones[Called].forwardTo)
twiml.hangup()
} else {
// Straight to voicemail
const voiceUrl = phones[req.body.Called].voiceMessageUrl
const audioResponse = `https://${SURGE_SUBDOMAIN}.surge.sh/${voiceUrl}`
twiml.play({}, audioResponse)
twiml.record({
maxLength: 60,
action: process.env.RECORDING_STATUS_CALLBACK
})
// Queue timeout in case they don't leave a voicemail
callsInProgress[CallSid] = {
data: {
CallerName,
FromCity,
FromZip,
FromState,
Caller,
Called,
CallSid
},
timeout: setTimeout(() => onHangup(req.body), 100000)
}
}
res.type('text/xml')
console.log(twiml.toString())
return res.send(twiml.toString())
})
app.post('/press-one', (req, res) => {
log('POST /press-one')
log(req.body)
const twiml = new VoiceResponse()
const gather = twiml.gather({
numDigits: 1,
action: '/gather-result'
})
const voice = 'Alice'
gather.say({ voice }, 'You are receiving a campaign call, press 1 to accept.')
twiml.hangup()
res.type('text/xml')
console.log(twiml.toString())
res.send(twiml.toString())
})
/*
* POST /gather-result
*
* Either they pressed one, or they didn't press one / it went to voicemail
* This endpoint just hangs up if they didn't press anything and passes the call to /call-complete
*
* This endpoint is not triggered if the phone does not have a forward to set
*/
app.post('/gather-result', (req, res) => {
log('POST /gather-result')
log(req.body)
log(req.params)
const twiml = new VoiceResponse()
if (!req.body.Digits || req.body.Digits.length == 0) {
twiml.hangup()
}
res.type('text/xml')
console.log(twiml.toString())
res.send(twiml.toString())
})
/*
* POST /call-complete
*
* Happens at the end of a forwarding call
*
*
*/
app.post('/call-complete', (req, res) => {
log('POST /call-complete')
log(req.body)
const twiml = new VoiceResponse()
if (['completed', 'answered'].includes(req.body['DialCallStatus'])) {
twiml.hangup()
} else {
const audioResponse = `https://${SURGE_SUBDOMAIN}.surge.sh/${phones[
req.body.Called
].voiceMessageUrl}`
twiml.play({}, audioResponse)
twiml.record({
maxLength: 60,
action: process.env.RECORDING_STATUS_CALLBACK
})
}
res.type('text/xml')
console.log(twiml.toString())
return res.send(twiml.toString())
})
/*
* POST /recorded
*
* Gets a callback after the caller has hung up and the recording has been processed
* Does posting to the webhook
*/
app.post('/recorded', (req, res) => {
log('POST /recorded')
res.sendStatus(200)
const { RecordingUrl, CallSid } = req.query
const {
CallerName,
FromCity,
FromZip,
FromState,
Caller,
Called
} = callsInProgress[CallSid].data
clearTimeout(callsInProgress[CallSid].timeout)
onRecorded({
RecordingUrl,
CallSid,
CallerName,
FromCity,
FromZip,
FromState,
Caller,
Called
})
callsInProgress[CallSid] = undefined
delete callsInProgress[CallSid]
})
/*
* POST /sms
*
* Gets a callback once a text has been received
*/
app.post('/sms', (req, res) => {
log('GET /sms')
const {
Body,
From,
FromCity,
FromCountry,
FromState,
FromZip,
FromName,
To,
SmsMessageSid
} = req.body
onText({
Body,
From,
FromCity,
FromCountry,
FromState,
FromZip,
To,
FromName,
SmsMessageSid
})
const twiml = new MessagingResponse()
res.writeHead(200, { 'Content-Type': 'text/xml' })
res.end(twiml.toString())
})
/*
* Fire it up
*/
const PORT = process.env.PORT || 3000
app.listen(PORT, () => {
log('Listening on port %s', PORT)
})