-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathrocketchat-out.js
40 lines (37 loc) · 1.34 KB
/
rocketchat-out.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
module.exports = function (RED) {
"use strict";
function RocketChatOut(n) {
RED.nodes.createNode(this, n);
const node = this;
const RocketChatApi = require('./rocket-chat').RocketChatApi;
node.on('input', function (msg) {
node.server = RED.nodes.getNode(n.server); // Retrieve the config node
if (node.server.host.indexOf('http') < 0) {
node.server.host = 'http://' + node.server.host;
}
let url;
try {
url = new URL(node.server.host);
} catch (e) {
node.error(e, msg);
}
let rocketChatApi = new RocketChatApi(url.protocol, url.hostname, url.port, node.server.user, node.server.credentials.password, "v1");
rocketChatApi.getPublicRooms(function (err, body) {
if (!err) {
node.status({fill: "blue", shape: "dot", text: "Connected"});
let rooms = body.channels.filter(f => f.name == n.room);
if (rooms.length === 1) {
rocketChatApi.sendMsg(rooms[0]._id, msg.payload, function () {
node.send(msg);
});
} else {
node.status({fill: "red", shape: "ring", text: "Error:" + err});
}
} else {
node.status({fill: "red", shape: "ring", text: "Error:" + err});
}
});
});
}
RED.nodes.registerType("rocketchat-out", RocketChatOut);
}