In the previous articles, I showed how to create a new chatbot using the Microsoft Bot Framework and I explained the relevant code in the sample chatbot you created.
In this article, I will show how to extend your bot by adding a new dialog.
Launch Visual Studio and create a new project based on the "Bot Builder Echo Bot", as shown in Fig. 1
From the Visual Studio menu, select Build | Rebuild Solution.
In the Visual Studio Solution Explorer, right-click the "Dialogs" folder and select Add | Class from the context menu, as shown in Fig. 2.
A dialog displays, allowing you to name and add a new class, as shown in Fig. 4.
Enter a name for this dialog class. I chose "TimeDialog.cs", because I plan to add code to display the current time. Click the [Add] button to create this class.
An editor with the default code in it displays. The default code is in Listing 1.
Above the class declaration, add the [Serializable] tag.
After the class name, add the following to derive from the IDialog interface.
The IDialog interface requires one method: StartAsync. Add this method as shown in Listing 2.
Notice that StartAsync asynchronously calls MessageReceivedAsync, so you will have to add this method, as shown in Listing 3.
Note that the output sent back to the channel is a string containing the current time.
Finally, you will need to add using statements at the top to resolve any errors. Add the following to the top of the file.
When you are finished, the class should look like Listing 5 below.
Next, modify the controller to point to your new Dialog. Open MessageController.cs and find the following line of code.
await Conversation.SendAsync(activity, () => new Dialogs.RootDialog());
Replace this line with the following:
await Conversation.SendAsync(activity, () => new Dialogs.TimeDialog());
Connect to your Bot from the Emulator and enter any text in the "Type your message" textbox and press ENTER.
The bot should respond by displaying the current date and time, as shown in Fig. 3.
In this article, I showed how to create and use a new Dialog class to an Bot Builder Echo Bot project. You can find this code in the NewDialogDemo of the Bot-Framework-Demos on my GitHub page.