In my previous post, we looked into creating our first Bot using the Microsoft Bot Framework and tested it using the Bot Emulator. We have also seen what the solution looks like and what a dialog is. Now, we will take a deeper dive into Dialogs and create our own custom dialog.

Getting Started

Using our previous solution, let us try to create a bot that does simple arithmetic operations—addition for instance. Visualizing how the conversation will go, it will be represented with the flow indicated below:

Bot Framework: 02 - Dialogs

Let’s implement this flow in the code.

  1. Create the Dialog
    • Right-click the Dialogs folder.
    • Select Add > Class.
    • Name it ArithmeticDialog.cs.
    • Add the [Serializable] attribute on the class.
    • Implement the IDialog interface.
    • Implement the StartAsync method.
    • Your class should look like the one below:

  1. Next, we need to call this dialog from the MessagesController.
    • Go to the MessagesController.cs.
    • Replace the RootDialog with ArithmeticDialog on the Conversation.SendAsync() method.

  1. Right now, the ArithmeticDialog doesn’t do anything so let us modify the code.
    • Paste the code below:

    • context.Wait<Activity>(MessageReceivedAsync) defines which method will be used when the next message comes in. Once a reply has been received, it calls the MessageReceivedAsync method.
    • PromptDialog is a class under the Dialogs namespace that allows for replies to the user. In this case, PromptDialog.Text() is used. Aside from the text method, the following are available for consumption:
      • Attachment
      • Choice
      • Confirm
      • Number
    • Press F5 to run the bot
  1. Let’s test the bot by going through the conversation:

 

And with that, you have created your own custom dialog which handles a simple addition logic. Of course, in a real-life scenario, your bot should be able to handle more than 1 dialogs. In my next post, we will learn how to chain dialogs.