-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwebhookrelay.js
111 lines (95 loc) · 3.99 KB
/
webhookrelay.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/**
* Copyright JS Foundation and other contributors, http://js.foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
// If you use this as a template, update the copyright with your own name.
// Sample Node-RED node file
module.exports = function (RED) {
"use strict";
// require any external libraries we may need....
//var foo = require("foo-library");
var ws = require(`webhookrelay-ws-client`);
// The main node definition - most things happen in here
function WebhookRelayNode(n) {
// Create a RED node
RED.nodes.createNode(this, n);
this.title = n.title
this.status({ fill: "red", shape: "ring", text: "disconnected" });
// Store local copies of the node configuration (as defined in the .html)
if (n.buckets !== "") {
this.bucketsFilter = n.buckets.split(",");
for (var i = 0; i < this.bucketsFilter.length; i++) {
this.bucketsFilter[i] = this.bucketsFilter[i].trim()
}
} else {
this.bucketsFilter = []
}
var credentials = this.credentials;
if ((credentials) && (credentials.hasOwnProperty("key"))) { this.key = credentials.key; }
else { this.error("No Webhook Relay token key set"); }
if ((credentials) && (credentials.hasOwnProperty("secret"))) { this.secret = credentials.secret; }
else { this.error("No Webhook Relay token secret set"); }
// copy "this" object in case we need it in context of callbacks of other functions.
var node = this;
var handler = function (payload) {
// checking for status messages
var parsed = JSON.parse(payload);
if (parsed.type === 'status') {
switch (parsed.status) {
case 'subscribed':
node.status({ fill: "green", shape: "dot", text: "connected" });
break;
case 'unauthorized':
node.status({ fill: "red", shape: "ring", text: "unauthorized" });
break;
default:
break;
}
// don't pass status messages to
// Node-RED
node.log(`webhook relay status event received: '${parsed.status}', message: '${parsed.message}'`)
return;
}
var msg = {
topic: parsed.meta.bucket_name,
payload: parsed
}
node.send(msg);
}
if (this.key && this.secret) {
var client = new ws.WebhookRelayClient(node.key, node.secret, node.bucketsFilter, handler);
client.connect();
node.whrClient = client;
}
this.on('input', function (msg) {
node.log(`responding to webhook event: '${msg.meta.id}', status: '${msg.status}'`)
var client = new ws.WebhookRelayClient(node.key, node.secret);
client.respond(msg)
});
this.on("close", function () {
// TODO: disconnect ws-client
if (node.whrClient) {
node.whrClient.disconnect();
}
});
}
// Register the node by name. This must be called before overriding any of the
// Node functions.
RED.nodes.registerType("webhookrelay", WebhookRelayNode, {
credentials: {
key: { type: "password" },
secret: { type: "password" }
}
});
}