forked from Samsung/SamsungAutomationStudio
-
Notifications
You must be signed in to change notification settings - Fork 1
/
303-SlackNotification.js
64 lines (57 loc) · 1.97 KB
/
303-SlackNotification.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
64
const request = require('request-promise');
const JWT = require('jsonwebtoken');
const when = require('when');
function createRequestOption(_method, _url, _payload, _headers) {
var req = this;
var prox;
if (process.env.http_proxy != null) {
prox = process.env.http_proxy;
}
if (process.env.HTTP_PROXY != null) {
prox = process.env.HTTP_PROXY;
}
if (prox) {
req.proxy = prox;
}
req.method = _method;
req.url = _url;
if (_payload) {
req.body = _payload;
}
req.json = true;
if (_headers) {
req.headers = _headers;
}
}
module.exports = function (RED) {
function Notification(n) {
RED.nodes.createNode(this, n);
var node = this;
node.name = n.name;
node.notificationtype = n.notificationtype;
node.datatype = n.datatype;
node.datavalue = n.datavalue;
node.on("input",function(msg) {
let payload = RED.util.evaluateNodeProperty(node.datavalue, node.datatype, this, msg);
if (payload.hooks == null || payload.hooks.indexOf("slack") < 0) {
node.status({fill: "red", shape: "dot", text: "error"});
node.error("Invalid 'hooks'. 'hooks' is required.");
return;
}
return when.promise((resolve, reject) => {
let requestOption = new createRequestOption('POST', payload.hooks, payload, null);
request(requestOption)
.then(body => {
node.warn(body);
resolve(JSON.stringify(body));
})
.catch(err => {
node.status({ fill: "red", shape: "dot", text: "error" });
node.error("Slack webhooks failed: " + err.toString(), msg);
reject(err);
});
});
});
}
RED.nodes.registerType('slack-notification', Notification);
};