Customer Support Chatbot | Part 5 | Creating Sub Dialogs | Microsoft Bot Framework

This is the bot series on Customer Support Chatbot. This bot will allow you to track orders, raise complaints, get bills/invoices, and cancel orders. In this part, we will be creating the sub dialogs and sending the user to these dialogs based on the selection.

Prerequisites

  1. Visual Studio
  2. Bot Framework Emulator

The links to the above requirements are available on the downloads page.

Video

Creating Sub Dialogs

MainDialog Is the root dialog for all your sub dialogs. You can call any number of sub dialogs from here. Create a new folder under Dialogs with the name Operations.

Add four new class files in the Operations folder with the name –

  1. TrackOrderDialog
  2. RaiseComplaintDialog
  3. GetBillsDialog
  4. CancelOrderDialog

Make sure to extend the ComponentDialog in all four classes. Below is the example from TrackOrderDialog.

using Microsoft.Bot.Builder.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace CustomerSupportBot.Dialogs.Operations
{
    public class TrackOrderDialog : ComponentDialog
    {
    }
}

Create a similar constructor for all four classes without any steps in the WaterfallStep[]. In the next parts, we will create these steps and implement our conversation.

public TrackOrderDialog() : base(nameof(TrackOrderDialog))
        {
            var waterfallSteps = new WaterfallStep[]
            {

            };

            AddDialog(new WaterfallDialog(nameof(WaterfallDialog), waterfallSteps));
            AddDialog(new TextPrompt(nameof(TextPrompt)));

            InitialDialogId = nameof(WaterfallDialog);
        }

Sending the user to these dialogs based on the selection from MainDialog

In the last part, we have captured the user input and now we will send the user to their respective dialogs based on the selection. Use the control flow as below in the ActStepAsync method in MainDialog.

private async Task<DialogTurnResult> ActStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
        {
            stepContext.Values["UserSelection"] = ((FoundChoice)stepContext.Result).Value;
            await stepContext.Context.SendActivityAsync(MessageFactory.Text("You have selected " + (string)stepContext.Values["UserSelection"]), cancellationToken);

            string userSelection = (string)stepContext.Values["UserSelection"];

            if (userSelection.Equals("Track Orders"))
            {
                return await stepContext.BeginDialogAsync(nameof(TrackOrderDialog), new UserProfile(), cancellationToken);
            }
            else if (userSelection.Equals("Raise Complaints")) {
                return await stepContext.BeginDialogAsync(nameof(RaiseComplaintDialog), new UserProfile(), cancellationToken);
            }
            else if (userSelection.Equals("Get Bill/Invoice"))
            {
                return await stepContext.BeginDialogAsync(nameof(GetBillsDialog), new UserProfile(), cancellationToken);
            }
            else
            {
                return await stepContext.BeginDialogAsync(nameof(CancelOrderDialog), new UserProfile(), cancellationToken);
            }
            
        }

Add a new empty class file UserProfile.cs to store user data if required. Also, define the sub-dialogs in the MainDialog constructor.

public MainDialog(ILogger<MainDialog> logger)
            : base(nameof(MainDialog))
        {
            
            Logger = logger;

            AddDialog(new TextPrompt(nameof(TextPrompt)));
            AddDialog(new ChoicePrompt(nameof(ChoicePrompt)));
            AddDialog(new TrackOrderDialog());
            AddDialog(new RaiseComplaintDialog());
            AddDialog(new GetBillsDialog());
            AddDialog(new CancelOrderDialog());
            AddDialog(new WaterfallDialog(nameof(WaterfallDialog), new WaterfallStep[]
            {
                IntroStepAsync,
                ActStepAsync,
                FinalStepAsync,
            }));

            // The initial child Dialog to run.
            InitialDialogId = nameof(WaterfallDialog);
        }

If you run this project now, you will see that, there is no change in the output from our last part. Because we haven’t implemented the steps in the Sub Dialogs, that is why control is going to the final step after entering their respective classes.

We will start coding the sub dialogs from the next part.

Thank you All!!! Hope you find this useful.

If you liked our content and it was helpful, you can buy us a coffee or a pizza. Thank you so much.


Leave a Reply

Up ↑

%d bloggers like this: