forked from guidone/node-red-contrib-chatbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chatbot-slack-receive.js
333 lines (274 loc) · 9.37 KB
/
chatbot-slack-receive.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
var _ = require('underscore');
var WebClient = require('@slack/client').WebClient;
var ChatContextStore = require('./lib/chat-context-store');
var helpers = require('./lib/slack/slack');
var fs = require('fs');
var os = require('os');
var RtmClient = require('@slack/client').RtmClient;
var RTM_EVENTS = require('@slack/client').RTM_EVENTS;
module.exports = function(RED) {
function SlackBotNode(n) {
RED.nodes.createNode(this, n);
var self = this;
this.botname = n.botname;
this.usernames = [];
if (n.usernames) {
this.usernames = _(n.usernames.split(',')).chain()
.map(function(userId) {
return userId.match(/^[a-zA-Z0-9_]+?$/) ? userId : null
})
.compact()
.value();
}
if (this.credentials) {
this.token = this.credentials.token;
if (this.token) {
this.token = this.token.trim();
if (!this.rtm) {
// add realtime wrapper
//this.rtm = new RtmClient(this.token, {logLevel: 'debug'});
this.rtm = new RtmClient(this.token);
this.rtm.start();
}
}
}
this.on('close', function (done) {
self.rtm.disconnect();
done();
});
this.isAuthorized = function (username, userId) {
if (self.usernames.length > 0) {
return self.usernames.indexOf(username) != -1 || self.usernames.indexOf(String(userId)) != -1;
}
return true;
}
}
RED.nodes.registerType('chatbot-slack-node', SlackBotNode, {
credentials: {
token: {
type: 'text'
}
}
});
// creates the message details object from the original message
function getMessageDetails(message, token) {
return new Promise(function (resolve, reject) {
if (message.subtype == 'file_share') {
// fetch image
helpers.downloadFile(message.file.url_private_download, token)
.then(function (buffer) {
// todo detect which kind of file
// todo add date
// resolve with an image type
resolve({
chatId: message.channel,
type: 'photo',
inbound: true,
content: buffer
});
})
.catch(function (error) {
reject(error);
});
} else if (message.text != null) {
resolve({
chatId: message.channel,
type: 'message',
inbound: true,
// todo add date
content: message.text
});
} else {
reject('Unable to detect inbound message for Slack');
}
});
}
function SlackInNode(config) {
RED.nodes.createNode(this, config);
var node = this;
this.bot = config.bot;
this.config = RED.nodes.getNode(this.bot);
if (this.config) {
this.status({fill: 'red', shape: 'ring', text: 'disconnected'});
//node.slackBot = this.config.slackBot;
node.rtm = this.config.rtm;
if (node.rtm) {
this.status({fill: 'green', shape: 'ring', text: 'connected'});
node.rtm.on(RTM_EVENTS.MESSAGE, function (botMsg) {
// mark the original message with the platform
botMsg.transport = 'slack';
if (botMsg.type !== 'message') {
return;
}
// avoid message from the bot itself
if (botMsg.subtype === 'bot_message') {
return;
}
// todo get this dinamically
if (botMsg.user === 'U1Q04RGU9') {
// eslint-disable-next-line no-console
console.log('from myself exiting');
return;
}
//var username = !_.isEmpty(botMsg.from.username) ? botMsg.from.username : null;
//var username = ''; // todo fix
var chatId = botMsg.channel;
var channelId = botMsg.channel;
var userId = botMsg.user;
//var context = node.context();
//var isAuthorized = node.config.isAuthorized(username, userId);
var isAuthorized = true; // fix is authorized
var chatContext = ChatContextStore.getOrCreateChatContext(this, channelId);
// todo store the user
/*if (!_.isEmpty(username)) {
chatBotUsers[chatId] = {
chatId: chatId,
username: username,
timestamp: moment()
};
chatContext.set('username', username);
}*/
// store some information
chatContext.set('chatId', channelId);
//chatContext.set('messageId', botMsg.message_id);
chatContext.set('userId', userId);
//chatContext.set('firstName', botMsg.from.first_name);
//chatContext.set('lastName', botMsg.from.last_name);
chatContext.set('authorized', isAuthorized);
chatContext.set('transport', 'slack');
// decode the message, eventually download stuff
getMessageDetails(botMsg, node.rtm._token)
.then(function(payload) {
var msg = {
payload: payload,
originalMessage: {
transport: 'slack',
chat: {
id: channelId
}
},
chat: function() {
return ChatContextStore.getChatContext(node, chatId);
}
};
var currentConversationNode = chatContext.get('currentConversationNode');
// if a conversation is going on, go straight to the conversation node, otherwise if authorized
// then first pin, if not second pin
if (currentConversationNode != null) {
// void the current conversation
chatContext.set('currentConversationNode', null);
// emit message directly the node where the conversation stopped
RED.events.emit('node:' + currentConversationNode, msg);
} else {
node.send(msg);
}
})
.catch(function(error) {
node.error(error);
});
});
} else {
node.warn("no bot in config.");
}
} else {
node.warn('Missing configuration in Slack Receiver');
}
}
RED.nodes.registerType('chatbot-slack-receive', SlackInNode);
function SlackOutNode(config) {
RED.nodes.createNode(this, config);
var node = this;
this.bot = config.bot;
this.config = RED.nodes.getNode(this.bot);
if (this.config) {
this.status({fill: 'red', shape: 'ring', text: 'disconnected'});
node.rtm = this.config.rtm;
if (node.rtm) {
this.status({fill: 'green', shape: 'ring', text: 'connected'});
} else {
node.warn("no bot in config.");
}
} else {
node.warn("no config.");
}
function prepareMessage(msg) {
return new Promise(function(resolve, reject) {
var type = msg.payload.type;
var rtm = node.rtm;
var slackAPI = new WebClient(rtm._token);
switch (type) {
case 'action':
rtm.sendTyping(msg.payload.chatId);
break;
case 'message':
rtm.sendMessage(msg.payload.content, msg.payload.chatId);
break;
case 'location':
// build link
var link = 'https://www.google.com/maps?f=q&q=' + msg.payload.content.latitude + ','
+ msg.payload.content.longitude + '&z=16';
// send simple attachment
var attachments = [
{
'author_name': 'Position',
'title': link,
'title_link': link,
'color': '#7CD197'
}
];
slackAPI.chat.postMessage(msg.payload.chatId, '', {attachments: attachments});
break;
case 'photo':
var filename = msg.payload.filename || 'tmp-image-file';
var image = msg.payload.content;
var tmpFile = os.tmpdir() + '/' + filename;
// write to filesystem to use stream
fs.writeFile(tmpFile, image, function(err) {
if (err) {
reject(err);
} else {
// upload and send
slackAPI.files.upload(
filename,
{
file: fs.createReadStream(tmpFile),
filetype: 'auto',
channels: msg.payload.chatId
}, function handleContentFileUpload() {
}
);
}
}); // end writeFile
break;
default:
reject('Unable to prepare unknown message type');
}
});
}
this.on('input', function (msg) {
if (msg.payload == null) {
node.warn("msg.payload is empty");
return;
}
if (msg.payload.chatId == null) {
node.warn("msg.payload.channelId is empty");
return;
}
if (msg.payload.type == null) {
node.warn("msg.payload.type is empty");
return;
}
//var channelId = msg.payload.chatId;
//var noop = function() {};
/*if (msg.payload.content == null) {
node.warn("msg.payload.content is empty");
return;
}*/
prepareMessage(msg)
.then(function() {
//node.slackBot.postMessage(channelId, obj.content, obj.params, noop);
});
});
}
RED.nodes.registerType('chatbot-slack-send', SlackOutNode);
};