UNPKG

23.6 kBJavaScriptView Raw
1"use strict";
2/**
3 * @module botbuilder
4 */
5/**
6 * Copyright (c) Microsoft Corporation. All rights reserved.
7 * Licensed under the MIT License.
8 */
9var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
10 return new (P || (P = Promise))(function (resolve, reject) {
11 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
12 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
13 function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
14 step((generator = generator.apply(thisArg, _arguments || [])).next());
15 });
16};
17Object.defineProperty(exports, "__esModule", { value: true });
18const botbuilder_core_1 = require("botbuilder-core");
19const teamsInfo_1 = require("./teamsInfo");
20class TeamsActivityHandler extends botbuilder_core_1.ActivityHandler {
21 /**
22 *
23 * @param context
24 */
25 onInvokeActivity(context) {
26 const _super = Object.create(null, {
27 onInvokeActivity: { get: () => super.onInvokeActivity }
28 });
29 return __awaiter(this, void 0, void 0, function* () {
30 let runEvents = true;
31 try {
32 if (!context.activity.name && context.activity.channelId === 'msteams') {
33 return yield this.handleTeamsCardActionInvoke(context);
34 }
35 else {
36 switch (context.activity.name) {
37 case 'fileConsent/invoke':
38 return botbuilder_core_1.ActivityHandler.createInvokeResponse(yield this.handleTeamsFileConsent(context, context.activity.value));
39 case 'actionableMessage/executeAction':
40 yield this.handleTeamsO365ConnectorCardAction(context, context.activity.value);
41 return botbuilder_core_1.ActivityHandler.createInvokeResponse();
42 case 'composeExtension/queryLink':
43 return botbuilder_core_1.ActivityHandler.createInvokeResponse(yield this.handleTeamsAppBasedLinkQuery(context, context.activity.value));
44 case 'composeExtension/query':
45 return botbuilder_core_1.ActivityHandler.createInvokeResponse(yield this.handleTeamsMessagingExtensionQuery(context, context.activity.value));
46 case 'composeExtension/selectItem':
47 return botbuilder_core_1.ActivityHandler.createInvokeResponse(yield this.handleTeamsMessagingExtensionSelectItem(context, context.activity.value));
48 case 'composeExtension/submitAction':
49 return botbuilder_core_1.ActivityHandler.createInvokeResponse(yield this.handleTeamsMessagingExtensionSubmitActionDispatch(context, context.activity.value));
50 case 'composeExtension/fetchTask':
51 return botbuilder_core_1.ActivityHandler.createInvokeResponse(yield this.handleTeamsMessagingExtensionFetchTask(context, context.activity.value));
52 case 'composeExtension/querySettingUrl':
53 return botbuilder_core_1.ActivityHandler.createInvokeResponse(yield this.handleTeamsMessagingExtensionConfigurationQuerySettingUrl(context, context.activity.value));
54 case 'composeExtension/setting':
55 yield this.handleTeamsMessagingExtensionConfigurationSetting(context, context.activity.value);
56 return botbuilder_core_1.ActivityHandler.createInvokeResponse();
57 case 'composeExtension/onCardButtonClicked':
58 yield this.handleTeamsMessagingExtensionCardButtonClicked(context, context.activity.value);
59 return botbuilder_core_1.ActivityHandler.createInvokeResponse();
60 case 'task/fetch':
61 return botbuilder_core_1.ActivityHandler.createInvokeResponse(yield this.handleTeamsTaskModuleFetch(context, context.activity.value));
62 case 'task/submit':
63 return botbuilder_core_1.ActivityHandler.createInvokeResponse(yield this.handleTeamsTaskModuleSubmit(context, context.activity.value));
64 default:
65 runEvents = false;
66 return _super.onInvokeActivity.call(this, context);
67 }
68 }
69 }
70 catch (err) {
71 if (err.message === 'NotImplemented') {
72 return { status: 501 };
73 }
74 else if (err.message === 'BadRequest') {
75 return { status: 400 };
76 }
77 throw err;
78 }
79 finally {
80 if (runEvents) {
81 this.defaultNextEvent(context)();
82 }
83 }
84 });
85 }
86 /**
87 *
88 * @param context
89 */
90 handleTeamsCardActionInvoke(context) {
91 return __awaiter(this, void 0, void 0, function* () {
92 throw new Error('NotImplemented');
93 });
94 }
95 /**
96 * Receives invoke activities with Activity name of 'fileConsent/invoke'. Handlers registered here run before
97 * `handleTeamsFileConsentAccept` and `handleTeamsFileConsentDecline`.
98 * Developers are not passed a pointer to the next `handleTeamsFileConsent` handler because the _wrapper_ around
99 * the handler will call `onDialogs` handlers after delegating to `handleTeamsFileConsentAccept` or `handleTeamsFileConsentDecline`.
100 * @param context
101 * @param fileConsentCardResponse
102 */
103 handleTeamsFileConsent(context, fileConsentCardResponse) {
104 return __awaiter(this, void 0, void 0, function* () {
105 switch (fileConsentCardResponse.action) {
106 case 'accept':
107 return yield this.handleTeamsFileConsentAccept(context, fileConsentCardResponse);
108 case 'decline':
109 return yield this.handleTeamsFileConsentDecline(context, fileConsentCardResponse);
110 default:
111 throw new Error('BadRequest');
112 }
113 });
114 }
115 /**
116 * Receives invoke activities with Activity name of 'fileConsent/invoke' with confirmation from user
117 * @remarks
118 * This type of invoke activity occur during the File Consent flow.
119 * @param context
120 * @param fileConsentCardResponse
121 */
122 handleTeamsFileConsentAccept(context, fileConsentCardResponse) {
123 return __awaiter(this, void 0, void 0, function* () {
124 throw new Error('NotImplemented');
125 });
126 }
127 /**
128 * Receives invoke activities with Activity name of 'fileConsent/invoke' with decline from user
129 * @remarks
130 * This type of invoke activity occur during the File Consent flow.
131 * @param context
132 * @param fileConsentCardResponse
133 */
134 handleTeamsFileConsentDecline(context, fileConsentCardResponse) {
135 return __awaiter(this, void 0, void 0, function* () {
136 throw new Error('NotImplemented');
137 });
138 }
139 /**
140 * Receives invoke activities with Activity name of 'actionableMessage/executeAction'
141 */
142 handleTeamsO365ConnectorCardAction(context, query) {
143 return __awaiter(this, void 0, void 0, function* () {
144 throw new Error('NotImplemented');
145 });
146 }
147 /*
148 * override default because to call teams specific events
149 */
150 onSignInInvoke(context) {
151 return __awaiter(this, void 0, void 0, function* () {
152 switch (context.activity.name) {
153 case botbuilder_core_1.verifyStateOperationName:
154 return yield this.handleTeamsSigninVerifyState(context, context.activity.value);
155 case botbuilder_core_1.tokenExchangeOperationName:
156 return yield this.handleTeamsSigninTokenExchange(context, context.activity.value);
157 }
158 });
159 }
160 /**
161 * Receives invoke activities with Activity name of 'signin/verifyState'
162 * @param context
163 * @param action
164 */
165 handleTeamsSigninVerifyState(context, query) {
166 return __awaiter(this, void 0, void 0, function* () {
167 throw new Error('NotImplemented');
168 });
169 }
170 /**
171 * Receives invoke activities with Activity name of 'signin/tokenExchange'
172 * @param context
173 * @param action
174 */
175 handleTeamsSigninTokenExchange(context, query) {
176 return __awaiter(this, void 0, void 0, function* () {
177 throw new Error('NotImplemented');
178 });
179 }
180 /**
181 * Receives invoke activities with Activity name of 'composeExtension/onCardButtonClicked'
182 * @param context
183 * @param cardData
184 */
185 handleTeamsMessagingExtensionCardButtonClicked(context, cardData) {
186 return __awaiter(this, void 0, void 0, function* () {
187 throw new Error('NotImplemented');
188 });
189 }
190 /**
191 * Receives invoke activities with Activity name of 'task/fetch'
192 * @param context
193 * @param taskModuleRequest
194 */
195 handleTeamsTaskModuleFetch(context, taskModuleRequest) {
196 return __awaiter(this, void 0, void 0, function* () {
197 throw new Error('NotImplemented');
198 });
199 }
200 /**
201 * Receives invoke activities with Activity name of 'task/submit'
202 * @param context
203 * @param taskModuleRequest
204 */
205 handleTeamsTaskModuleSubmit(context, taskModuleRequest) {
206 return __awaiter(this, void 0, void 0, function* () {
207 throw new Error('NotImplemented');
208 });
209 }
210 /**
211 * Receives invoke activities with Activity name of 'composeExtension/queryLink'
212 * @remarks
213 * Used in creating a Search-based Message Extension.
214 * @param context
215 * @param query
216 */
217 handleTeamsAppBasedLinkQuery(context, query) {
218 return __awaiter(this, void 0, void 0, function* () {
219 throw new Error('NotImplemented');
220 });
221 }
222 /**
223 * Receives invoke activities with the name 'composeExtension/query'.
224 * @remarks
225 * Used in creating a Search-based Message Extension.
226 * @param context
227 * @param action
228 */
229 handleTeamsMessagingExtensionQuery(context, query) {
230 return __awaiter(this, void 0, void 0, function* () {
231 throw new Error('NotImplemented');
232 });
233 }
234 /**
235 * Receives invoke activities with the name 'composeExtension/selectItem'.
236 * @remarks
237 * Used in creating a Search-based Message Extension.
238 * @param context
239 * @param action
240 */
241 handleTeamsMessagingExtensionSelectItem(context, query) {
242 return __awaiter(this, void 0, void 0, function* () {
243 throw new Error('NotImplemented');
244 });
245 }
246 /**
247 * Receives invoke activities with the name 'composeExtension/submitAction' and dispatches to botMessagePreview-flows as applicable.
248 * @remarks
249 * A handler registered through this method does not dispatch to the next handler (either `handleTeamsMessagingExtensionSubmitAction`, `handleTeamsMessagingExtensionBotMessagePreviewEdit`, or `handleTeamsMessagingExtensionBotMessagePreviewSend`).
250 * This method exists for developers to optionally add more logic before the TeamsActivityHandler routes the activity to one of the
251 * previously mentioned handlers.
252 * @param context
253 * @param action
254 */
255 handleTeamsMessagingExtensionSubmitActionDispatch(context, action) {
256 return __awaiter(this, void 0, void 0, function* () {
257 if (action.botMessagePreviewAction) {
258 switch (action.botMessagePreviewAction) {
259 case 'edit':
260 return yield this.handleTeamsMessagingExtensionBotMessagePreviewEdit(context, action);
261 case 'send':
262 return yield this.handleTeamsMessagingExtensionBotMessagePreviewSend(context, action);
263 default:
264 throw new Error('BadRequest');
265 }
266 }
267 else {
268 return yield this.handleTeamsMessagingExtensionSubmitAction(context, action);
269 }
270 });
271 }
272 /**
273 * Receives invoke activities with the name 'composeExtension/submitAction'.
274 * @remarks
275 * This invoke activity is received when a user
276 * @param context
277 * @param action
278 */
279 handleTeamsMessagingExtensionSubmitAction(context, action) {
280 return __awaiter(this, void 0, void 0, function* () {
281 throw new Error('NotImplemented');
282 });
283 }
284 /**
285 * Receives invoke activities with the name 'composeExtension/submitAction' with the 'botMessagePreview' property present on activity.value.
286 * The value for 'botMessagePreview' is 'edit'.
287 * @remarks
288 * This invoke activity is received when a user
289 * @param context
290 * @param action
291 */
292 handleTeamsMessagingExtensionBotMessagePreviewEdit(context, action) {
293 return __awaiter(this, void 0, void 0, function* () {
294 throw new Error('NotImplemented');
295 });
296 }
297 /**
298 * Receives invoke activities with the name 'composeExtension/submitAction' with the 'botMessagePreview' property present on activity.value.
299 * The value for 'botMessagePreview' is 'send'.
300 * @remarks
301 * This invoke activity is received when a user
302 * @param context
303 * @param action
304 */
305 handleTeamsMessagingExtensionBotMessagePreviewSend(context, action) {
306 return __awaiter(this, void 0, void 0, function* () {
307 throw new Error('NotImplemented');
308 });
309 }
310 /**
311 * Receives invoke activities with the name 'composeExtension/fetchTask'
312 * @param context
313 * @param action
314 */
315 handleTeamsMessagingExtensionFetchTask(context, action) {
316 return __awaiter(this, void 0, void 0, function* () {
317 throw new Error('NotImplemented');
318 });
319 }
320 /**
321 * Receives invoke activities with the name 'composeExtension/querySettingUrl'
322 * @param context
323 * @param query
324 */
325 handleTeamsMessagingExtensionConfigurationQuerySettingUrl(context, query) {
326 return __awaiter(this, void 0, void 0, function* () {
327 throw new Error('NotImplemented');
328 });
329 }
330 /**
331 * Receives invoke activities with the name 'composeExtension/setting'
332 * @param context
333 * @param query
334 */
335 handleTeamsMessagingExtensionConfigurationSetting(context, settings) {
336 throw new Error('NotImplemented');
337 }
338 /**
339 * Override this method to change the dispatching of ConversationUpdate activities.
340 * @remarks
341 *
342 * @param context
343 */
344 dispatchConversationUpdateActivity(context) {
345 const _super = Object.create(null, {
346 dispatchConversationUpdateActivity: { get: () => super.dispatchConversationUpdateActivity }
347 });
348 return __awaiter(this, void 0, void 0, function* () {
349 yield this.handle(context, 'ConversationUpdate', () => __awaiter(this, void 0, void 0, function* () {
350 if (context.activity.channelId == "msteams") {
351 const channelData = context.activity.channelData;
352 if (context.activity.membersAdded && context.activity.membersAdded.length > 0) {
353 return yield this.onTeamsMembersAdded(context);
354 }
355 if (context.activity.membersRemoved && context.activity.membersRemoved.length > 0) {
356 return yield this.onTeamsMembersRemoved(context);
357 }
358 if (!channelData || !channelData.eventType) {
359 return yield _super.dispatchConversationUpdateActivity.call(this, context);
360 }
361 switch (channelData.eventType) {
362 case 'channelCreated':
363 return yield this.onTeamsChannelCreated(context);
364 case 'channelDeleted':
365 return yield this.onTeamsChannelDeleted(context);
366 case 'channelRenamed':
367 return yield this.onTeamsChannelRenamed(context);
368 case 'teamRenamed':
369 return yield this.onTeamsTeamRenamed(context);
370 default:
371 return yield _super.dispatchConversationUpdateActivity.call(this, context);
372 }
373 }
374 else {
375 return yield _super.dispatchConversationUpdateActivity.call(this, context);
376 }
377 }));
378 });
379 }
380 /**
381 * Called in `dispatchConversationUpdateActivity()` to trigger the `'TeamsMembersAdded'` handlers.
382 * @remarks
383 * If no handlers are registered for the `'TeamsMembersAdded'` event, the `'MembersAdded'` handlers will run instead.
384 * @param context
385 */
386 onTeamsMembersAdded(context) {
387 return __awaiter(this, void 0, void 0, function* () {
388 if ('TeamsMembersAdded' in this.handlers && this.handlers['TeamsMembersAdded'].length > 0) {
389 for (let i = 0; i < context.activity.membersAdded.length; i++) {
390 const channelAccount = context.activity.membersAdded[i];
391 // check whether we have a TeamChannelAccount, or the member is the bot
392 if ('givenName' in channelAccount ||
393 'surname' in channelAccount ||
394 'email' in channelAccount ||
395 'userPrincipalName' in channelAccount ||
396 context.activity.recipient.id === channelAccount.id) {
397 // we must have a TeamsChannelAccount, or a bot so skip to the next one
398 continue;
399 }
400 try {
401 context.activity.membersAdded[i] = yield teamsInfo_1.TeamsInfo.getMember(context, channelAccount.id);
402 }
403 catch (err) {
404 const errCode = err.body && err.body.error && err.body.error.code;
405 if (errCode === 'ConversationNotFound') {
406 // unable to find the member added in ConversationUpdate Activity in the response from the getMember call
407 const teamsChannelAccount = {
408 id: channelAccount.id,
409 name: channelAccount.name,
410 aadObjectId: channelAccount.aadObjectId,
411 role: channelAccount.role,
412 };
413 context.activity.membersAdded[i] = teamsChannelAccount;
414 }
415 else {
416 throw err;
417 }
418 }
419 }
420 yield this.handle(context, 'TeamsMembersAdded', this.defaultNextEvent(context));
421 }
422 else {
423 yield this.handle(context, 'MembersAdded', this.defaultNextEvent(context));
424 }
425 });
426 }
427 /**
428 * Called in `dispatchConversationUpdateActivity()` to trigger the `'TeamsMembersRemoved'` handlers.
429 * @remarks
430 * If no handlers are registered for the `'TeamsMembersRemoved'` event, the `'MembersRemoved'` handlers will run instead.
431 * @param context
432 */
433 onTeamsMembersRemoved(context) {
434 return __awaiter(this, void 0, void 0, function* () {
435 if ('TeamsMembersRemoved' in this.handlers && this.handlers['TeamsMembersRemoved'].length > 0) {
436 yield this.handle(context, 'TeamsMembersRemoved', this.defaultNextEvent(context));
437 }
438 else {
439 yield this.handle(context, 'MembersRemoved', this.defaultNextEvent(context));
440 }
441 });
442 }
443 /**
444 *
445 * @param context
446 */
447 onTeamsChannelCreated(context) {
448 return __awaiter(this, void 0, void 0, function* () {
449 yield this.handle(context, 'TeamsChannelCreated', this.defaultNextEvent(context));
450 });
451 }
452 /**
453 *
454 * @param context
455 */
456 onTeamsChannelDeleted(context) {
457 return __awaiter(this, void 0, void 0, function* () {
458 yield this.handle(context, 'TeamsChannelDeleted', this.defaultNextEvent(context));
459 });
460 }
461 /**
462 *
463 * @param context
464 */
465 onTeamsChannelRenamed(context) {
466 return __awaiter(this, void 0, void 0, function* () {
467 yield this.handle(context, 'TeamsChannelRenamed', this.defaultNextEvent(context));
468 });
469 }
470 /**
471 *
472 * @param context
473 */
474 onTeamsTeamRenamed(context) {
475 return __awaiter(this, void 0, void 0, function* () {
476 yield this.handle(context, 'TeamsTeamRenamed', this.defaultNextEvent(context));
477 });
478 }
479 /**
480 *
481 * @param handler
482 */
483 onTeamsMembersAddedEvent(handler) {
484 return this.on('TeamsMembersAdded', (context, next) => __awaiter(this, void 0, void 0, function* () {
485 const teamsChannelData = context.activity.channelData;
486 yield handler(context.activity.membersAdded, teamsChannelData.team, context, next);
487 }));
488 }
489 /**
490 *
491 * @param handler
492 */
493 onTeamsMembersRemovedEvent(handler) {
494 return this.on('TeamsMembersRemoved', (context, next) => __awaiter(this, void 0, void 0, function* () {
495 const teamsChannelData = context.activity.channelData;
496 yield handler(context.activity.membersRemoved, teamsChannelData.team, context, next);
497 }));
498 }
499 /**
500 *
501 * @param handler
502 */
503 onTeamsChannelCreatedEvent(handler) {
504 return this.on('TeamsChannelCreated', (context, next) => __awaiter(this, void 0, void 0, function* () {
505 const teamsChannelData = context.activity.channelData;
506 yield handler(teamsChannelData.channel, teamsChannelData.team, context, next);
507 }));
508 }
509 /**
510 *
511 * @param handler
512 */
513 onTeamsChannelDeletedEvent(handler) {
514 return this.on('TeamsChannelDeleted', (context, next) => __awaiter(this, void 0, void 0, function* () {
515 const teamsChannelData = context.activity.channelData;
516 yield handler(teamsChannelData.channel, teamsChannelData.team, context, next);
517 }));
518 }
519 /**
520 *
521 * @param handler
522 */
523 onTeamsChannelRenamedEvent(handler) {
524 return this.on('TeamsChannelRenamed', (context, next) => __awaiter(this, void 0, void 0, function* () {
525 const teamsChannelData = context.activity.channelData;
526 yield handler(teamsChannelData.channel, teamsChannelData.team, context, next);
527 }));
528 }
529 /**
530 *
531 * @param handler
532 */
533 onTeamsTeamRenamedEvent(handler) {
534 return this.on('TeamsTeamRenamed', (context, next) => __awaiter(this, void 0, void 0, function* () {
535 const teamsChannelData = context.activity.channelData;
536 yield handler(teamsChannelData.team, context, next);
537 }));
538 }
539}
540exports.TeamsActivityHandler = TeamsActivityHandler;
541//# sourceMappingURL=teamsActivityHandler.js.map
\No newline at end of file