Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Chain.com Webhooks Example
/*
Chain's Webhooks system will send an HTTP request
to our app with an event type of 'address-transaction'.
The body of the request will contain Chain's transaction
object. We can use this payload to do whatever we want!
*/
app.post('/', function (req, res) {
if (req.body['event'] == 'address-transaction') {
sendSMS(req.body.transaction)
res.send('OK\n');
}
});
// Using twilio's Node.js SDK, we can send
// an SMS to our phone alerting us of the change
// to our address.
var sendSMS = function(txn) {
var b = 'sent=' + txnDiff(txn.outputs) +
'received=' + txnDiff(txn.inputs);
twilio.messages.create({
body: b,
to: process.env.YOUR_NUMBER,
from: process.env.TWILIO_NUMBER
}, function(err, message) {
process.stdout.write(message.sid);
});
}
/*
This function will sum the values of inputs or outputs
if the particular input or output is related to
the address we specify in our environment.
*/
var txnDiff = function(col) {
return _.filter(col, function(x) {
return _.contains(x['addresses'], process.env.ADDRESS);
}).reduce(col, function(memo, num) {return memo + num;}, 0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment