Skip to content

Instantly share code, notes, and snippets.

@StephenFluin
Created October 9, 2015 14:47
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save StephenFluin/c99bbd64ba14b1c98633 to your computer and use it in GitHub Desktop.
Save StephenFluin/c99bbd64ba14b1c98633 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);
});
@bbeale
Copy link

bbeale commented Dec 4, 2019

4 years later, thank you SO MUCH for this. I had spent hours banging my head against my keyboard while trying to get a simple webhook up and running with little progress, no thanks to the lack of adequate official documentation.

This even works alongside my existing Power-Up code that I'd written a while ago.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment