forked from howdyai/botkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTeams.js
594 lines (452 loc) · 19.2 KB
/
Teams.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
var Botkit = require(__dirname + '/CoreBot.js');
var express = require('express');
var bodyParser = require('body-parser');
var querystring = require('querystring');
var request = require('requestretry');
var clone = require('clone');
var async = require('async');
var TeamsAPI = require(__dirname + '/TeamsAPI.js');
function TeamsBot(configuration) {
var controller = Botkit(configuration || {});
controller.api = TeamsAPI(configuration || {});
controller.api.getToken(function(err) {
if (err) {
// this is a fatal error - could not create a Teams API client
throw new Error(err);
}
});
controller.defineBot(function(botkit, config) {
var bot = {
type: 'teams',
botkit: botkit,
config: config || {},
utterances: botkit.utterances
};
bot.startConversation = function(message, cb) {
botkit.startConversation(this, message, cb);
};
bot.createConversation = function(message, cb) {
botkit.createConversation(this, message, cb);
};
bot.channelLink = function(channel_info) {
return '<a href="https://teams.microsoft.com/l/channel/' + channel_info.id + '/' + channel_info.name + '">' + channel_info.name + '</a>';
};
bot.startPrivateConversation = function(message, cb) {
bot.createPrivateConversation(message, function(err, new_convo) {
if (err) {
cb(err);
} else {
new_convo.activate();
cb(null, new_convo);
}
});
};
bot.createPrivateConversation = function(message, cb) {
bot.openPrivateConvo(message, function(err, new_convo) {
if (err) {
cb(err);
} else {
message.raw_message.conversation = new_convo;
bot.createConversation(message, cb);
}
});
};
bot.openPrivateConvo = function(src, cb) {
var data = {
bot: src.recipient,
members: [src.raw_message.from],
channelData: src.channelData,
};
bot.api.createConversation(data, cb);
};
bot.openConvo = function(src, members, cb) {
var data = {
isGroup: true,
bot: src.recipient,
members: members,
channelData: src.channelData,
};
bot.api.createConveration(data, cb);
};
bot.send = function(message, cb) {
bot.api.addMessageToConversation(message.conversation.id, message, cb);
};
bot.replyWithActivity = function(src, message, cb) {
var data = {
type: 'message',
recipient: src.raw_message.from,
from: src.raw_message.recipient,
conversation: src.conversation,
channelData: {
notification: {
alert: true
}
},
text: message.text,
summary: message.summary,
attachments: message.attachments || null,
attachmentLayout: message.attachmentLayout || 'list',
};
bot.api.addMessageToConversation(src.conversation.id, data, cb);
};
bot.replyToComposeExtension = function(src, attachments, cb) {
// attachments will be an array of attachments
// need to wrap it in necessary stuff
var resp = {
composeExtension: {
type: 'result',
attachmentLayout: 'list',
attachments: attachments,
}
};
src.http_res.send(resp);
if (cb) {
cb();
}
};
bot.replyInThread = function(src, resp, cb) {
// can't clone theis, not needed for this type of messages.
delete(src.http_res);
var copy = clone(src);
// make sure this does NOT include the activity id
copy.raw_message.conversation = src.raw_message.channelData.channel;
bot.reply(copy, resp, cb);
};
bot.reply = function(src, resp, cb) {
if (src.type === 'composeExtension') {
bot.replyToComposeExtension(src, resp, cb);
}
if (typeof resp == 'string') {
resp = {
text: resp
};
}
resp.serviceUrl = src.raw_message.serviceUrl;
resp.from = src.raw_message.recipient;
resp.recipient = src.raw_message.from;
resp.to = src.user;
resp.channel = src.channel;
resp.conversation = src.raw_message.conversation;
bot.say(resp, cb);
};
bot.findConversation = function(message, cb) {
botkit.debug('CUSTOM FIND CONVO', message.user, message.channel);
for (var t = 0; t < botkit.tasks.length; t++) {
for (var c = 0; c < botkit.tasks[t].convos.length; c++) {
if (
botkit.tasks[t].convos[c].isActive() &&
botkit.tasks[t].convos[c].source_message.user == message.user &&
botkit.excludedEvents.indexOf(message.type) == -1 // this type of message should not be included
) {
botkit.debug('FOUND EXISTING CONVO!');
cb(botkit.tasks[t].convos[c]);
return;
}
}
}
cb();
};
// return info about the specific instance of this bot
// including identity information, and any other info that is relevant
bot.getInstanceInfo = function(cb) {
return new Promise(function(resolve, reject) {
var instance = {
identity: {},
team: {},
};
instance.identity.name = bot.identity.name;
instance.identity.id = bot.identity.id;
if (bot.config.team) {
instance.team.id = bot.config.team;
}
if (cb) cb(null, instance);
resolve(instance);
});
};
bot.getMessageUser = function(message, cb) {
return new Promise(function(resolve, reject) {
bot.api.getUserById(message.channel, message.user, function(err, identity) {
if (err) {
if (cb) {
cb(err);
}
return reject(err);
}
// normalize this into what botkit wants to see
var profile = {
id: message.user,
username: identity.name,
first_name: identity.givenName,
last_name: identity.surname,
full_name: identity.givenName + ' ' + identity.surname,
email: identity.email, // may be blank
gender: null, // no source for this info
timezone: null, // no source for this info
timezone_offset: null, // no source for this info
};
if (cb) {
cb(null, profile);
}
resolve(profile);
});
});
};
/* helper functions for creating attachments */
bot.createAttachment = function(type, title, subtitle, text, images, buttons, tap) {
var obj = {
content: (typeof(title) === 'object') ? title : {
title: title || null,
subtitle: subtitle || null,
text: text || null,
buttons: buttons || [],
images: images || [],
tap: tap || null,
},
contentType: 'application/vnd.microsoft.card.' + type,
title: function(v) {
this.content.title = v;
return this;
},
subtitle: function(v) {
this.content.subtitle = v;
return this;
},
text: function(v) {
this.content.text = v;
return this;
},
button: function(type, title, payload) {
if (!this.content.buttons) {
this.content.buttons = [];
}
var button_obj = (typeof(type) === 'object') ? type : {
type: type,
title: title,
payload: payload,
};
this.content.buttons.push(button_obj);
return this;
},
image: function(url, alt) {
if (!this.content.images) {
this.content.images = [];
}
var img_obj = (typeof(url) === 'object') ? type : {
url: url,
alt: alt || null
};
this.content.images.push(img_obj);
return this;
},
tap: function(type, title, payload) {
var tap_action = (typeof(type) === 'object') ? type : {
type: type,
title: title,
payload: payload,
};
this.content.tap = tap_action;
return this;
},
asString: function() {
return JSON.stringify(this, null, 2);
}
};
return obj;
};
bot.createHero = function(title, subtitle, text, buttons, images, tap) {
return bot.createAttachment('hero', title, subtitle, text, buttons, images, tap);
};
bot.createThumbnail = function(title, subtitle, text, buttons, images, tap) {
return bot.createAttachment('thumbnail', title, subtitle, text, buttons, images, tap);
};
return bot;
});
controller.createWebhookEndpoints = function() {
controller.webserver.post('/teams/receive', function(req, res) {
var message = req.body;
var options = {
serviceUrl: message.serviceUrl,
};
if (message.channelData && message.channelData.team && message.channelData.team.id) {
options.team = message.channelData.team.id;
}
var bot = controller.spawn(options);
if (message.recipient) {
bot.identity = message.recipient;
}
controller.ingest(bot, message, res);
});
};
controller.middleware.spawn.use(function(bot, next) {
if (!bot.config.serviceUrl) {
throw new Error('Cannot spawn a bot without a serviceUrl in the configuration');
}
// set up the teams api client
bot.api = TeamsAPI({
clientId: controller.config.clientId,
clientSecret: controller.config.clientSecret,
token: controller.config.token,
serviceUrl: bot.config.serviceUrl,
team: bot.config.team,
});
next();
});
controller.middleware.ingest.use(function(bot, message, res, next) {
res.status(200);
if (message.name != 'composeExtension/query') {
// send a result back immediately
res.send('');
}
message.http_res = res;
next();
});
controller.middleware.normalize.use(function(bot, message, next) {
message.user = message.raw_message.from.id;
message.channel = message.raw_message.conversation.id;
next();
});
controller.middleware.categorize.use(function(bot, message, next) {
if (message.type === 'invoke' && message.name === 'composeExtension/query') {
message.type = 'composeExtension';
// teams only supports a single parameter, it either exists or doesn't!
message.text = message.value.parameters[0].value;
}
next();
});
controller.middleware.categorize.use(function(bot, message, next) {
if (message.type == 'conversationUpdate') {
if (message.raw_message.membersAdded) {
// replies to these end up in the right place
for (var m = 0; m < message.raw_message.membersAdded.length; m++) {
// clone the message
// and copy this member into the from list
delete(message.http_res); // <-- that can't be cloned safely
var copy = clone(message);
copy.from = message.raw_message.membersAdded[m];
copy.user = copy.from.id;
if (copy.user == message.raw_message.recipient.id) {
copy.type = 'bot_channel_join';
} else {
copy.type = 'user_channel_join';
}
// restart the categorize process for the newly cloned messages
controller.categorize(bot, copy);
}
} else if (message.raw_message.membersRemoved) {
// replies to these end up in the right place
for (var m = 0; m < message.raw_message.membersRemoved.length; m++) {
// clone the message
// and copy this member into the from list
delete(message.http_res); // <-- that can't be cloned safely
var copy = clone(message);
copy.from = message.raw_message.membersRemoved[m];
copy.user = copy.from.id;
if (copy.user == message.raw_message.recipient.id) {
copy.type = 'bot_channel_leave';
} else {
copy.type = 'user_channel_leave';
}
// restart the categorize process for the newly cloned messages
controller.categorize(bot, copy);
}
next();
} else if (message.raw_message.channelData && message.raw_message.channelData.eventType) {
// channelCreated
// channelDeleted
// channelRenamed
// teamRenamed
message.type = message.raw_message.channelData.eventType;
// replies to these end up in general
next();
}
} else {
next();
}
});
controller.middleware.categorize.use(function(bot, message, next) {
if (message.type == 'message') message.type = 'message_received';
if (!message.conversation.isGroup && message.type == 'message_received') {
message.type = 'direct_message';
} else if (message.conversation.isGroup && message.type == 'message_received') {
// start by setting this to a mention, meaning that the bot's name was _somewhere_ in the string
message.type = 'mention';
// check to see if this is a direct mention ,meaning bot was mentioned at start of string
for (var e = 0; e < message.entities.length; e++) {
var entity = message.entities[0];
if (entity.type == 'mention' && message.text) {
var pattern = new RegExp(message.recipient.id);
if (entity.mentioned.id.match(pattern)) {
var clean = new RegExp('^' + entity.text + '\\s+');
if (message.text.match(clean)) {
message.text = message.text.replace(clean, '');
message.type = 'direct_mention';
}
}
}
}
}
next();
});
// This middleware looks for Slack-style user mentions in a message
// <@USERID> and translates them into Microsoft Teams style mentions
// which look like <at>@User Name</at> and have a matching row in the
// message.entities field.
controller.middleware.send.use(function(bot, message, next) {
var matches;
var uniques = [];
// extract all the <@USERID> patterns
if (matches = message.text.match(/\<\@(.*?)\>/igm)) {
// get a set of UNIQUE mentions - since the lookup of profile data is expensive
for (var m = 0; m < matches.length; m++) {
if (uniques.indexOf(matches[m]) == -1) {
uniques.push(matches[m]);
}
}
// loop over each mention
async.each(uniques, function(match, next_match) {
var uid = match.replace(/^\<\@/, '').replace(/\>$/, '');
// use the teams API to load the latest profile information for the user
bot.api.getUserById(message.channel, uid, function(err, user_profile) {
// if user is valid, replace the Slack-style mention and append to entities list
if (user_profile) {
var pattern = new RegExp('<@' + uid + '>', 'g');
message.text = message.text.replace(pattern, '<at>@' + user_profile.name + '</at>');
if (!message.entities) {
message.entities = [];
}
message.entities.push({
type: 'mention',
mentioned: {
id: uid,
name: user_profile.name,
},
text: '<at>@' + user_profile.name + '</at>',
});
}
next_match();
});
}, function() {
// we've processed all the matches, continue
next();
});
} else {
// if there were no matches, continue
next();
}
});
controller.middleware.format.use(function(bot, message, platform_message, next) {
platform_message.type = 'message';
platform_message.recipient = message.recipient;
platform_message.from = message.from;
platform_message.text = message.text;
platform_message.textFormat = 'markdown';
platform_message.entities = message.entities;
platform_message.attachments = message.attachments || null;
platform_message.attachmentLayout = message.attachmentLayout || 'list';
platform_message.conversation = message.conversation;
next();
});
controller.startTicking();
return controller;
}
module.exports = TeamsBot;