Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add installation update to activity handler #2562

Merged
merged 2 commits into from
Jul 23, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions libraries/botbuilder-core/etc/botbuilder-core.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ export class ActivityHandler extends ActivityHandlerBase {
protected onTurnActivity(context: TurnContext): Promise<void>;
onTyping(handler: BotHandler): this;
protected onTypingActivity(context: TurnContext): Promise<void>;
onInstallationUpdate(handler: BotHandler): this;
protected onInstallationUpdateActivity(context: TurnContext): Promise<void>;
protected onUnrecognizedActivity(context: TurnContext): Promise<void>;
onUnrecognizedActivityType(handler: BotHandler): this;
run(context: TurnContext): Promise<void>;
Expand All @@ -95,6 +97,7 @@ export class ActivityHandlerBase {
protected onReactionsRemovedActivity(reactionsRemoved: MessageReaction[], context: TurnContext): Promise<void>;
protected onTurnActivity(context: TurnContext): Promise<void>;
protected onTypingActivity(context: TurnContext): Promise<void>;
protected onInstallationUpdateActivity(context: TurnContext): Promise<void>;
protected onUnrecognizedActivity(context: TurnContext): Promise<void>;
run(context: TurnContext): Promise<void>;
}
Expand Down
31 changes: 31 additions & 0 deletions libraries/botbuilder-core/src/activityHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,21 @@ export class ActivityHandler extends ActivityHandlerBase {
return this.on('Typing', handler);
}

/**
* Registers an activity event handler for the _installationupdate_ activity.
*
* @param handler The event handler.
*
* @remarks
* Returns a reference to the [ActivityHandler](xref:botbuilder-core.ActivityHandler) object.
*
* To handle a InstallationUpdate event, use the
* [onInstallationUpdate](xref:botbuilder-core.ActivityHandler.onInstallationUpdate) type-specific event handler.
*/
public onInstallationUpdate(handler: BotHandler): this {
return this.on('InstallationUpdate', handler);
}

/**
* Registers an activity event handler for the _tokens-response_ event, emitted for any incoming
* `tokens/response` event activity. These are generated as part of the OAuth authentication flow.
Expand Down Expand Up @@ -498,6 +513,22 @@ export class ActivityHandler extends ActivityHandlerBase {
await this.handle(context, 'Typing', this.defaultNextEvent(context));
}

/**
* Runs all registered _instllationupdate_ handlers and then continues the event emission process.
*
* @param context The context object for the current turn.
*
* @remarks
* Overwrite this method to support channel-specific behavior across multiple channels.
*
* The default logic is to call any handlers registered via
* [onInstallationUpdateActivity](xref:botbuilder-core.ActivityHandler.onInstallationUpdateActivity),
* and then continue by calling [defaultNextEvent](xref:botbuilder-core.ActivityHandler.defaultNextEvent).
*/
protected async onInstallationUpdateActivity(context: TurnContext): Promise<void> {
await this.handle(context, 'InstallationUpdate', this.defaultNextEvent(context));
}

/**
* Runs all registered _unrecognized activity type_ handlers and then continues the event emission process.
*
Expand Down
16 changes: 16 additions & 0 deletions libraries/botbuilder-core/src/activityHandlerBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ export class ActivityHandlerBase {
case ActivityTypes.Typing:
await this.onTypingActivity(context);
break;
case ActivityTypes.InstallationUpdate:
await this.onInstallationUpdateActivity(context);
break;
default:
// handler for unknown or unhandled types
await this.onUnrecognizedActivity(context);
Expand Down Expand Up @@ -200,6 +203,19 @@ export class ActivityHandlerBase {
return;
}

/**
* Provides a hook for emitting the _installationupdate_ event.
*
* @param context The context object for the current turn.
*
* @remarks
* Overwrite this method to run registered _installationupdate_ handlers and then continue the event
* emission process.
*/
protected async onInstallationUpdateActivity(context: TurnContext): Promise<void> {
return;
}

/**
* Provides a hook for emitting the _unrecognized_ event.
*
Expand Down
13 changes: 13 additions & 0 deletions libraries/botbuilder-core/tests/ActivityHandler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,19 @@ describe('ActivityHandler', function() {
processActivity({type: ActivityTypes.Typing}, bot, done);
});

it(`should fire onInstallationUpdate`, async function(done) {

const bot = new ActivityHandler();

bot.onInstallationUpdate(async (context, next) => {
assert(true, 'onInstallationUpdate not called');
done();
await next();
});

processActivity({type: ActivityTypes.InstallationUpdate}, bot, done);
});

it(`should fire onUnrecognizedActivityType`, async function(done) {

const bot = new ActivityHandler();
Expand Down
9 changes: 9 additions & 0 deletions libraries/botbuilder-core/tests/activityHandlerBase.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ describe('ActivityHandlerBase', function() {
let onEventCalled = false;
let onEndOfConversationCalled = false;
let onTypingCalled = false;
let onInstallationUpdateCalled = false;
let onUnrecognizedActivity = false;

afterEach(function() {
Expand All @@ -39,6 +40,7 @@ describe('ActivityHandlerBase', function() {
onEventCalled = false;
onEndOfConversationCalled = false;
onTypingCalled = false;
onInstallationUpdateCalled = false;
onUnrecognizedActivity = false;
});

Expand Down Expand Up @@ -127,6 +129,11 @@ describe('ActivityHandlerBase', function() {
onTypingCalled = true;
}

async onInstallationUpdateActivity(context) {
assert(context, 'context not found');
onInstallationUpdateCalled = true;
}

async onUnrecognizedActivity(context) {
assert(context, 'context not found');
onUnrecognizedActivity = true;
Expand All @@ -142,6 +149,7 @@ describe('ActivityHandlerBase', function() {
processActivity({type: ActivityTypes.Event}, bot, done);
processActivity({type: ActivityTypes.EndOfConversation}, bot, done);
processActivity({type: ActivityTypes.Typing}, bot, done);
processActivity({type: ActivityTypes.InstallationUpdate}, bot, done);
processActivity({ type: 'unrecognized' }, bot, done);

assert(onTurnActivityCalled, 'onTurnActivity was not called');
Expand All @@ -151,6 +159,7 @@ describe('ActivityHandlerBase', function() {
assert(onEventCalled, 'onEventActivity was not called');
assert(onEndOfConversationCalled, 'onEndOfConversationCalled was not called');
assert(onTypingCalled, 'onTypingCalled was not called');
assert(onInstallationUpdateCalled, 'onInstallationUpdateCalled was not called');
assert(onUnrecognizedActivity, 'onUnrecognizedActivity was not called');
done();
});
Expand Down