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

[Node.js Samples] Refactor bots to override ActivityHandler.run() to save state instead #1964

Merged
merged 2 commits into from
Jan 24, 2020
Merged
Changes from all commits
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
12 changes: 10 additions & 2 deletions samples/javascript_nodejs/03.welcome-users/bots/welcomeBot.js
Original file line number Diff line number Diff line change
@@ -55,8 +55,6 @@ class WelcomeBot extends ActivityHandler {
Framework Emulator, press the 'Start Over' button to simulate user joining a bot or a channel`);
}
}
// Save state changes
await this.userState.saveChanges(context);

// By calling next() you ensure that the next BotHandler is run.
await next();
@@ -88,6 +86,16 @@ class WelcomeBot extends ActivityHandler {
});
}

/**
* Override the ActivityHandler.run() method to save state changes after the bot logic completes.
*/
async run(context) {
await super.run(context);

// Save state changes
await this.userState.saveChanges(context);
}

async sendIntroCard(context) {
const card = CardFactory.heroCard(
'Welcome to Bot Framework!',
16 changes: 10 additions & 6 deletions samples/javascript_nodejs/05.multi-turn-prompt/bots/dialogBot.js
Original file line number Diff line number Diff line change
@@ -29,13 +29,17 @@ class DialogBot extends ActivityHandler {

await next();
});
}

this.onDialog(async (context, next) => {
// Save any state changes. The load happened during the execution of the Dialog.
await this.conversationState.saveChanges(context, false);
await this.userState.saveChanges(context, false);
await next();
});
/**
* Override the ActivityHandler.run() method to save state changes after the bot logic completes.
*/
async run(context) {
await super.run(context);

// Save any state changes. The load happened during the execution of the Dialog.
await this.conversationState.saveChanges(context, false);
await this.userState.saveChanges(context, false);
}
}

16 changes: 9 additions & 7 deletions samples/javascript_nodejs/06.using-cards/bots/dialogBot.js
Original file line number Diff line number Diff line change
@@ -37,15 +37,17 @@ class DialogBot extends ActivityHandler {
// By calling next() you ensure that the next BotHandler is run.
await next();
});
}

this.onDialog(async (context, next) => {
// Save any state changes. The load happened during the execution of the Dialog.
await this.conversationState.saveChanges(context, false);
await this.userState.saveChanges(context, false);
/**
* Override the ActivityHandler.run() method to save state changes after the bot logic completes.
*/
async run(context) {
await super.run(context);

// By calling next() you ensure that the next BotHandler is run.
await next();
});
// Save any state changes. The load happened during the execution of the Dialog.
await this.conversationState.saveChanges(context, false);
await this.userState.saveChanges(context, false);
}
}

16 changes: 9 additions & 7 deletions samples/javascript_nodejs/13.core-bot/bots/dialogBot.js
Original file line number Diff line number Diff line change
@@ -30,15 +30,17 @@ class DialogBot extends ActivityHandler {
// By calling next() you ensure that the next BotHandler is run.
await next();
});
}

this.onDialog(async (context, next) => {
// Save any state changes. The load happened during the execution of the Dialog.
await this.conversationState.saveChanges(context, false);
await this.userState.saveChanges(context, false);
/**
* Override the ActivityHandler.run() method to save state changes after the bot logic completes.
*/
async run(context) {
await super.run(context);

// By calling next() you ensure that the next BotHandler is run.
await next();
});
// Save any state changes. The load happened during the execution of the Dialog.
await this.conversationState.saveChanges(context, false);
await this.userState.saveChanges(context, false);
}
}

Original file line number Diff line number Diff line change
@@ -62,7 +62,6 @@ class MultilingualBot extends ActivityHandler {
// selected language.
// If Spanish was selected by the user, the reply below will actually be shown in spanish to the user.
await context.sendActivity(`Your current language code is: ${ lang }`);
await this.userState.saveChanges(context);
} else {
// Show the user the possible options for language. The translation middleware
// will pick up the language selected by the user and
@@ -88,6 +87,16 @@ class MultilingualBot extends ActivityHandler {
await next();
});
}

/**
* Override the ActivityHandler.run() method to save state changes after the bot logic completes.
*/
async run(context) {
await super.run(context);

// Save state changes
await this.userState.saveChanges(context);
}
}

/**
Original file line number Diff line number Diff line change
@@ -29,14 +29,17 @@ class DialogBot extends ActivityHandler {

await next();
});
}

this.onDialog(async (context, next) => {
// Save any state changes. The load happened during the execution of the Dialog.
await this.conversationState.saveChanges(context, false);
await this.userState.saveChanges(context, false);
/**
* Override the ActivityHandler.run() method to save state changes after the bot logic completes.
*/
async run(context) {
await super.run(context);

await next();
});
// Save any state changes. The load happened during the execution of the Dialog.
await this.conversationState.saveChanges(context, false);
await this.userState.saveChanges(context, false);
}
}

16 changes: 9 additions & 7 deletions samples/javascript_nodejs/19.custom-dialogs/bots/dialogBot.js
Original file line number Diff line number Diff line change
@@ -30,15 +30,17 @@ class DialogBot extends ActivityHandler {
// By calling next() you ensure that the next BotHandler is run.
await next();
});
}

this.onDialog(async (context, next) => {
// Save any state changes. The load happened during the execution of the Dialog.
await this.conversationState.saveChanges(context, false);
await this.userState.saveChanges(context, false);
/**
* Override the ActivityHandler.run() method to save state changes after the bot logic completes.
*/
async run(context) {
await super.run(context);

// By calling next() you ensure that the next BotHandler is run.
await next();
});
// Save any state changes. The load happened during the execution of the Dialog.
await this.conversationState.saveChanges(context, false);
await this.userState.saveChanges(context, false);
}
}

Original file line number Diff line number Diff line change
@@ -30,15 +30,17 @@ class DialogBot extends ActivityHandler {
// By calling next() you ensure that the next BotHandler is run.
await next();
});
}

this.onDialog(async (context, next) => {
// Save any state changes. The load happened during the execution of the Dialog.
await this.conversationState.saveChanges(context, false);
await this.userState.saveChanges(context, false);
/**
* Override the ActivityHandler.run() method to save state changes after the bot logic completes.
*/
async run(context) {
await super.run(context);

// By calling next() you ensure that the next BotHandler is run.
await next();
});
// Save any state changes. The load happened during the execution of the Dialog.
await this.conversationState.saveChanges(context, false);
await this.userState.saveChanges(context, false);
}
}

16 changes: 9 additions & 7 deletions samples/javascript_nodejs/43.complex-dialog/bots/dialogBot.js
Original file line number Diff line number Diff line change
@@ -30,15 +30,17 @@ class DialogBot extends ActivityHandler {
// By calling next() you ensure that the next BotHandler is run.
await next();
});
}

this.onDialog(async (context, next) => {
// Save any state changes. The load happened during the execution of the Dialog.
await this.conversationState.saveChanges(context, false);
await this.userState.saveChanges(context, false);
/**
* Override the ActivityHandler.run() method to save state changes after the bot logic completes.
*/
async run(context) {
await super.run(context);

// By calling next() you ensure that the next BotHandler is run.
await next();
});
// Save any state changes. The load happened during the execution of the Dialog.
await this.conversationState.saveChanges(context, false);
await this.userState.saveChanges(context, false);
}
}

Original file line number Diff line number Diff line change
@@ -37,15 +37,17 @@ class CustomPromptBot extends ActivityHandler {
// By calling next() you ensure that the next BotHandler is run.
await next();
});
}

this.onDialog(async (context, next) => {
// Save any state changes. The load happened during the execution of the Dialog.
await this.conversationState.saveChanges(context, false);
await this.userState.saveChanges(context, false);
/**
* Override the ActivityHandler.run() method to save state changes after the bot logic completes.
*/
async run(context) {
await super.run(context);

// By calling next() you ensure that the next BotHandler is run.
await next();
});
// Save any state changes. The load happened during the execution of the Dialog.
await this.conversationState.saveChanges(context, false);
await this.userState.saveChanges(context, false);
}

// Manages the conversation flow for filling out the user's profile.
Original file line number Diff line number Diff line change
@@ -57,15 +57,6 @@ class StateManagementBot extends ActivityHandler {
await next();
});

this.onDialog(async (turnContext, next) => {
// Save any state changes. The load happened during the execution of the Dialog.
await this.conversationState.saveChanges(turnContext, false);
await this.userState.saveChanges(turnContext, false);

// By calling next() you ensure that the next BotHandler is run.
await next();
});

this.onMembersAdded(async (context, next) => {
const membersAdded = context.activity.membersAdded;
for (let cnt = 0; cnt < membersAdded.length; ++cnt) {
@@ -77,6 +68,17 @@ class StateManagementBot extends ActivityHandler {
await next();
});
}

/**
* Override the ActivityHandler.run() method to save state changes after the bot logic completes.
*/
async run(context) {
await super.run(context);

// Save any state changes. The load happened during the execution of the Dialog.
await this.conversationState.saveChanges(context, false);
await this.userState.saveChanges(context, false);
}
}

module.exports.StateManagementBot = StateManagementBot;
15 changes: 9 additions & 6 deletions samples/javascript_nodejs/46.teams-auth/bots/dialogBot.js
Original file line number Diff line number Diff line change
@@ -29,14 +29,17 @@ class DialogBot extends TeamsActivityHandler {

await next();
});
}

this.onDialog(async (context, next) => {
// Save any state changes. The load happened during the execution of the Dialog.
await this.conversationState.saveChanges(context, false);
await this.userState.saveChanges(context, false);
/**
* Override the ActivityHandler.run() method to save state changes after the bot logic completes.
*/
async run(context) {
await super.run(context);

await next();
});
// Save any state changes. The load happened during the execution of the Dialog.
await this.conversationState.saveChanges(context, false);
await this.userState.saveChanges(context, false);
}
}

20 changes: 11 additions & 9 deletions samples/javascript_nodejs/47.inspection/bot.js
Original file line number Diff line number Diff line change
@@ -27,15 +27,6 @@ class IntersectionBot extends ActivityHandler {
await next();
});

this.onDialog(async (context, next) => {
// Save any state changes. The load happened during the execution of the Dialog.
await this.conversationState.saveChanges(context, false);
await this.userState.saveChanges(context, false);

// By calling next() you ensure that the next BotHandler is run.
await next();
});

this.onMembersAdded(async (context, next) => {
const membersAdded = context.activity.membersAdded;
for (let cnt = 0; cnt < membersAdded.length; ++cnt) {
@@ -47,6 +38,17 @@ class IntersectionBot extends ActivityHandler {
await next();
});
}

/**
* Override the ActivityHandler.run() method to save state changes after the bot logic completes.
*/
async run(context) {
await super.run(context);

// Save any state changes. The load happened during the execution of the Dialog.
await this.conversationState.saveChanges(context, false);
await this.userState.saveChanges(context, false);
}
}

module.exports.IntersectionBot = IntersectionBot;
Original file line number Diff line number Diff line change
@@ -47,15 +47,17 @@ class QnABot extends ActivityHandler {
// By calling next() you ensure that the next BotHandler is run.
await next();
});
}

this.onDialog(async (context, next) => {
// Save any state changes. The load happened during the execution of the Dialog.
await this.conversationState.saveChanges(context, false);
await this.userState.saveChanges(context, false);
/**
* Override the ActivityHandler.run() method to save state changes after the bot logic completes.
*/
async run(context) {
await super.run(context);

// By calling next() you ensure that the next BotHandler is run.
await next();
});
// Save any state changes. The load happened during the execution of the Dialog.
await this.conversationState.saveChanges(context, false);
await this.userState.saveChanges(context, false);
}
}

Original file line number Diff line number Diff line change
@@ -23,6 +23,16 @@ class TeamsMessagingExtensionsSearchAuthConfigBot extends TeamsActivityHandler {
this.userState = userState;
}

/**
* Override the ActivityHandler.run() method to save state changes after the bot logic completes.
*/
async run(context) {
await super.run(context);

// Save state changes
await this.userState.saveChanges(context);
}

async handleTeamsMessagingExtensionConfigurationQuerySettingUrl(context, query) {
// The user has requested the Messaging Extension Configuration page settings url.
const userSettings = await this.userConfigurationProperty.get(context, '');
@@ -47,7 +57,6 @@ class TeamsMessagingExtensionsSearchAuthConfigBot extends TeamsActivityHandler {
// When the user submits the settings page, this event is fired.
if (settings.state != null) {
await this.userConfigurationProperty.set(context, settings.state);
await this.userState.saveChanges(context, false);
}
}

16 changes: 9 additions & 7 deletions samples/typescript_nodejs/13.core-bot/src/bots/dialogBot.ts
Original file line number Diff line number Diff line change
@@ -43,14 +43,16 @@ export class DialogBot extends ActivityHandler {
// By calling next() you ensure that the next BotHandler is run.
await next();
});
}

this.onDialog(async (context, next) => {
// Save any state changes. The load happened during the execution of the Dialog.
await this.conversationState.saveChanges(context, false);
await this.userState.saveChanges(context, false);
/**
* Override the ActivityHandler.run() method to save state changes after the bot logic completes.
*/
async run(context): Promise<void> {
await super.run(context);

// By calling next() you ensure that the next BotHandler is run.
await next();
});
// Save any state changes. The load happened during the execution of the Dialog.
await this.conversationState.saveChanges(context, false);
await this.userState.saveChanges(context, false);
}
}