Introduction
The Bot Framework enables you to build bots that support different types of interactions with users. You can design conversations in your bot to be freeform. Your bot can also have more guided interactions where it provides the user choices or actions. The conversation can use simple text strings or more complex rich cards that contain text, images, and action buttons. And, you can add natural language interactions, which let your users interact with your bots in a natural and expressive way.
Bot Builder SDK introduced Form Flow, it will automatically generate the dialog's conversation based on your property and type that was specified on a class. Bot Dialog also is more powerful but when handling a conversation like a registration or ordering, it requires more effort to complete the process using bot dialog.
In this article, I will help you to understand how to use Bot FormFlow and how you can use it to collect information from the users. We are creating a sample demo Bot for a bus booking bot.
Prerequisite
I have already explained about Bot framework Installation, deployment, and implementation in the below articles.
- Getting Started with Chatbot Using Azure Bot Service
- Getting Started with Bots Using Visual Studio 2017
- Deploying A Bot to Azure Using Visual Studio 2017
- How to Create ChatBot In Xamarin
- Getting Started with Dialog Using Microsoft Bot Framework
- Getting Started with Prompt Dialog Using Microsoft Bot Framework
Create New Bot Service
Let’s create a new Bot Service application using Visual Studio 2017. Open Visual Studio, select File menu, and create a new Project (Ctrl + Shift +N). Here, select Bot application.
The Bot application template is created with all the components and all required NuGet references installed in the solution. Just add a new class for BusFormFlow to the project.
In this solution, we have two main classes - MessagesController.cs and BusFormFlow.
Create New FormFlow Class
We can start creating a class that represents our form and creates properties for each field. Form fields can be one of the following types.
- Enum
- List of Enum
- Integral – sbyte, byte, short, ushort, int, uint, long, ulong
- Floating point – float, double
- String
- DateTime
FormFlow will automatically validate the type based on user input and reply with the validation message.
- Create a new enum field for the point from where the bus starts from.
-
-
-
- public enum FromCity
- {
- Bangalore,Chennai,Madurai,Trichy,Coimbatore,Tanjore,pudukkottai
- }
The bot output looks like below.
- Create a new enum field for bus end point.
-
-
-
- public enum ToCity
- {
- Bangalore, Chennai, Madurai, Trichy, Coimbatore, Tanjore, Pudukkottai
- }
The bot output looks like below.
- Create a new enum for bus type.
-
-
-
- public enum BusType
- {
- AC, NonAC,Slepper, Siting
- }
The output looks like below.
- Create a new enum for gender.
-
-
-
- public enum Gender
- {
- Male,Female
- }
The output looks like below.
- Create a class for FormFlow with the [Serializable] attribute and create build form method for showing FormFlow.
- using Microsoft.Bot.Builder.FormFlow;
- using Microsoft.Bot.Builder.Dialogs;
- using System;
-
- namespace FormFlowBot.FormFlow
- {
- [Serializable]
- public class BusFormFlow
- {
-
-
-
- public FromCity? FromAddress;
- public ToCity? ToAddress;
- public DateTime? StartDate;
- public BusType? BusTypes;
- public Gender? SelectGender;
- public string Name;
- public int Age;
-
-
-
-
-
- public static IForm<BusFormFlow> BuildForm()
- {
- return new FormBuilder<BusFormFlow>()
- .Message("Welcome to the BotChat Bus Booking !")
- .OnCompletion(async (context, profileForm) =>
- {
- string message = "Your Bus booking Successfully Completed .You will get confirmation email and SMS .Thanks for using Chat Bot Bus Booking , Welcome Again !!! :)";
- await context.PostAsync(message);
- })
- .Build();
- }
-
- }
- }
Calling FormFlow
- The Messagescontroller class is added in the solution and adds the following MakeRootDialog from a Messagescontroller class.
- internal static IDialog<BusFormFlow> MakeRootDialog()
- {
- return Chain.From(() => FormDialog.FromForm(BusFormFlow.BuildForm));
- }
- Call MakeRootDialog from post method in MessagesController calss.
-
-
-
-
- public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
- {
- if (activity.Type == ActivityTypes.Message)
- {
- await Conversation.SendAsync(activity, MakeRootDialog);
-
-
- }
- else
- {
- HandleSystemMessage(activity);
- }
- var response = Request.CreateResponse(HttpStatusCode.OK);
- return response;
- }
Run Bot Application
The emulator is a desktop application that lets us test and debug our Bot on localhost. Now, you can click on "Run the application" in Visual Studio and execute in the browser.
Test application on Bot Emulator
You can follow the below steps for testing your bot application.
- Open Bot Emulator.
- Copy the above localhost URL and paste it in emulator e.g. - http://localHost:3979
- You can append the /api/messages in the above URL; e.g. - http://localHost:3979/api/messages.
- You won't need to specify Microsoft App ID and Microsoft App Password for localhost testing. So, click on "Connect".
Summary
In this article, we learned how to use FormFlow and collect information from the users. If you have any questions/ feedback/ issues, please write in the comment box.