-
Notifications
You must be signed in to change notification settings - Fork 12
/
app.js
52 lines (45 loc) · 1.53 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
var twilio = require('twilio'),
client = twilio('ACCOUNTSID', 'AUTHTOKEN'),
cronJob = require('cron').CronJob;
var express = require('express'),
bodyParser = require('body-parser'),
app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
var Firebase = require('firebase'),
usersRef = new Firebase('{FIREBASEURL}/Users/');
var numbers = [];
usersRef.on('child_added', function(snapshot) {
numbers.push( snapshot.val() );
console.log( 'Added number ' + snapshot.val() );
});
var textJob = new cronJob( '0 18 * * *', function(){
for( var i = 0; i < numbers.length; i++ ) {
client.sendMessage( { to:numbers[i], from:'YOURTWILIONUMBER', body:'Hello! Hope you’re having a good day.'}, function( err, data ) {
console.log( data.body );
});
}
}, null, true);
app.post('/message', function (req, res) {
var resp = new twilio.TwimlResponse();
if( req.body.Body.trim().toLowerCase() === 'subscribe' ) {
var fromNum = req.body.From;
if(numbers.indexOf(fromNum) !== -1) {
resp.message('You already subscribed!');
} else {
resp.message('Thank you, you are now subscribed. Reply "STOP" to stop receiving updates.');
usersRef.push(fromNum);
}
} else {
resp.message('Welcome to Daily Updates. Text "Subscribe" receive updates.');
}
res.writeHead(200, {
'Content-Type':'text/xml'
});
res.end(resp.toString());
});
var server = app.listen(3000, function() {
console.log('Listening on port %d', server.address().port);
});