-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
94 lines (74 loc) · 2.32 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
var express = require('express');
var bodyParser = require('body-parser');
var twilio = require('twilio');
// Construct the app based on the passed-in configuration parameters.
module.exports = function appctor(cfg) {
var twiclient = twilio(cfg.twilio.accountSid, cfg.twilio.authToken);
// Create the app
var app = express();
app.set('trust proxy', true);
// Parse incoming request bodies
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
// Use the Connect favicon.
app.use(express.static(__dirname + '/static'));
function playResponse(req, res) {
// Create our TwiML response.
var resTwiml = new twilio.TwimlResponse();
// Play any defined sound, or just hang up if undefined
if (req.query.play) {
resTwiml.play(req.query.play);
} else {
resTwiml.hangup();
}
// Send the response we've built to Twilio
return res.type('text/xml').send(resTwiml.toString());
}
function callNumber(req, res, next) {
if (!req.body.to) {
next('No number specified');
}
var responseUrl = req.protocol + '://' + req.header('host') + '/response';
if (req.body.play) {
responseUrl += '?play=' + encodeURIComponent(req.body.play);
}
return twiclient.makeCall({
to: req.body.to,
from: cfg.twilio.number,
url: responseUrl
}).then(function (result) {
res.send({status: 'ok'});
}).catch(function (err) {
console.log(err);
return next(err);
});
}
function smsNumber(req, res, next) {
if (!req.body.to) {
next('No numbers specified');
}
if (!Array.isArray(req.body.to)){
req.body.to = [req.body.to];
}
return Promise.all(req.body.to.map(function (number) {
return twiclient.sms.messages.post({
to: number,
from: cfg.twilio.number,
body: req.body.body
});
})).then(function (result) {
res.send({status: 'ok'});
}).catch(function (err) {
console.log(err);
return next(err);
});
}
// The route for recieving a call from Twilio.
app.get('/response', playResponse);
app.post('/response', playResponse);
app.post('/call', callNumber);
app.post('/sms', smsNumber);
// Respond with a 404 code for any other requests
app.use(function(req,res){return res.status(404).send()});
return app;
};