-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
97 lines (88 loc) · 3.01 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
95
96
97
var ApiBuilder = require('claudia-api-builder'),
api = new ApiBuilder()
module.exports = api
api.post('/call', (req) => {
const { parse } = require('querystring')
const VoiceResponse = require('twilio').twiml.VoiceResponse
const SlackBot = require('slackbots')
const bot = new SlackBot({
token: process.env.SLACK_BOT_TOKEN,
name: 'OfficeBot'
})
const twiml = new VoiceResponse()
const body = parse(req.body)
const callSid = body.CallSid
let data = {
attachments: [{
fallback: 'Someone is at the door',
title: 'Someone is at the door',
// title_link: recordingUrl,
// text: 'Click link to hear the recording',
callback_id: `door_open:${callSid}`,
actions: [
{
name: 'open_door',
text: 'Let them in',
type: 'button',
value: 'open_door'
},
{
name: 'deny_access',
text: 'No.',
type: 'button',
value: 'deny_access'
}
]
}]
}
return new Promise((resolve, reject) => {
bot.on('start', () => {
bot.postMessageToChannel('talk-charlottetown', '<!channel> Someone is at the door', data, () => {
twiml.say('One moment please.', {voice: 'alice'})
twiml.pause({length: 240})
twiml.say('Sorry, no one is in the office right now.', {voice: 'alice'})
resolve(twiml.toString())
})
})
})
}, {
success: { contentType: 'text/xml' }
})
api.post('/call/response', (req) => {
const { parse } = require('querystring')
const payload = JSON.parse(parse(req.body).payload)
const callSid = payload.callback_id.split(':')[1]
const action = payload.actions[0]
const client = require('twilio')(process.env.TWILIO_ACCOUNT_SID, process.env.TWILLIO_AUTH_TOKEN)
let continueAt = ''
let response = ''
if (action.value === 'open_door') {
continueAt = 'call/accept'
response = 'Letting them in'
} else {
continueAt = 'call/reject';
response = 'Telling them to go away'
}
return client.calls(callSid).update({
url: `https://kpib3ihpwe.execute-api.us-east-1.amazonaws.com/latest/${continueAt}`,
method: 'POST'
}).then(() => response)
}, {
success: { contentType: 'text/plain' }
})
api.post('/call/accept', (req) => {
const VoiceResponse = require('twilio').twiml.VoiceResponse
const twiml = new VoiceResponse()
twiml.play({digits: 9})
return twiml.toString()
}, {
success: { contentType: 'text/xml' }
})
api.post('/call/reject', (req) => {
const VoiceResponse = require('twilio').twiml.VoiceResponse
const twiml = new VoiceResponse()
twiml.say('Sorry, no one is in the office right now.', {voice: 'alice'})
return twiml.toString()
}, {
success: { contentType: 'text/xml' }
})