AutoSaveStateMiddleware class

Middleware that will automatically save any state changes at the end of the turn.

Remarks

The AutoSaveStateMiddleware class should be added towards the top of your bot's middleware stack, before any other components that use state. Any BotState plugins passed to the constructor will have their BotState.saveChanges() method called upon successful completion of the turn.

This example shows boilerplate code for reading and writing conversation and user state within a bot:

const { AutoSaveStateMiddleware, ConversationState, UserState, MemoryStorage } = require('botbuilder');

const storage = new MemoryStorage();
const conversationState = new ConversationState(storage);
const userState = new UserState(storage);
adapter.use(new AutoSaveStateMiddleware(conversationState, userState));

server.post('/api/messages', (req, res) => {
   adapter.processActivity(req, res, async (turnContext) => {
      // Get state
      const convo = await conversationState.load(turnContext);
      const user = await userState.load(turnContext);

      // ... route activity ...
      // ...make changes to state objects...
      // ... no need to call userState.saveChanges() or conversationState.saveChanges() anymore!
   });
});

Constructors

AutoSaveStateMiddleware(BotState[])

Creates a new AutoSaveStateMiddleware instance.

Properties

botStateSet

Set of BotState plugins being automatically saved.

Methods

add(BotState[])

Adds additional BotState plugins to be saved.

onTurn(TurnContext, () => Promise<void>)

Called by the adapter (for example, a BotFrameworkAdapter) at runtime in order to process an inbound Activity.

Constructor Details

AutoSaveStateMiddleware(BotState[])

Creates a new AutoSaveStateMiddleware instance.

new AutoSaveStateMiddleware(botStates: BotState[])

Parameters

botStates

BotState[]

One or more BotState plugins to automatically save at the end of the turn.

Property Details

botStateSet

Set of BotState plugins being automatically saved.

botStateSet: BotStateSet

Property Value

Method Details

add(BotState[])

Adds additional BotState plugins to be saved.

function add(botStates: BotState[]): this

Parameters

botStates

BotState[]

One or more BotState plugins to add.

Returns

this

The updated BotStateSet object.

onTurn(TurnContext, () => Promise<void>)

Called by the adapter (for example, a BotFrameworkAdapter) at runtime in order to process an inbound Activity.

function onTurn(context: TurnContext, next: () => Promise<void>): Promise<void>

Parameters

context
TurnContext

The context object for this turn.

next

() => Promise<void>

The next delegate function.

Returns

Promise<void>