Skip to content

Instantly share code, notes, and snippets.

@katowulf
Last active December 2, 2016 23:53
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save katowulf/bbd4f60ec9e354c03afa to your computer and use it in GitHub Desktop.
Save katowulf/bbd4f60ec9e354c03afa to your computer and use it in GitHub Desktop.
Push notifications from Firebase to Twilio.
//require the Twilio module and create a REST client
var client = require('twilio')('ACCOUNT_SID', 'AUTH_TOKEN');
var Firebase = require('firebase');
var fb = new Firebase('FIREBASE_URL');
fb.auth('FIREBASE_SECRET', function(err) {
if( err ) { throw err; }
listenForEvents();
})
function listenForEvents() {
fb.child('push_notifications').on('child_added', function(snap) {
var dat = snap.val();
sendMessage(dat.from, dat.text, snap.ref());
})
}
function sendMessage(from, text, ref) {
//Send an SMS text message
client.sendMessage({
to:'+16515556677', // Any number Twilio can deliver to
from: from, // A number you bought from Twilio and can use for outbound communication
body: text // body of the SMS message
}, function(err, responseData) { //this function is executed when a response is received from Twilio
if (err) { // "err" is an error received during the request, if any
console.error(err);
}
else {
// remove the queue event
ref.remove();
console.log(responseData);
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment