-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
49 lines (43 loc) · 1.29 KB
/
server.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
var bodyParser = require('body-parser');
var express = require('express');
var app = express();
var smsService = require('./twilioFuncs.js');
// var auth = require('http-auth');
// var basic = auth.basic({
// realm: "LEAN sms blast",
// file: __dirname + "/users.htpasswd"
// });
//app.use('/',auth.connect(basic));
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
app.post('/send', function(req, res) {
// var count = 0;
var toArray = req.body['send-to']
.split(',')
.map(function(val) {
if (val.substring(0, 1) === '0') {
val = val.substring(1);
}
return '+61' + val;
});
var message = req.body.message;
smsService.sendMessages(toArray, message, function(err, sendResult) {
if (err) {
return res.end('Error sending messages ' + err);
}
console.log(sendResult);
var html = 'Sent' + sendResult.count + ' messages successfully\n';
if (sendResult.errorCount > 0) {
html += 'and encountered ' + sendResult.errorCount + ' errors\n';
for (var e in sendResult.errors) {
html += e + ': ' + JSON.stringify(sendResult.errors[e]) + '\n';
}
}
res.end(html);
});
});
app.post('/incoming', smsService.smsToEmail);
app.use(express.static(__dirname + '/public'));
app.listen(8080);