forked from microsoft/botbuilder-js
-
Notifications
You must be signed in to change notification settings - Fork 3
/
activityHandlerBase.ts
441 lines (415 loc) · 16.2 KB
/
activityHandlerBase.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
/**
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/
import { ActivityTypes, ChannelAccount, MessageReaction, TurnContext } from '.';
import { InvokeResponse } from './invokeResponse';
import { StatusCodes } from 'botframework-schema';
// This key is exported internally so that subclassed ActivityHandlers and BotAdapters will not override any already set InvokeResponses.
export const INVOKE_RESPONSE_KEY = Symbol('invokeResponse');
/**
* Defines the core behavior for event-emitting activity handlers for bots.
*
* @remarks
* This provides an extensible class for handling incoming activities in an event-driven way.
* You can register an arbitrary set of handlers for each event type.
*
* To register a handler for an event, use the corresponding _on event_ method. If multiple handlers are
* registered for an event, they are run in the order in which they were registered.
*
* This object emits a series of _events_ as it processes an incoming activity.
* A handler can stop the propagation of the event by not calling the continuation function.
*
* | Event type | Description |
* | :--- | :--- |
* | Type-specific | Emitted for the specific activity type, before emitting an event for any sub-type. |
* | Sub-type | Emitted for certain specialized events, based on activity content. |
*
* **See also**
* - The [Bot Framework Activity schema](https://aka.ms/botSpecs-activitySchema)
*/
export class ActivityHandlerBase {
/**
* Called at the start of the event emission process.
*
* @param context The context object for the current turn.
*
* @remarks
* Override this method to use custom logic for emitting events.
*
* The default logic is to call any type-specific and sub-type handlers registered via
* the various _on event_ methods. Type-specific events are defined for:
* - Message activities
* - Conversation update activities
* - Message reaction activities
* - Event activities
* - Invoke activities
* - _Unrecognized_ activities, ones that this class has not otherwise defined an _on event_ method for.
*/
protected async onTurnActivity(context: TurnContext): Promise<void> {
switch (context.activity.type) {
case ActivityTypes.Message:
await this.onMessageActivity(context);
break;
case ActivityTypes.MessageUpdate:
await this.onMessageUpdateActivity(context);
break;
case ActivityTypes.MessageDelete:
await this.onMessageDeleteActivity(context);
break;
case ActivityTypes.ConversationUpdate:
await this.onConversationUpdateActivity(context);
break;
case ActivityTypes.MessageReaction:
await this.onMessageReactionActivity(context);
break;
case ActivityTypes.Event:
await this.onEventActivity(context);
break;
case ActivityTypes.Invoke: {
const invokeResponse = await this.onInvokeActivity(context);
// If onInvokeActivity has already sent an InvokeResponse, do not send another one.
if (invokeResponse && !context.turnState.get(INVOKE_RESPONSE_KEY)) {
await context.sendActivity({ value: invokeResponse, type: 'invokeResponse' });
}
break;
}
case ActivityTypes.EndOfConversation:
await this.onEndOfConversationActivity(context);
break;
case ActivityTypes.Typing:
await this.onTypingActivity(context);
break;
case ActivityTypes.InstallationUpdate:
await this.onInstallationUpdateActivity(context);
break;
case ActivityTypes.Command:
await this.onCommandActivity(context);
break;
case ActivityTypes.CommandResult:
await this.onCommandResultActivity(context);
break;
default:
// handler for unknown or unhandled types
await this.onUnrecognizedActivity(context);
break;
}
}
/**
* Provides a hook for emitting the _message_ event.
*
* @param _context The context object for the current turn.
*
* @remarks
* Override this method to run registered _message_ handlers and then continue the event
* emission process.
*/
protected async onMessageActivity(_context: TurnContext): Promise<void> {
return;
}
/**
* Provides a hook for emitting the _message update_ event.
*
* @param _context The context object for the current turn.
*
* @remarks
* Override this method to run registered _message update_ handlers and then continue the event
* emission process.
*/
protected async onMessageUpdateActivity(_context: TurnContext): Promise<void> {
return;
}
/**
* Provides a hook for emitting the _message delete_ event.
*
* @param _context The context object for the current turn.
*
* @remarks
* Override this method to run registered _message delete_ handlers and then continue the event
* emission process.
*/
protected async onMessageDeleteActivity(_context: TurnContext): Promise<void> {
return;
}
/**
* Provides a hook for emitting the _conversation update_ event.
*
* @param context The context object for the current turn.
*
* @remarks
* Override this method to run registered _conversation update_ handlers and then continue the event
* emission process.
*
* The default logic is:
* - If members other than the bot were added to the conversation,
* call [onMembersAddedActivity](xref:botbuilder-core.ActivityHandlerBase.onMembersAddedActivity).
* - If members other than the bot were removed from the conversation,
* call [onMembersRemovedActivity](xref:botbuilder-core.ActivityHandlerBase.onMembersRemovedActivity).
*/
protected async onConversationUpdateActivity(context: TurnContext): Promise<void> {
if (context.activity.membersAdded && context.activity.membersAdded.length > 0) {
if (
context.activity.membersAdded.filter(
(m) => context.activity.recipient && context.activity.recipient.id !== m.id
).length
) {
await this.onMembersAddedActivity(context.activity.membersAdded, context);
}
} else if (context.activity.membersRemoved && context.activity.membersRemoved.length > 0) {
if (
context.activity.membersRemoved.filter(
(m) => context.activity.recipient && context.activity.recipient.id !== m.id
).length
) {
await this.onMembersRemovedActivity(context.activity.membersRemoved, context);
}
}
}
/**
* Provides a hook for emitting the _message reaction_ event.
*
* @param context The context object for the current turn.
*
* @remarks
* Override this method to run registered _message reaction_ handlers and then continue the event
* emission process.
*
* The default logic is:
* - If reactions were added to a message,
* call [onReactionsAddedActivity](xref:botbuilder-core.ActivityHandlerBase.onReactionsAddedActivity).
* - If reactions were removed from a message,
* call [onReactionsRemovedActivity](xref:botbuilder-core.ActivityHandlerBase.onReactionsRemovedActivity).
*/
protected async onMessageReactionActivity(context: TurnContext): Promise<void> {
if (context.activity.reactionsAdded?.length) {
await this.onReactionsAddedActivity(context.activity.reactionsAdded, context);
}
if (context.activity.reactionsRemoved?.length) {
await this.onReactionsRemovedActivity(context.activity.reactionsRemoved, context);
}
}
/**
* Provides a hook for emitting the _event_ event.
*
* @param _context The context object for the current turn.
*
* @remarks
* Override this method to run registered _event_ handlers and then continue the event
* emission process.
*/
protected async onEventActivity(_context: TurnContext): Promise<void> {
return;
}
/**
* Provides a hook for invoke calls.
*
* @param _context The context object for the current turn.
* @returns {Promise<InvokeResponse>} An Invoke Response for the activity.
* @remarks
* Override this method to handle particular invoke calls.
*/
protected async onInvokeActivity(_context: TurnContext): Promise<InvokeResponse> {
return { status: StatusCodes.NOT_IMPLEMENTED };
}
/**
* Provides a hook for emitting the _end of conversation_ event.
*
* @param _context The context object for the current turn.
*
* @remarks
* Override this method to run registered _end of conversation_ handlers and then continue the event
* emission process.
*/
protected async onEndOfConversationActivity(_context: TurnContext): Promise<void> {
return;
}
/**
* Provides a hook for emitting the _typing_ event.
*
* @param _context The context object for the current turn.
*
* @remarks
* Override this method to run registered _typing_ handlers and then continue the event
* emission process.
*/
protected async onTypingActivity(_context: TurnContext): Promise<void> {
return;
}
/**
* Provides a hook for emitting the _installationupdate_ event.
*
* @param context The context object for the current turn.
*
* @remarks
* Override this method to run registered _installationupdate_ handlers and then continue the event
* emission process.
*/
protected async onInstallationUpdateActivity(context: TurnContext): Promise<void> {
switch (context.activity.action) {
case 'add':
case 'add-upgrade':
await this.onInstallationUpdateAddActivity(context);
return;
case 'remove':
case 'remove-upgrade':
await this.onInstallationUpdateRemoveActivity(context);
return;
}
}
/**
* Provides a hook for emitting the _installationupdateadd_ event.
*
* @param _context The context object for the current turn.
*
* @remarks
* Override this method to run registered _installationupdateadd_ handlers and then continue the event
* emission process.
*/
protected async onInstallationUpdateAddActivity(_context: TurnContext): Promise<void> {
return;
}
/**
* Provides a hook for emitting the _installationupdateremove_ event.
*
* @param _context The context object for the current turn.
*
* @remarks
* Override this method to run registered _installationupdateremove_ handlers and then continue the event
* emission process.
*/
protected async onInstallationUpdateRemoveActivity(_context: TurnContext): Promise<void> {
return;
}
/**
* Provides a hook for emitting the _unrecognized_ event.
*
* @param _context The context object for the current turn.
*
* @remarks
* Override this method to run registered _unrecognized_ handlers and then continue the event
* emission process.
*/
protected async onUnrecognizedActivity(_context: TurnContext): Promise<void> {
return;
}
/**
* Provides a hook for emitting the _members added_ event,
* a sub-type of the _conversation update_ event.
*
* @param _membersAdded An array of the members added to the conversation.
* @param _context The context object for the current turn.
*
* @remarks
* Override this method to run registered _members added_ handlers and then continue the event
* emission process.
*/
protected async onMembersAddedActivity(_membersAdded: ChannelAccount[], _context: TurnContext): Promise<void> {
return;
}
/**
* Provides a hook for emitting the _members removed_ event,
* a sub-type of the _conversation update_ event.
*
* @param _membersRemoved An array of the members removed from the conversation.
* @param _context The context object for the current turn.
*
* @remarks
* Override this method to run registered _members removed_ handlers and then continue the event
* emission process.
*/
protected async onMembersRemovedActivity(_membersRemoved: ChannelAccount[], _context: TurnContext): Promise<void> {
return;
}
/**
* Provides a hook for emitting the _reactions added_ event,
* a sub-type of the _message reaction_ event.
*
* @param _reactionsAdded An array of the reactions added to a message.
* @param _context The context object for the current turn.
*
* @remarks
* Override this method to run registered _reactions added_ handlers and then continue the event
* emission process.
*/
protected async onReactionsAddedActivity(_reactionsAdded: MessageReaction[], _context: TurnContext): Promise<void> {
return;
}
/**
* Provides a hook for emitting the _reactions removed_ event,
* a sub-type of the _message reaction_ event.
*
* @param _reactionsRemoved An array of the reactions removed from a message.
* @param _context The context object for the current turn.
*
* @remarks
* Override this method to run registered _reactions removed_ handlers and then continue the event
* emission process.
*/
protected async onReactionsRemovedActivity(
_reactionsRemoved: MessageReaction[],
_context: TurnContext
): Promise<void> {
return;
}
/**
* Invoked when a command activity is received when the base behavior of
* `onTurn()` is used.
* Commands are requests to perform an action and receivers typically respond with
* one or more commandResult activities. Receivers are also expected to explicitly
* reject unsupported command activities.
*
* @param _context A context object for this turn.
* @returns A promise that represents the work queued to execute.
*/
protected async onCommandActivity(_context: TurnContext): Promise<void> {
return;
}
/**
* Invoked when a commandResult activity is received when the base behavior of
* `onTurn()` is used.
* CommandResult activity can be used to communicate the result of a command execution.
*
* @param _context A context object for this turn.
* @returns A promise that represents the work queued to execute.
*/
protected async onCommandResultActivity(_context: TurnContext): Promise<void> {
return;
}
/**
* Called to initiate the event emission process.
*
* @param context The context object for the current turn.
*
* @remarks
* Typically, you would provide this method as the function handler that the adapter calls
* to perform the bot's logic after the received activity has been pre-processed by the adapter
* and routed through any middleware.
*
* For example:
* ```javascript
* server.post('/api/messages', (req, res) => {
* adapter.processActivity(req, res, async (context) => {
* // Route to main dialog.
* await bot.run(context);
* });
* });
* ```
*
* **See also**
* - [BotFrameworkAdapter.processActivity](xref:botbuilder.BotFrameworkAdapter.processActivity)
*/
async run(context: TurnContext): Promise<void> {
if (!context) {
throw new Error('Missing TurnContext parameter');
}
if (!context.activity) {
throw new Error('TurnContext does not include an activity');
}
if (!context.activity.type) {
throw new Error('Activity is missing its type');
}
// List of all Activity Types:
// https://github.com/Microsoft/botbuilder-js/blob/main/libraries/botframework-schema/src/index.ts#L1627
await this.onTurnActivity(context);
}
}