Azure Chatbot State Management example

Prerequisite  for bot development using .net code

Below  tools and SDKs are required:

1. Visual Studio 2015/2017

2.Visual Studio Bot Application Template for C#

3.Download the bot application template from

the download contains a zip file named

4.Copy the zip to the Visual Studio Templates folder for C#. The
default location for templates is %USERPROFILE%\Documents\
Visual Studio 2015\Templates\ProjectTemplates\Visual C#\. (This
path is for VS 2015; for other version like VS 2017, use the Visual
Studio 2017 Templates folder path will be %USERPROFILE%\Documents\Visual Studio 2017\Templates\ProjectTemplates\Visual C#).
• Open Visual Studio as Administrator and click on New Project.
Search for “Bot Application” using the search section at the top
right corner. You should now be able to see the project template
for bots in the Templates section.

5.Download and install Bot Emulator from here

6.The Azure Bot framework provides three stores that you can use to store state, as follows:

  • User data state
  • Conversation data state
  • Private conversation state

7.The Conversation and the Private Conversation data stores are available for the lifetime of a conversation within a session.User data continues to be available even if the user switches or creates a new conversation.By default, Azure  Bot framework stores the state in cache using the CachingBotDataStore Object.

Step1.Create new botapplication in visual studio


Step2 :add new class StateDialog.cs

using Microsoft.Bot.Builder.Dialogs;

using Microsoft.Bot.Connector;

using System;

using System.Collections.Generic;

using System.Linq;

using System.Threading.Tasks;

using System.Web;

namespace Bot_State_Application.Dialogs



public class StateDialog: IDialog<object>

public async Task StartAsync(IDialogContext dialogContext)


await dialogContext.PostAsync("Provide some details to help you");


public async Task ConversationStart(IDialogContext dialogContext, IAwaitable<IMessageActivity> result)


var patiantName = await result;

//Set user  data

await dialogContext.PostAsync("Please provide patient name");



public async Task GetPatientName(IDialogContext dialogContext, IAwaitable<IMessageActivity> result)


var patiantName = await result;

//Set conversion  data

dialogContext.UserData.SetValue("patientname", patiantName.Text);

await dialogContext.PostAsync("Please provide patient address");



public async Task GetPatientAddress(IDialogContext dialogContext, IAwaitable<IMessageActivity> result)


var patiantAddress = await result;

//Set conversion  data

dialogContext.ConversationData.SetValue("address", patiantAddress.Text);

await dialogContext.PostAsync("Please provide patient medicalHistory");



public async Task GetPatientMedicalHistory(IDialogContext dialogContext, IAwaitable<IMessageActivity> result)


var medicalHistory = await result;

//Set PrivateConversationData  data

dialogContext.PrivateConversationData.SetValue("patientMedicalHistory", medicalHistory.Text);

await dialogContext.PostAsync("Do you want to see all patient info");



public async Task ShowPatientData(IDialogContext dialogContext, IAwaitable<IMessageActivity> result)


var yes = await result;

//get data

//get userdata

var name = dialogContext.UserData.GetValue<string>("patientname");

var address = dialogContext.ConversationData.GetValue<string>("address");

var medicalHistory = dialogContext.PrivateConversationData.GetValue<string>("patientMedicalHistory");

var data = $"Details retrieved from Bot state  >> Patient Name: {name}, Address: {address}, MedicalHistory: {medicalHistory} ";

await dialogContext.PostAsync(data);





Step 3:MessagesController.cs changes

Comment the line  >>  await Conversation.SendAsync(activity, () => new Dialogs.RootDialog());

And add line  await Conversation.SendAsync(activity, () => new Dialogs.StateDialog());

Code changes show here. ..

public async Task<HttpResponseMessage> Post([FromBody]Activity activity)


if (activity.Type == ActivityTypes.Message)


// await Conversation.SendAsync(activity, () => new Dialogs.RootDialog());

await Conversation.SendAsync(activity, () => new Dialogs.StateDialog());






var response = Request.CreateResponse(HttpStatusCode.OK);

return response;



Step 4:Build project and add bot api url in emulator.

Keep application Id and password blank.

Test chatbot in here. Emulator Bot responses shows the state data :

You can download working code here

Leave a Reply

Your email address will not be published. Required fields are marked *