Last active
December 2, 2016 23:53
-
-
Save katowulf/bbd4f60ec9e354c03afa to your computer and use it in GitHub Desktop.
Push notifications from Firebase to Twilio.
This file contains 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
//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