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 | 1x
1x
1x
1x
1x
1x
1x
| 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');
function TeamsBot(configuration) {
var controller = Botkit(configuration || {});
controller.api = require(__dirname + '/TeamsAPI.js')(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',
api: controller.api,
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(src.serviceUrl, data, cb);
};
bot.openConvo = function(src, members, cb) {
var data = {
isGroup: true,
bot: src.recipient,
members: members,
channelData: src.channelData,
};
bot.api.createConveration(src.serviceUrl, data, cb);
};
bot.send = function(message, cb) {
var serviceUrl = message.serviceUrl;
var data = {
type: 'message',
recipient: message.recipient,
from: message.from,
text: message.text,
textFormat: 'markdown',
entities: message.entities,
attachments: message.attachments || null,
attachmentLayout: message.attachmentLayout || 'list',
};
bot.api.addMessageToConversation(serviceUrl, message.conversation.id, data, 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.serviceUrl, src.conversation.id, data, cb);
};
bot.replyToComposeExtension = function(src, resp, cb) {
// console.log('src: ', src);
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.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.debug('FOUND EXISTING CONVO!');
cb(botkit.tasks[t].convos[c]);
return;
}
}
}
cb();
};
return bot;
});
controller.configureIncomingWebhook = function() {
controller.webserver.post('/teams/receive', function(req, res) {
var bot = controller.spawn({});
var message = req.body;
bot.serviceUrl = message.serviceUrl;
controller.ingest(bot, message, res);
});
};
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') {
console.log('got a conversation update event', JSON.stringify(message.channelData));
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();
});
controller.startTicking();
return controller;
}
module.exports = TeamsBot;
|