-
Notifications
You must be signed in to change notification settings - Fork 281
/
teamsActivityHandler.ts
1368 lines (1255 loc) · 55.3 KB
/
teamsActivityHandler.ts
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
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @module botbuilder
*/
/**
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/
import {
ActivityHandler,
AppBasedLinkQuery,
ChannelInfo,
Channels,
ConfigResponse,
FileConsentCardResponse,
InvokeResponse,
MeetingEndEventDetails,
MeetingParticipantsEventDetails,
MeetingStartEventDetails,
MessagingExtensionAction,
MessagingExtensionActionResponse,
MessagingExtensionQuery,
MessagingExtensionResponse,
O365ConnectorCardActionQuery,
SigninStateVerificationQuery,
TabRequest,
TabResponse,
TabSubmit,
TaskModuleRequest,
TaskModuleResponse,
TeamInfo,
TeamsChannelAccount,
TeamsChannelData,
tokenExchangeOperationName,
TurnContext,
verifyStateOperationName,
} from 'botbuilder-core';
import { ReadReceiptInfo } from 'botframework-connector';
import { TeamsInfo } from './teamsInfo';
import * as z from 'zod';
const TeamsMeetingStartT = z
.object({
Id: z.string(),
JoinUrl: z.string(),
MeetingType: z.string(),
Title: z.string(),
StartTime: z.string(),
})
.nonstrict();
const TeamsMeetingEndT = z
.object({
Id: z.string(),
JoinUrl: z.string(),
MeetingType: z.string(),
Title: z.string(),
EndTime: z.string(),
})
.nonstrict();
/**
* Adds support for Microsoft Teams specific events and interactions.
*
* @remarks
* Developers may handle Message Update, Message Delete, and Conversation Update activities sent from Microsoft Teams via two methods:
* 1. Overriding methods starting with `on..` and *not* ending in `..Event()` (e.g. `onTeamsMembersAdded()`), or instead
* 2. Passing callbacks to methods starting with `on..` *and* ending in `...Event()` (e.g. `onTeamsMembersAddedEvent()`),
* to stay in line with older {@see ActivityHandler} implementation.
*
* Developers should use either #1 or #2, above for all Message Update, Message Delete, and Conversation Update activities and not *both* #1 and #2 for the same activity. Meaning,
* developers should override `onTeamsMembersAdded()` and not use both `onTeamsMembersAdded()` and `onTeamsMembersAddedEvent()`.
*
* Developers wanting to handle Invoke activities *must* override methods starting with `handle...()` (e.g. `handleTeamsTaskModuleFetch()`).
*/
export class TeamsActivityHandler extends ActivityHandler {
/**
* Invoked when an invoke activity is received from the connector.
* Invoke activities can be used to communicate many different things.
*
* @param context A context object for this turn.
* @returns An Invoke Response for the activity.
*/
protected async onInvokeActivity(context: TurnContext): Promise<InvokeResponse> {
let runEvents = true;
try {
if (!context.activity.name && context.activity.channelId === 'msteams') {
return await this.handleTeamsCardActionInvoke(context);
} else {
switch (context.activity.name) {
case 'config/fetch':
return ActivityHandler.createInvokeResponse(
await this.handleTeamsConfigFetch(context, context.activity.value)
);
case 'config/submit':
return ActivityHandler.createInvokeResponse(
await this.handleTeamsConfigSubmit(context, context.activity.value)
);
case 'fileConsent/invoke':
return ActivityHandler.createInvokeResponse(
await this.handleTeamsFileConsent(context, context.activity.value)
);
case 'actionableMessage/executeAction':
await this.handleTeamsO365ConnectorCardAction(context, context.activity.value);
return ActivityHandler.createInvokeResponse();
case 'composeExtension/queryLink':
return ActivityHandler.createInvokeResponse(
await this.handleTeamsAppBasedLinkQuery(context, context.activity.value)
);
case 'composeExtension/anonymousQueryLink':
return ActivityHandler.createInvokeResponse(
await this.handleTeamsAnonymousAppBasedLinkQuery(context, context.activity.value)
);
case 'composeExtension/query':
return ActivityHandler.createInvokeResponse(
await this.handleTeamsMessagingExtensionQuery(context, context.activity.value)
);
case 'composeExtension/selectItem':
return ActivityHandler.createInvokeResponse(
await this.handleTeamsMessagingExtensionSelectItem(context, context.activity.value)
);
case 'composeExtension/submitAction':
return ActivityHandler.createInvokeResponse(
await this.handleTeamsMessagingExtensionSubmitActionDispatch(
context,
context.activity.value
)
);
case 'composeExtension/fetchTask':
return ActivityHandler.createInvokeResponse(
await this.handleTeamsMessagingExtensionFetchTask(context, context.activity.value)
);
case 'composeExtension/querySettingUrl':
return ActivityHandler.createInvokeResponse(
await this.handleTeamsMessagingExtensionConfigurationQuerySettingUrl(
context,
context.activity.value
)
);
case 'composeExtension/setting':
await this.handleTeamsMessagingExtensionConfigurationSetting(context, context.activity.value);
return ActivityHandler.createInvokeResponse();
case 'composeExtension/onCardButtonClicked':
await this.handleTeamsMessagingExtensionCardButtonClicked(context, context.activity.value);
return ActivityHandler.createInvokeResponse();
case 'task/fetch':
return ActivityHandler.createInvokeResponse(
await this.handleTeamsTaskModuleFetch(context, context.activity.value)
);
case 'task/submit':
return ActivityHandler.createInvokeResponse(
await this.handleTeamsTaskModuleSubmit(context, context.activity.value)
);
case 'tab/fetch':
return ActivityHandler.createInvokeResponse(
await this.handleTeamsTabFetch(context, context.activity.value)
);
case 'tab/submit':
return ActivityHandler.createInvokeResponse(
await this.handleTeamsTabSubmit(context, context.activity.value)
);
default:
runEvents = false;
return super.onInvokeActivity(context);
}
}
} catch (err: any) {
if (err.message === 'NotImplemented') {
return { status: 501 };
} else if (err.message === 'BadRequest') {
return { status: 400 };
}
throw err;
} finally {
if (runEvents) {
this.defaultNextEvent(context)();
}
}
}
/**
* Handles a Teams Card Action Invoke activity.
*
* @param _context A context object for this turn.
* @returns An Invoke Response for the activity.
*/
protected async handleTeamsCardActionInvoke(_context: TurnContext): Promise<InvokeResponse> {
throw new Error('NotImplemented');
}
/**
* Handles a config/fetch invoke activity.
*
* @param _context A context object for this turn.
* @param _configData The object representing the configuration.
* @returns A Config Response for the activity.
*/
protected async handleTeamsConfigFetch(_context: TurnContext, _configData: any): Promise<ConfigResponse> {
throw new Error('NotImplemented');
}
/**
* Handles a config/submit invoke activity.
*
* @param _context A context object for this turn.
* @param _configData The object representing the configuration.
* @returns A Config Response for the activity.
*/
protected async handleTeamsConfigSubmit(_context: TurnContext, _configData: any): Promise<ConfigResponse> {
throw new Error('NotImplemented');
}
/**
* Receives invoke activities with Activity name of 'fileConsent/invoke'. Handlers registered here run before
* `handleTeamsFileConsentAccept` and `handleTeamsFileConsentDecline`.
* Developers are not passed a pointer to the next `handleTeamsFileConsent` handler because the _wrapper_ around
* the handler will call `onDialogs` handlers after delegating to `handleTeamsFileConsentAccept` or `handleTeamsFileConsentDecline`.
*
* @param context A context object for this turn.
* @param fileConsentCardResponse Represents the value of the invoke activity sent when the user acts on a file consent card.
* @returns A promise that represents the work queued.
*/
protected async handleTeamsFileConsent(
context: TurnContext,
fileConsentCardResponse: FileConsentCardResponse
): Promise<void> {
switch (fileConsentCardResponse.action) {
case 'accept':
return await this.handleTeamsFileConsentAccept(context, fileConsentCardResponse);
case 'decline':
return await this.handleTeamsFileConsentDecline(context, fileConsentCardResponse);
default:
throw new Error('BadRequest');
}
}
/**
* Receives invoke activities with Activity name of 'fileConsent/invoke' with confirmation from user
*
* @remarks
* This type of invoke activity occur during the File Consent flow.
* @param _context A context object for this turn.
* @param _fileConsentCardResponse Represents the value of the invoke activity sent when the user acts on a file consent card.
* @returns A promise that represents the work queued.
*/
protected async handleTeamsFileConsentAccept(
_context: TurnContext,
_fileConsentCardResponse: FileConsentCardResponse
): Promise<void> {
throw new Error('NotImplemented');
}
/**
* Receives invoke activities with Activity name of 'fileConsent/invoke' with decline from user
*
* @remarks
* This type of invoke activity occur during the File Consent flow.
* @param _context A context object for this turn.
* @param _fileConsentCardResponse Represents the value of the invoke activity sent when the user acts on a file consent card.
* @returns A promise that represents the work queued.
*/
protected async handleTeamsFileConsentDecline(
_context: TurnContext,
_fileConsentCardResponse: FileConsentCardResponse
): Promise<void> {
throw new Error('NotImplemented');
}
/**
* Receives invoke activities with Activity name of 'actionableMessage/executeAction'.
*
* @param _context A context object for this turn.
* @param _query The O365 connector card HttpPOST invoke query.
* @returns A promise that represents the work queued.
*/
protected async handleTeamsO365ConnectorCardAction(
_context: TurnContext,
_query: O365ConnectorCardActionQuery
): Promise<void> {
throw new Error('NotImplemented');
}
/**
* Invoked when a signIn invoke activity is received from the connector.
*
* @param context A context object for this turn.
* @returns A promise that represents the work queued.
*/
protected async onSignInInvoke(context: TurnContext): Promise<void> {
switch (context.activity.name) {
case verifyStateOperationName:
return await this.handleTeamsSigninVerifyState(context, context.activity.value);
case tokenExchangeOperationName:
return await this.handleTeamsSigninTokenExchange(context, context.activity.value);
}
}
/**
* Receives invoke activities with Activity name of 'signin/verifyState'.
*
* @param _context A context object for this turn.
* @param _query Signin state (part of signin action auth flow) verification invoke query.
* @returns A promise that represents the work queued.
*/
protected async handleTeamsSigninVerifyState(
_context: TurnContext,
_query: SigninStateVerificationQuery
): Promise<void> {
throw new Error('NotImplemented');
}
/**
* Receives invoke activities with Activity name of 'signin/tokenExchange'
*
* @param _context A context object for this turn.
* @param _query Signin state (part of signin action auth flow) verification invoke query
* @returns A promise that represents the work queued.
*/
protected async handleTeamsSigninTokenExchange(
_context: TurnContext,
_query: SigninStateVerificationQuery
): Promise<void> {
throw new Error('NotImplemented');
}
/**
* Receives invoke activities with Activity name of 'composeExtension/onCardButtonClicked'
*
* @param _context A context object for this turn.
* @param _cardData Object representing the card data.
* @returns A promise that represents the work queued.
*/
protected async handleTeamsMessagingExtensionCardButtonClicked(
_context: TurnContext,
_cardData: any
): Promise<void> {
throw new Error('NotImplemented');
}
/**
* Receives invoke activities with Activity name of 'task/fetch'
*
* @param _context A context object for this turn.
* @param _taskModuleRequest The task module invoke request value payload.
* @returns A Task Module Response for the request.
*/
protected async handleTeamsTaskModuleFetch(
_context: TurnContext,
_taskModuleRequest: TaskModuleRequest
): Promise<TaskModuleResponse> {
throw new Error('NotImplemented');
}
/**
* Receives invoke activities with Activity name of 'task/submit'
*
* @param _context A context object for this turn.
* @param _taskModuleRequest The task module invoke request value payload.
* @returns A Task Module Response for the request.
*/
protected async handleTeamsTaskModuleSubmit(
_context: TurnContext,
_taskModuleRequest: TaskModuleRequest
): Promise<TaskModuleResponse> {
throw new Error('NotImplemented');
}
/**
* Receives invoke activities with Activity name of 'tab/fetch'
*
* @param _context A context object for this turn.
* @param _tabRequest The tab invoke request value payload.
* @returns A Tab Response for the request.
*/
protected async handleTeamsTabFetch(_context: TurnContext, _tabRequest: TabRequest): Promise<TabResponse> {
throw new Error('NotImplemented');
}
/**
* Receives invoke activities with Activity name of 'tab/submit'
*
* @param _context A context object for this turn.
* @param _tabSubmit The tab submit invoke request value payload.
* @returns A Tab Response for the request.
*/
protected async handleTeamsTabSubmit(_context: TurnContext, _tabSubmit: TabSubmit): Promise<TabResponse> {
throw new Error('NotImplemented');
}
/**
* Receives invoke activities with Activity name of 'composeExtension/queryLink'
*
* @remarks
* Used in creating a Search-based Message Extension.
* @param _context A context object for this turn.
* @param _query he invoke request body type for app-based link query.
* @returns The Messaging Extension Response for the query.
*/
protected async handleTeamsAppBasedLinkQuery(
_context: TurnContext,
_query: AppBasedLinkQuery
): Promise<MessagingExtensionResponse> {
throw new Error('NotImplemented');
}
/**
* Receives invoke activities with Activity name of 'composeExtension/anonymousQueryLink'
*
* @remarks
* Used in creating a Search-based Message Extension.
* @param _context A context object for this turn.
* @param _query he invoke request body type for app-based link query.
* @returns The Messaging Extension Response for the query.
*/
protected async handleTeamsAnonymousAppBasedLinkQuery(
_context: TurnContext,
_query: AppBasedLinkQuery
): Promise<MessagingExtensionResponse> {
throw new Error('NotImplemented');
}
/**
* Receives invoke activities with the name 'composeExtension/query'.
*
* @remarks
* Used in creating a Search-based Message Extension.
* @param _context A context object for this turn.
* @param _query The query for the search command.
* @returns The Messaging Extension Response for the query.
*/
protected async handleTeamsMessagingExtensionQuery(
_context: TurnContext,
_query: MessagingExtensionQuery
): Promise<MessagingExtensionResponse> {
throw new Error('NotImplemented');
}
/**
* Receives invoke activities with the name 'composeExtension/selectItem'.
*
* @remarks
* Used in creating a Search-based Message Extension.
* @param _context A context object for this turn.
* @param _query The object representing the query.
* @returns The Messaging Extension Response for the query.
*/
protected async handleTeamsMessagingExtensionSelectItem(
_context: TurnContext,
_query: any
): Promise<MessagingExtensionResponse> {
throw new Error('NotImplemented');
}
/**
* Receives invoke activities with the name 'composeExtension/submitAction' and dispatches to botMessagePreview-flows as applicable.
*
* @remarks
* A handler registered through this method does not dispatch to the next handler (either `handleTeamsMessagingExtensionSubmitAction`, `handleTeamsMessagingExtensionBotMessagePreviewEdit`, or `handleTeamsMessagingExtensionBotMessagePreviewSend`).
* This method exists for developers to optionally add more logic before the TeamsActivityHandler routes the activity to one of the
* previously mentioned handlers.
* @param context A context object for this turn.
* @param action The messaging extension action.
* @returns The Messaging Extension Action Response for the action.
*/
protected async handleTeamsMessagingExtensionSubmitActionDispatch(
context: TurnContext,
action: MessagingExtensionAction
): Promise<MessagingExtensionActionResponse> {
if (action.botMessagePreviewAction) {
switch (action.botMessagePreviewAction) {
case 'edit':
return await this.handleTeamsMessagingExtensionBotMessagePreviewEdit(context, action);
case 'send':
return await this.handleTeamsMessagingExtensionBotMessagePreviewSend(context, action);
default:
throw new Error('BadRequest');
}
} else {
return await this.handleTeamsMessagingExtensionSubmitAction(context, action);
}
}
/**
* Receives invoke activities with the name 'composeExtension/submitAction'.
*
* @param _context A context object for this turn.
* @param _action The messaging extension action.
* @returns The Messaging Extension Action Response for the action.
*/
protected async handleTeamsMessagingExtensionSubmitAction(
_context: TurnContext,
_action: MessagingExtensionAction
): Promise<MessagingExtensionActionResponse> {
throw new Error('NotImplemented');
}
/**
* Receives invoke activities with the name 'composeExtension/submitAction' with the 'botMessagePreview' property present on activity.value.
* The value for 'botMessagePreview' is 'edit'.
*
* @param _context A context object for this turn.
* @param _action The messaging extension action.
* @returns The Messaging Extension Action Response for the action.
*/
protected async handleTeamsMessagingExtensionBotMessagePreviewEdit(
_context: TurnContext,
_action: MessagingExtensionAction
): Promise<MessagingExtensionActionResponse> {
throw new Error('NotImplemented');
}
/**
* Receives invoke activities with the name 'composeExtension/submitAction' with the 'botMessagePreview' property present on activity.value.
* The value for 'botMessagePreview' is 'send'.
*
* @param _context A context object for this turn.
* @param _action The messaging extension action.
* @returns The Messaging Extension Action Response for the action.
*/
protected async handleTeamsMessagingExtensionBotMessagePreviewSend(
_context: TurnContext,
_action: MessagingExtensionAction
): Promise<MessagingExtensionActionResponse> {
throw new Error('NotImplemented');
}
/**
* Receives invoke activities with the name 'composeExtension/fetchTask'
*
* @param _context A context object for this turn.
* @param _action The messaging extension action.
* @returns The Messaging Extension Action Response for the action.
*/
protected async handleTeamsMessagingExtensionFetchTask(
_context: TurnContext,
_action: MessagingExtensionAction
): Promise<MessagingExtensionActionResponse> {
throw new Error('NotImplemented');
}
/**
* Receives invoke activities with the name 'composeExtension/querySettingUrl'
*
* @param _context A context object for this turn.
* @param _query The Messaging extension query.
* @returns The Messaging Extension Action Response for the query.
*/
protected async handleTeamsMessagingExtensionConfigurationQuerySettingUrl(
_context: TurnContext,
_query: MessagingExtensionQuery
): Promise<MessagingExtensionResponse> {
throw new Error('NotImplemented');
}
/**
* Receives invoke activities with the name 'composeExtension/setting'
*
* @param _context A context object for this turn.
* @param _settings Object representing the configuration settings.
*/
protected handleTeamsMessagingExtensionConfigurationSetting(_context: TurnContext, _settings: any): Promise<void> {
throw new Error('NotImplemented');
}
/**
* Override this method to change the dispatching of ConversationUpdate activities.
*
* @param context A context object for this turn.
* @returns A promise that represents the work queued.
*/
protected async dispatchConversationUpdateActivity(context: TurnContext): Promise<void> {
if (context.activity.channelId == 'msteams') {
const channelData = context.activity.channelData as TeamsChannelData;
if (context.activity.membersAdded && context.activity.membersAdded.length > 0) {
return await this.onTeamsMembersAdded(context);
}
if (context.activity.membersRemoved && context.activity.membersRemoved.length > 0) {
return await this.onTeamsMembersRemoved(context);
}
if (!channelData || !channelData.eventType) {
return await super.dispatchConversationUpdateActivity(context);
}
switch (channelData.eventType) {
case 'channelCreated':
return await this.onTeamsChannelCreated(context);
case 'channelDeleted':
return await this.onTeamsChannelDeleted(context);
case 'channelRenamed':
return await this.onTeamsChannelRenamed(context);
case 'teamArchived':
return await this.onTeamsTeamArchived(context);
case 'teamDeleted':
return await this.onTeamsTeamDeleted(context);
case 'teamHardDeleted':
return await this.onTeamsTeamHardDeleted(context);
case 'channelRestored':
return await this.onTeamsChannelRestored(context);
case 'teamRenamed':
return await this.onTeamsTeamRenamed(context);
case 'teamRestored':
return await this.onTeamsTeamRestored(context);
case 'teamUnarchived':
return await this.onTeamsTeamUnarchived(context);
default:
return await super.dispatchConversationUpdateActivity(context);
}
} else {
return await super.dispatchConversationUpdateActivity(context);
}
}
/**
* Override this method to change the dispatching of MessageUpdate activities.
*
* @param context A context object for this turn.
* @returns A promise that represents the work queued.
*/
protected async dispatchMessageUpdateActivity(context: TurnContext): Promise<void> {
if (context.activity.channelId == 'msteams') {
const channelData = context.activity.channelData as TeamsChannelData;
switch (channelData.eventType) {
case 'undeleteMessage':
return await this.onTeamsMessageUndelete(context);
case 'editMessage':
return await this.onTeamsMessageEdit(context);
default:
return super.dispatchMessageUpdateActivity(context);
}
} else {
return await super.dispatchMessageUpdateActivity(context);
}
}
/**
* Override this method to change the dispatching of MessageDelete activities.
*
* @param context A context object for this turn.
* @returns A promise that represents the work queued.
*/
protected async dispatchMessageDeleteActivity(context: TurnContext): Promise<void> {
if (context.activity.channelId == 'msteams') {
const channelData = context.activity.channelData as TeamsChannelData;
switch (channelData.eventType) {
case 'softDeleteMessage':
return await this.onTeamsMessageSoftDelete(context);
default:
return super.dispatchMessageDeleteActivity(context);
}
} else {
return await super.dispatchMessageDeleteActivity(context);
}
}
/**
* Called in `dispatchMessageUpdateActivity()` to trigger the `'TeamsMessageUndelete'` handlers.
* Override this in a derived class to provide logic for when a deleted message in a conversation is undeleted.
* For example, when the user decides to "undo" a deleted message.
*
* @param context A context object for this turn.
* @returns A promise that represents the work queued.
*/
protected async onTeamsMessageUndelete(context: TurnContext): Promise<void> {
await this.handle(context, 'TeamsMessageUndelete', this.defaultNextEvent(context));
}
/**
* Called in `dispatchMessageUpdateActivity()` to trigger the `'TeamsMessageEdit'` handlers.
* Override this in a derived class to provide logic for when a message in a conversation is edited.
*
* @param context A context object for this turn.
* @returns A promise that represents the work queued.
*/
protected async onTeamsMessageEdit(context: TurnContext): Promise<void> {
await this.handle(context, 'TeamsMessageEdit', this.defaultNextEvent(context));
}
/**
* Called in `dispatchMessageDeleteActivity()` to trigger the `'TeamsMessageEdit'` handlers.
* Override this in a derived class to provide logic for when a message in a conversation is soft deleted.
* This means that the message as the option of being undeleted.
*
* @param context A context object for this turn.
* @returns A promise that represents the work queued.
*/
protected async onTeamsMessageSoftDelete(context: TurnContext): Promise<void> {
await this.handle(context, 'onTeamsMessageSoftDelete', this.defaultNextEvent(context));
}
/**
* Called in `dispatchConversationUpdateActivity()` to trigger the `'TeamsMembersAdded'` handlers.
* Override this in a derived class to provide logic for when members other than the bot
* join the channel, such as your bot's welcome logic.
*
* @remarks
* If no handlers are registered for the `'TeamsMembersAdded'` event, the `'MembersAdded'` handlers will run instead.
* @param context A context object for this turn.
* @returns A promise that represents the work queued.
*/
protected async onTeamsMembersAdded(context: TurnContext): Promise<void> {
if ('TeamsMembersAdded' in this.handlers && this.handlers['TeamsMembersAdded'].length > 0) {
if (!context.activity || !context.activity.membersAdded) {
throw new Error('OnTeamsMemberAdded: context.activity is undefined');
}
for (let i = 0; i < context.activity.membersAdded.length; i++) {
const channelAccount = context.activity.membersAdded[i];
// check whether we have a TeamChannelAccount, or the member is the bot
if (
'givenName' in channelAccount ||
'surname' in channelAccount ||
'email' in channelAccount ||
'userPrincipalName' in channelAccount ||
context.activity.recipient.id === channelAccount.id
) {
// we must have a TeamsChannelAccount, or a bot so skip to the next one
continue;
}
try {
context.activity.membersAdded[i] = await TeamsInfo.getMember(context, channelAccount.id);
} catch (err: any) {
const errCode: string = err.body && err.body.error && err.body.error.code;
if (errCode === 'ConversationNotFound') {
// unable to find the member added in ConversationUpdate Activity in the response from the getMember call
const teamsChannelAccount: TeamsChannelAccount = {
id: channelAccount.id,
name: channelAccount.name,
aadObjectId: channelAccount.aadObjectId,
role: channelAccount.role,
};
context.activity.membersAdded[i] = teamsChannelAccount;
} else {
throw err;
}
}
}
await this.handle(context, 'TeamsMembersAdded', this.defaultNextEvent(context));
} else {
await this.handle(context, 'MembersAdded', this.defaultNextEvent(context));
}
}
/**
* Called in `dispatchConversationUpdateActivity()` to trigger the `'TeamsMembersRemoved'` handlers.
* Override this in a derived class to provide logic for when members other than the bot
* leave the channel, such as your bot's good-bye logic.
*
* @remarks
* If no handlers are registered for the `'TeamsMembersRemoved'` event, the `'MembersRemoved'` handlers will run instead.
* @param context A context object for this turn.
* @returns A promise that represents the work queued.
*/
protected async onTeamsMembersRemoved(context: TurnContext): Promise<void> {
if ('TeamsMembersRemoved' in this.handlers && this.handlers['TeamsMembersRemoved'].length > 0) {
await this.handle(context, 'TeamsMembersRemoved', this.defaultNextEvent(context));
} else {
await this.handle(context, 'MembersRemoved', this.defaultNextEvent(context));
}
}
/**
* Invoked when a Channel Created event activity is received from the connector.
* Channel Created corresponds to the user creating a new channel.
* Override this in a derived class to provide logic for when a channel is created.
*
* @param context A context object for this turn.
* @returns A promise that represents the work queued.
*/
protected async onTeamsChannelCreated(context: TurnContext): Promise<void> {
await this.handle(context, 'TeamsChannelCreated', this.defaultNextEvent(context));
}
/**
* Invoked when a Channel Deleted event activity is received from the connector.
* Channel Deleted corresponds to the user deleting a channel.
* Override this in a derived class to provide logic for when a channel is deleted.
*
* @param context A context object for this turn.
* @returns A promise that represents the work queued.
*/
protected async onTeamsChannelDeleted(context: TurnContext): Promise<void> {
await this.handle(context, 'TeamsChannelDeleted', this.defaultNextEvent(context));
}
/**
* Invoked when a Channel Renamed event activity is received from the connector.
* Channel Renamed corresponds to the user renaming a new channel.
* Override this in a derived class to provide logic for when a channel is renamed.
*
* @param context A context object for this turn.
* @returns A promise that represents the work queued.
*/
protected async onTeamsChannelRenamed(context: TurnContext): Promise<void> {
await this.handle(context, 'TeamsChannelRenamed', this.defaultNextEvent(context));
}
/**
* Invoked when a Team Archived event activity is received from the connector.
* Team Archived corresponds to the user archiving a team.
* Override this in a derived class to provide logic for when a team is archived.
*
* @param context The context for this turn.
* @returns A promise that represents the work queued.
*/
protected async onTeamsTeamArchived(context: TurnContext): Promise<void> {
await this.handle(context, 'TeamsTeamArchived', this.defaultNextEvent(context));
}
/**
* Invoked when a Team Deleted event activity is received from the connector.
* Team Deleted corresponds to the user deleting a team.
* Override this in a derived class to provide logic for when a team is deleted.
*
* @param context The context for this turn.
* @returns A promise that represents the work queued.
*/
protected async onTeamsTeamDeleted(context: TurnContext): Promise<void> {
await this.handle(context, 'TeamsTeamDeleted', this.defaultNextEvent(context));
}
/**
* Invoked when a Team Hard Deleted event activity is received from the connector.
* Team Hard Deleted corresponds to the user hard-deleting a team.
* Override this in a derived class to provide logic for when a team is hard-deleted.
*
* @param context The context for this turn.
* @returns A promise that represents the work queued.
*/
protected async onTeamsTeamHardDeleted(context: TurnContext): Promise<void> {
await this.handle(context, 'TeamsTeamHardDeleted', this.defaultNextEvent(context));
}
/**
*
* Invoked when a Channel Restored event activity is received from the connector.
* Channel Restored corresponds to the user restoring a previously deleted channel.
* Override this in a derived class to provide logic for when a channel is restored.
*
* @param context The context for this turn.
* @returns A promise that represents the work queued.
*/
protected async onTeamsChannelRestored(context: TurnContext): Promise<void> {
await this.handle(context, 'TeamsChannelRestored', this.defaultNextEvent(context));
}
/**
* Invoked when a Team Renamed event activity is received from the connector.
* Team Renamed corresponds to the user renaming a team.
* Override this in a derived class to provide logic for when a team is renamed.
*
* @param context The context for this turn.
* @returns A promise that represents the work queued.
*/
protected async onTeamsTeamRenamed(context: TurnContext): Promise<void> {
await this.handle(context, 'TeamsTeamRenamed', this.defaultNextEvent(context));
}
/**
* Invoked when a Team Restored event activity is received from the connector.
* Team Restored corresponds to the user restoring a team.
* Override this in a derived class to provide logic for when a team is restored.
*
* @param context The context for this turn.
* @returns A promise that represents the work queued.
*/
protected async onTeamsTeamRestored(context: TurnContext): Promise<void> {
await this.handle(context, 'TeamsTeamRestored', this.defaultNextEvent(context));
}
/**
* Invoked when a Team Unarchived event activity is received from the connector.
* Team Unarchived corresponds to the user unarchiving a team.
* Override this in a derived class to provide logic for when a team is unarchived.
*
* @param context The context for this turn.
* @returns A promise that represents the work queued.
*/
protected async onTeamsTeamUnarchived(context: TurnContext): Promise<void> {
await this.handle(context, 'TeamsTeamUnarchived', this.defaultNextEvent(context));
}
/**
* Registers a handler for TeamsMessageUndelete events, such as for when a message in a conversation that is
* observed by the bot goes from a soft delete state to the normal state.
*
* @param handler A callback to handle the teams undelete message event.
* @returns A promise that represents the work queued.
*/
onTeamsMessageUndeleteEvent(handler: (context: TurnContext, next: () => Promise<void>) => Promise<void>): this {
return this.on('TeamsMessageUndelete', async (context, next) => {
await handler(context, next);
});
}
/**
* Registers a handler for TeamsMessageEdit events, such as for when a message in a conversation that is
* observed by the bot is edited.
*
* @param handler A callback to handle the teams edit message event.
* @returns A promise that represents the work queued.
*/
onTeamsMessageEditEvent(handler: (context: TurnContext, next: () => Promise<void>) => Promise<void>): this {
return this.on('TeamsMessageEdit', async (context, next) => {
await handler(context, next);
});
}
/**
* Registers a handler for TeamsMessageSoftDelete events, such as for when a message in a conversation that is
* observed by the bot is soft deleted. This means that the deleted message, up to a certain time period,
* can be undoed.
*
* @param handler A callback to handle the teams edit message event.
* @returns A promise that represents the work queued.
*/
onTeamsMessageSoftDeleteEvent(handler: (context: TurnContext, next: () => Promise<void>) => Promise<void>): this {
return this.on('onTeamsMessageSoftDelete', async (context, next) => {
await handler(context, next);
});
}
/**
* Registers a handler for TeamsMembersAdded events, such as for when members other than the bot
* join the channel, such as your bot's welcome logic.
*
* @param handler A callback to handle the teams members added event.
* @returns A promise that represents the work queued.
*/
onTeamsMembersAddedEvent(
handler: (
membersAdded: TeamsChannelAccount[],
teamInfo: TeamInfo,
context: TurnContext,
next: () => Promise<void>
) => Promise<void>
): this {
return this.on('TeamsMembersAdded', async (context, next) => {
const teamsChannelData = context.activity.channelData as TeamsChannelData;
await handler(context.activity.membersAdded, teamsChannelData.team, context, next);
});
}
/**
* Registers a handler for TeamsMembersRemoved events, such as for when members other than the bot
* leave the channel, such as your bot's good-bye logic.
*
* @param handler A callback to handle the teams members removed event.
* @returns A promise that represents the work queued.
*/
onTeamsMembersRemovedEvent(
handler: (
membersRemoved: TeamsChannelAccount[],
teamInfo: TeamInfo,
context: TurnContext,
next: () => Promise<void>
) => Promise<void>
): this {
return this.on('TeamsMembersRemoved', async (context, next) => {
const teamsChannelData = context.activity.channelData as TeamsChannelData;
await handler(context.activity.membersRemoved, teamsChannelData.team, context, next);
});
}
/**