Skip to content

Instantly share code, notes, and snippets.

@banzera
Forked from devjin0617/app.js
Created March 7, 2017 20:51
Show Gist options
  • Save banzera/c0e96ef2b7141bb71204460b3adb19d3 to your computer and use it in GitHub Desktop.
Save banzera/c0e96ef2b7141bb71204460b3adb19d3 to your computer and use it in GitHub Desktop.
Sample Trello node.js Webhook Server
/**
* This is a sample webhook server that listens for webhook
* callbacks coming from Trello, and updates any cards that are
* added or modified so everyone knows they are "PRIORITY"
*
* To get started
* * Add your key and token below
* * Install dependencies via `npm install express request body-parser`
* * Run `node app.js` on a publicly visible IP
* * Register your webhook and point to http://<ip or domain>:3123/priority
*
* Note that this sample does not authenticate incoming signatures,
* which Trello DOES support.
*/
var express = require('express'),
request = require('request'),
app = express(),
bodyParser = require('body-parser'),
port = process.env.PORT || 3123,
env = process.env.NODE_ENV || 'development',
key = "YOUR KEY",
token = "YOUR TOKEN";
// Allows us to easily read the payload from the webhook
app.use( bodyParser.json() );
// Only act when a specific route is called
// This reduces malicious / accidental use
app.all("/priority", function(req, res, next) {
// What type of actions do we want to respond to?
// In this case, updateCard or createCard
if(req.body.action.type === 'updateCard' ||
req.body.action.type === 'createCard' &&
req.body.action.data.card.name) {
// Get the name and id of the card
oldName = req.body.action.data.card.name;
id = req.body.action.data.card.id;
// Let's only update if it's not already marked priority
if(oldName.indexOf("PRIORITY:") === -1) {
newName = "PRIORITY:" + oldName;
// Construct and send the Trello PUT with the new name
var path = 'https://api.trello.com/1/cards/' + id + '/name?key=' + key + '&token=' + token;
request(
{
method: 'PUT',
uri: path,
body: {value: newName},
json: true
},
function (error, response, body) {
if(response.statusCode == 200){
console.log("successfully updated card");
} else {
console.log('error: '+ response.statusCode);
console.log(body);
}
});
}
}
res.send('OK');
});
// Standard NodeJS Listener
var server = app.listen(port, function () {
var host = server.address().address;
var port = server.address().port;
console.log('Priority Enforcer listening at http://%s:%s in %s', host, port, env);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment