States in a Bot is used to maintain a sequential or meaningful flow. There can be Bots without state management too, where any information regarding the user or conversations is not needed in the future conversation. However, when information has to flow within the conversation between the user and the bot, state has to be maintained.
There are 3 types of bots that can be maintained.
- User State – This will be available in any turn in a bot and user conversation.
- Conversation State – This is available in any turn in a specific conversation.
- Private conversation State – This is available for a specific user in a specific conversation.
States are Key Value pair information stored in the storage layer. There are three ways of storing data.
- Memory Storage – Data will be cleared when the bot restarts.
- Azure Blob Storage.
- Azure Cosmos DB partitioned storage.
State Accessor:
State Accessors are used to get, set or delete state properties in the storage layer. This facilitates the transfer of state data between the Bot state cache (Local cache maintained by the Bot) and the storage layer.
- Get Method: Retrieves the state from State Cache
- Set Method: Sets the bot state State Cache
- Save Method: Checks the state in the State cache and updates it in the storage.
Echo Bot with State management:
Create Sample echo bot as mentioned in our earlier blog.
In the “EchoBot.cs” follow the below changes.
Add two bot states to capture conversation and user data.
private BotState _conversationState;
private BotState _userState;
On every “TurnAsync”(activity) save the data to the states as below.
await _conversationState.SaveChangesAsync(turnContext, false, cancellationToken);
await _userState.SaveChangesAsync(turnContext, false, cancellationToken);
Create a class called “UserProfile” to collect username.
public class UserProfile
{
public string Name { get; set; }
}
Create a class called “ConversationData” to collect information about conversation.
public class ConversationData
{
public string Timestamp { get; set; }
// The ID of the user's channel.
public string ChannelId { get; set; }
// Track whether we have already asked the user's name
public bool PromptedUserForName { get; set; } = false;
}
There are two accessors to get or store information in Local memory cache. The lines below are used to get the information from local memory.
var conversationData = await conversationStateAccessors.GetAsync(turnContext, () => new ConversationData());
var userStateAccessors = _userState.CreateProperty<UserProfile>(nameof(UserProfile));
We can get the information from the local memory by the following lines.
var userProfile = await userStateAccessors.GetAsync(turnContext, () => new UserProfile());
After our welcome message, the responses from Bot framework emulator are given below.
Please find the sample code with all the above steps explained here.
BharathiDemo/StateManagementBot: Echo Bot with State Management Properties explained. (github.com)
References:
Managing state in Bot Framework SDK – Bot Service | Microsoft Learn
Hope this blog helps in creating Bot with State Management.
No Comment! Be the first one.