Skip to content

Instantly share code, notes, and snippets.

@gkobilansky
Created October 27, 2016 02:00
Show Gist options
  • Save gkobilansky/8632ccd2d727ea7c5818944be81da13a to your computer and use it in GitHub Desktop.
Save gkobilansky/8632ccd2d727ea7c5818944be81da13a to your computer and use it in GitHub Desktop.
var events = require('events');
var request = require('request');
var Trello = require('node-trello');
// Fill these in
var trelloKey = //AppKEY;
var trelloToken = //UserToken;
var idBoard = //BoardId;
var trello = new Trello(trelloKey, trelloToken);
function boardActionEmitter(idBoard) {
var emitter = new events.EventEmitter(idBoard);
var date = new Date().toString();
var params = {
since: date
};
setInterval(
function(){
trello.get('/1/boards/'+ idBoard + '/actions', params, function(err, actions) {
if (err) console.error(err);
if (actions) params.since = new Date().toString();
actions.forEach(function(action) {
emitter.emit(action["type"], action);
});
})
}, 5000);
return emitter;
}
var emitter = boardActionEmitter(idBoard);
emitter.on('createCard', function(action) {
var idCard = action.data.card.id;
var name = action.data.card.name;
request(
{
url: "http://api.giphy.com/v1/gifs/search",
qs: {
q: name,
limit: 1,
api_key: //giphyKey
}
},
function(err, response, bodyJSON) {
if (err) {
console.error(err);
return;
}
var body = JSON.parse(bodyJSON);
var url = body.data[0].images.downsized.url;
trello.post(
"/1/cards/" + idCard + "/attachments",
{ url: url },
function(err) { if (err) console.error(err); }
);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment