K48 K48 - 1 year ago 944
C# Question

How to create multiple dialogs in bot using MS bot framework so that bot remembers which dialog is in progress

I'm using MS Bot Framework and C# to build a bot that can handle 3 dialogs. Each dialog is built using the FormDialog and FormBuilder, like this:

internal static IDialog<OrderDialogForm> BuildDialog()
return Chain.From(() => FormDialog.FromForm(BuildForm));

When you first talk to the bot, it offers you to select one of the three dialogs, e.g. "fill in the order", "enter your user profile", "get support",

Once the users selects, for example, "fill in the order", the bot launches the appropriate dialog.

Obviously, the user should just continue answering the questions inside the dialog until the dialog is over.

But every time the user sends a message, it is passed to this method in the API controller:

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

From here, the bot needs to decide which of the three dialogs is currently in progress, and continue that dialog.

How do I do that, remember which dialog is currently in progress and with each new message from the user, continue that dialog instead of returning the user to the main screen?

My idea is to create some kind of global variable or a record that is stored somewhere else, maybe in the database. The record would contain the type of the current dialog that this user is having with the bot right now. Every time the bot receives a message, it would query the database to find out that the last interaction of the user was with the OrderDialog, and so the program code can decide to continue with the OrderDialog. But it seems slow and maybe there is some kind of built-in function in Bot Framework to store data about the user, such as which dialog type it last interacted with.

Answer Source

Use the Bot State Service https://docs.botframework.com/en-us/csharp/builder/sdkreference/stateapi.html

public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
    // Detect if this is a Message activity
    if (activity.Type == ActivityTypes.Message)
        // Get any saved values
        StateClient sc = activity.GetStateClient();
        BotData userData = sc.BotState.GetPrivateConversationData(
            activity.ChannelId, activity.Conversation.Id, activity.From.Id);
        var boolProfileComplete = userData.GetProperty<bool>("ProfileComplete");
        if (!boolProfileComplete)
            // Call our FormFlow by calling MakeRootDialog
            await Conversation.SendAsync(activity, MakeRootDialog);
            // Get the saved profile values
            var FirstName = userData.GetProperty<string>("FirstName");
            var LastName = userData.GetProperty<string>("LastName");
            var Gender = userData.GetProperty<string>("Gender");
            // Tell the user their profile is complete
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append("Your profile is complete.\n\n");
            sb.Append(String.Format("FirstName = {0}\n\n", FirstName));
            sb.Append(String.Format("LastName = {0}\n\n", LastName));
            sb.Append(String.Format("Gender = {0}", Gender));
            // Create final reply
            ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl));
            Activity replyMessage = activity.CreateReply(sb.ToString());
            await connector.Conversations.ReplyToActivityAsync(replyMessage);
        // This was not a Message activity
    // Send response
    var response = Request.CreateResponse(HttpStatusCode.OK);
    return response;

Sample From: Introduction To FormFlow With The Microsoft Bot Framework http://aihelpwebsite.com/Blog/EntryId/8/Introduction-To-FormFlow-With-The-Microsoft-Bot-Framework

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download