/
node-trello-in.js
63 lines (54 loc) · 2.19 KB
/
node-trello-in.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
const request = require('request');
// --------------------------------------------------------------------------
// NODE-RED
// --------------------------------------------------------------------------
module.exports = function(RED) {
const register = function(config) {
RED.nodes.createNode(this, config);
let node = this;
if (!config.key) {node.status({fill:"red", shape:"ring", text: 'Missing credential'}); }
let key = RED.nodes.getNode(config.key);
start(RED, node, config);
this.on('input', (data) => { input(node, data, config, key.credentials) });
this.on('close', (done) => { stop(done) });
}
RED.nodes.registerType("trello-in", register, {});
// Register for button callback
RED.httpAdmin.post("/trello-in/:id", RED.auth.needsPermission("trello-in.write"), function(req,res) {
var node = RED.nodes.getNode(req.params.id);
if (node != null) {
try {
node.receive();
res.sendStatus(200);
} catch(err) {
res.sendStatus(500);
node.error(RED._("inject.failed",{error:err.toString()}));
}
} else { res.sendStatus(404); }
});
}
const stop = (done) => { done(); }
const start = (RED, node, config) => {
let uri = '/trello-callback'+config.path+'/';
RED.httpNode.get (uri, (req, res, next) => { node.send({'payload' : req.body}); res.sendStatus(200); });
RED.httpNode.post (uri, (req, res, next) => { node.send({'payload' : req.body}); res.sendStatus(200); });
}
const input = (node, data, config, credentials) => {
let url = 'https://api.trello.com/1/tokens/'+credentials.token+'/webhooks/?key='+credentials.key;
let json = {
description: "Trello Webhook " + (config.name || node.id),
callbackURL: CONFIG.server.host + 'trello-callback'+config.path+'/',
idModel: config.model,
}
let req = {
url: url,
method: 'POST',
headers: {'ContentType': 'application/json'},
form: json
};
let n = node;
request(req, (err, response, body) => {
if (err) { return n.error(err); }
n.warn(body);
});
}