-
Notifications
You must be signed in to change notification settings - Fork 30
/
303-SlackNotification.js
67 lines (59 loc) · 2 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
65
66
67
const _axios = require('axios');
const HttpsProxyAgent = require('https-proxy-agent');
const axiosConfig = {};
if (process.env.https_proxy || process.env.http_proxy) {
axiosConfig.httpsAgent = new HttpsProxyAgent(process.env.https_proxy || process.env.http_proxy);
axiosConfig.proxy = false;
}
const axios = _axios.create(axiosConfig);
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 axios
.post(payload.hooks, payload)
.then((response) => {
node.warn(response.data);
return response.data;
})
.catch((err) => {
node.status({ fill: 'red', shape: 'dot', text: 'error', });
node.error(`Slack webhooks failed: ${err.message} ${msg}`);
});
});
}
RED.nodes.registerType('slack-notification', Notification);
};