forked from lucianocosta/node-red-contrib-rocketchat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rocketchat-out.js
182 lines (173 loc) · 4.74 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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
const api = require('./rocketchat');
const stringifyError = require('./utils/stringifyError');
module.exports = function (RED) {
'use strict';
function RocketChatOut(config) {
RED.nodes.createNode(this, config);
const node = this;
node.server = RED.nodes.getNode(config.server);
const {
messageText,
messageTextType,
avatarText,
avatarTextType,
aliasText,
aliasTextType,
emojiText,
emojiTextType,
attachments: configAttachments,
attachmentsType,
attachmentsHeaders: configAttachmentsHeaders,
attachmentsHeadersType,
allowedFileTypes: configAllowedFileTypes,
allowedFileTypesType,
room,
roomType,
roomData,
liveChatTokenConfig,
liveChatTokenConfigType,
} = config;
node.on('input', async function (msg) {
const { host, user, token } = node.server;
const apiInstance = api({ host, user, token });
let roomId;
if (roomType === 'form') {
const { i } = JSON.parse(roomData);
roomId = i;
} else {
roomId = RED.util.evaluateNodeProperty(room, roomType, this, msg);
}
const avatar = RED.util.evaluateNodeProperty(avatarText, avatarTextType, this, msg);
const alias = RED.util.evaluateNodeProperty(aliasText, aliasTextType, this, msg);
const emoji = RED.util.evaluateNodeProperty(emojiText, emojiTextType, this, msg);
const text = RED.util.evaluateNodeProperty(messageText, messageTextType, this, msg);
const attachments = RED.util.evaluateNodeProperty(configAttachments, attachmentsType, this, msg);
let attachmentsHeaders = RED.util.evaluateNodeProperty(
configAttachmentsHeaders,
attachmentsHeadersType,
this,
msg
);
let allowedFileTypes = RED.util.evaluateNodeProperty(
configAllowedFileTypes,
allowedFileTypesType,
this,
msg
);
const liveChatToken = RED.util.evaluateNodeProperty(
liveChatTokenConfig,
liveChatTokenConfigType,
this,
msg
);
if (roomId == null) {
try {
if (config.destination === 'live' || config.destination === 'chatbot_response') {
const getLiveChatRoomsResponse = await apiInstance.getLiveChatRooms({
visitorToken: liveChatToken,
});
if (getLiveChatRoomsResponse.rooms.length >= 1) {
roomId = getLiveChatRoomsResponse.rooms[0]._id;
}
} else {
throw new Error('roomId cannot be null when destination is not live');
}
} catch (error) {
node.warn(RED._('rocketchat-out.errors.invalid-data', error));
node.status({
fill: 'red',
shape: 'ring',
text: RED._('rocketchat-out.errors.invalid-data', error),
});
return;
}
}
node.status({ fill: 'blue', shape: 'dot', text: 'rocketchat-out.label.sending' });
try {
switch (config.destination) {
case 'users': {
const { success, room, errors } = await apiInstance.createIM({ username: roomId });
if (success) {
const { _id } = room;
roomId = _id;
} else {
throw new Error(errors);
}
break;
}
case 'rooms': {
await apiInstance.send({
roomId,
text,
attachments,
alias,
avatar,
emoji,
});
break;
}
case 'chatbot_response': {
await apiInstance.sendMessage({
rid: roomId,
msg: text,
});
break;
}
case 'live': {
if (Array.isArray(attachments) && attachments.length >= 1) {
await apiInstance.sendMessage({
rid: roomId,
msg: text,
});
for (const attachment of attachments) {
let uri =
attachment.video_url ||
attachment.audio_url ||
attachment.image_url ||
attachment.file_url;
try {
await apiInstance.downloadAndUploadFile({
uri,
rid: roomId,
msg: attachment.caption || "User didn't set caption for this attachment",
headers: attachmentsHeaders,
allowedFileTypes,
});
} catch (error) {
let errorText;
if (error.errorType === 'error-invalid-file-type') {
errorText = 'Upload failed because of invalid file type.';
} else {
errorText = 'Upload failed.';
}
await apiInstance.sendMessage({
msg: errorText,
rid: roomId,
});
}
}
} else {
await apiInstance.liveChatSend({
token: liveChatToken,
text,
rid: roomId,
});
}
break;
}
default:
throw new Error('Invalid destination');
}
node.status({});
} catch (error) {
node.error(stringifyError(error));
node.status({
fill: 'red',
shape: 'ring',
text: RED._('rocketchat-out.errors.error-processing', error),
});
}
});
}
RED.nodes.registerType('rocketchat-out', RocketChatOut);
};