Created
September 18, 2014 18:58
-
-
Save RobSpectre/6250db0aaffedab9438b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var app = require('express')(); | |
var twilio = require('./node_modules/twilio'); | |
var http = require('http').Server(app); | |
var io = require('socket.io')(http); | |
var bodyParser = require('body-parser'); | |
app.get('/', function(req, res){ | |
res.sendfile('index.html'); | |
}); | |
app.get('/stashes', function(req, res) { | |
var client = new twilio.RestClient(process.env.TWILIO_ACCOUNT_SID, | |
process.env.TWILIO_AUTH_TOKEN); | |
client.messages.get({from: '+16468468238', status: 'delivered', num_media: 1, page_size: 100}, function(err, response) { | |
response.messages.forEach(function(message) { | |
if (message.num_media != '0') { | |
console.log(message); | |
client.messages(message.sid).media.list(function(err, response) { | |
console.log(media.uri); | |
io.emit('stash', media.uri); | |
}); | |
} | |
}); | |
}); | |
res.json("{'status': 'OK'}"); | |
}); | |
app.post('/log', function(req, res) { | |
var num_media = request.body.num_media; | |
var status = request.body.status; | |
if (status == 'delivered') { | |
if (num_media != '0') { | |
io.emit('stash', request.body.media_url_0); | |
} | |
} else { | |
io.emit('error', "An error occured."); | |
} | |
}); | |
io.on('connection', function(socket){ | |
socket.on('stash', function(url){ | |
io.emit('stash', url); | |
}); | |
socket.on('error', function(err){ | |
io.emit('error', err); | |
}); | |
}); | |
http.listen(3000, function(){ | |
console.log('listening on *:3000'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment