Skip to content

Instantly share code, notes, and snippets.

@monkbroc
Last active November 6, 2015 18:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save monkbroc/6ec531821f99b31fcf0b to your computer and use it in GitHub Desktop.
Save monkbroc/6ec531821f99b31fcf0b to your computer and use it in GitHub Desktop.
Button webhook
module['exports'] = function counter (hook) {
var expectedEvent = "magicButton";
function authorized() {
return hook.params.event === expectedEvent;
}
function increment(key, callback) {
var datastore = hook.datastore;
datastore.get(key, function (err, counter) {
if(err) {
callback(err);
} else {
counter = (counter || 0) + 1;
datastore.set(key, counter, function (err) {
if(err) {
callback(err);
} else {
callback(null, counter);
}
});
}
});
}
function outputResult(err, result) {
var data;
if(err) {
data = { error: err.message };
} else {
data = { result: result };
}
hook.res.end(JSON.stringify(data, true, 2));
}
// check that the event name in the hook matches the expected name
if(!authorized()) {
outputResult({ message: 'Unexpected event'});
return;
}
// increment a key in the cloud datastore
increment('counter', outputResult);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment