Skip to content

Instantly share code, notes, and snippets.

@dreadjr
Forked from katowulf/push_notifications.js
Created September 25, 2015 17:35
Show Gist options
  • Save dreadjr/2bb16a20bf9b2b6e66d6 to your computer and use it in GitHub Desktop.
Save dreadjr/2bb16a20bf9b2b6e66d6 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