Articles → MICROSOFT BOT FRAMEWORK → Managing States In Microsoft Bot Framework
Managing States In Microsoft Bot Framework
What Does Managing A State Mean?
Scenario
- Get the name from the user.
- Store the name in the memory using the SetAsync method.
- Fetch the name from memory using the GetAsync method.
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
//
// Generated with Bot Builder V4 SDK Template for Visual Studio EchoBot v4.9.2
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Karan_EchoBot.Models;
using Microsoft.Bot.Builder;
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Schema;
using Microsoft.Extensions.Configuration;
namespace Karan_EchoBot.Bots {
public class EchoBot < T >: ActivityHandler where T: Microsoft.Bot.Builder.Dialogs.Dialog {
private IConfiguration _configuration;
private Microsoft.Bot.Builder.Dialogs.Dialog _dialog;
private UserState _userState;
private ConversationState _conversationState;
public EchoBot(IConfiguration configuration, UserState userState, ConversationState conversationState, T dialog) {
_userState = userState;
_conversationState = conversationState;
_configuration = configuration;
this._dialog = dialog;
}
public override async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken =
default) {
await base.OnTurnAsync(turnContext, cancellationToken);
//Saving States.(This is used to save data to User State and Conversation State for each turn)
await _userState.SaveChangesAsync(turnContext, false, cancellationToken);
await _conversationState.SaveChangesAsync(turnContext, false, cancellationToken);
}
protected override async Task OnMessageActivityAsync(ITurnContext < IMessageActivity > turnContext, CancellationToken cancellationToken) {
await _dialog.RunAsync(turnContext, _conversationState.CreateProperty < DialogState > ("DialogState"), cancellationToken);
/*
// Get Your Data.
var userData = await _userData.GetAsync(turnContext, () => new UserData(), cancellationToken);
var conversationData = await _conversationData.GetAsync(turnContext, () => new ConversationData(), cancellationToken);
if (conversationData.PromptedUserForName && !conversationData.PromptedUserForAddress)
{
//Save Name
userData.Name = turnContext.Activity.Text;
await _userData.SetAsync(turnContext, userData, cancellationToken);
await turnContext.SendActivityAsync(MessageFactory.Text("Where do you stay?"),cancellationToken);
//Save Conversation
conversationData.PromptedUserForAddress = true;
await _conversationData.SetAsync(turnContext, conversationData,cancellationToken);
}
else if (conversationData.PromptedUserForName && conversationData.PromptedUserForAddress)
{
//Save Address
userData.Address = turnContext.Activity.Text;
await _userData.SetAsync(turnContext, userData, cancellationToken);
await turnContext.SendActivityAsync(MessageFactory.Text($"Hi,**{userData.Name}** we will deliver your order at **{userData.Address}**. \nThanks."),cancellationToken);
}
else {
await turnContext.SendActivityAsync(MessageFactory.Text("Hey, What's your good name?"), cancellationToken);
//Save Conversation
conversationData.PromptedUserForName = true;
await _conversationData.SetAsync(turnContext, conversationData, cancellationToken);
}*/
}
protected override Task OnEventAsync(ITurnContext < IEventActivity > turnContext, CancellationToken cancellationToken) {
//Any logic
return base.OnEventAsync(turnContext, cancellationToken);
}
protected override async Task OnMembersAddedAsync(IList < ChannelAccount > membersAdded, ITurnContext < IConversationUpdateActivity > turnContext, CancellationToken cancellationToken) {
var welcomeText = "Hello! I am Zobo! A food delivery bot!";
foreach(var member in membersAdded) {
if (member.Id != turnContext.Activity.Recipient.Id) {
await turnContext.SendActivityAsync(MessageFactory.Text(welcomeText, welcomeText), cancellationToken);
}
}
}
}
}
using Karan_EchoBot.Models;
using Microsoft.Bot.Builder;
using Microsoft.Bot.Builder.Dialogs;
using System.Threading;
using System.Threading.Tasks;
namespace Karan_EchoBot.Dialog {
public class RootDialog: ComponentDialog {
private UserState _userState;
private ConversationState _conversationState;
private IStatePropertyAccessor < UserData > _userData;
private IStatePropertyAccessor < ConversationData > _conversationData;
public RootDialog(UserState userState, ConversationState conversationState): base(nameof(RootDialog)) {
_userState = userState;
_conversationState = conversationState;
_userData = userState.CreateProperty < UserData > ("UserData");
_conversationData = conversationState.CreateProperty < ConversationData > ("ConversationData");
var steps = new WaterfallStep[] {
AskName,
AskAddress
};
AddDialog(new WaterfallDialog("WaterFallId", steps));
}
private async Task < DialogTurnResult > AskAddress(WaterfallStepContext stepContext, CancellationToken cancellationToken) {
var userData = await _userData.GetAsync(stepContext.Context, () => new UserData(), cancellationToken);
userData.Name = stepContext.Context.Activity.Text;
await _userData.SetAsync(stepContext.Context, userData, cancellationToken);
await stepContext.Context.SendActivityAsync(MessageFactory.Text("This is the second message"), cancellationToken);
return await stepContext.EndDialogAsync(null);
}
private async Task < DialogTurnResult > AskName(WaterfallStepContext stepContext, CancellationToken cancellationToken) {
var userData = await _userData.GetAsync(stepContext.Context, () => new UserData(), cancellationToken);
if (!string.IsNullOrWhiteSpace(userData.Name)) {
await stepContext.Context.SendActivityAsync(MessageFactory.Text($"Hi {userData.Name}!!! What is your address?"), cancellationToken);
} else {
await stepContext.Context.SendActivityAsync(MessageFactory.Text("What is your name"), cancellationToken);
}
return Microsoft.Bot.Builder.Dialogs.Dialog.EndOfTurn;
}
}
}
Output
Click to Enlarge