ToDo Bot | Part 5 | Creating Sub Dialogs | Microsoft Bot Framework

This is the bot series on ToDo Bot where we will be creating a Chatbot to create, view, and delete tasks. In this part, we will be creating the sub dialogs and sending the user to these dialogs based on the selection.


  1. ToDo Bot | Part 1 | Setup the Development Environment | Microsoft Bot Framework
  2. ToDo Bot | Part 2 | Welcome Card | Adaptive Cards | Microsoft Bot Framework
  3. ToDo Bot | Part 3 | Planning and Designing the Bot | Microsoft Bot Framework
  4. ToDo Bot | Part 4 | Coding the Main Dialog | Microsoft Bot Framework


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 three new class files in the Operations folder with the name –

  1. CreateTaskDialog
  2. ViewTaskDialog
  3. DeleteTaskDialog

Make sure to extend the Component Dialog in all the three classes. Below is the example from CreateTaskDialog.

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

namespace ToDoBot.Dialogs.Operations
    public class CreateTaskDialog : ComponentDialog

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

public CreateTaskDialog() : base(nameof(CreateTaskDialog))
            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["Operation"] = ((FoundChoice)stepContext.Result).Value;
            string operation = (string)stepContext.Values["Operation"];
            await stepContext.Context.SendActivityAsync(MessageFactory.Text("You have selected - " + operation), cancellationToken);

            if ("Create Task".Equals(operation))
                return await stepContext.BeginDialogAsync(nameof(CreateTaskDialog), new User(), cancellationToken);
            else if ("View Task".Equals(operation))
                return await stepContext.BeginDialogAsync(nameof(ViewTaskDialog), new User(), cancellationToken);
            else if ("Delete Task".Equals(operation))
                return await stepContext.BeginDialogAsync(nameof(DeleteTaskDialog), new User(), cancellationToken);
                await stepContext.Context.SendActivityAsync(MessageFactory.Text("User Input not matched."), cancellationToken);
                return await stepContext.NextAsync(null, cancellationToken);

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

public MainDialog(ToDoLUISRecognizer luisRecognizer, ILogger<MainDialog> logger)
            : base(nameof(MainDialog))
            _luisRecognizer = luisRecognizer;
            Logger = logger;

            AddDialog(new TextPrompt(nameof(TextPrompt)));
            AddDialog(new ChoicePrompt(nameof(ChoicePrompt)));
            AddDialog(new CreateTaskDialog());
            AddDialog(new ViewTaskDialog());
            AddDialog(new DeleteTaskDialog());
            AddDialog(new WaterfallDialog(nameof(WaterfallDialog), new WaterfallStep[]

            // 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 next part.

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

Leave a Reply

Up ↑

%d bloggers like this: