Basic FormFlow Design Using Microsoft Bot FrameWork

In this article you will learn about Basic FormFlow Design Using Microsoft Bot FrameWork.

Introduction

In this article I will explain about FormFlow using bot builder SDK for .NET. Using Microsoft bot framework with FormFlow designing we can have a conversation with the user for getting the information from them. It means that the bot will ask the questions and gather the responses from the user. It automatically generates the dialogs that are necessary for managing the conversation based on the guidelines that we specify. The FormFlow is different from the LuisDialog. FormFlow guides for completing the form, whereas the LuisDialog evaluates the input.

Prerequisites

  • Visual studio 2017
  • Microsoft bot framework.

Creating the FormFlow project

Step 1

Open Visual Studio 2017, click->File->New->Project.

Step 2

Select the bot template and provide a name for the project as per your desire and click->ok.

Step 3

After the project is created, now we need to update the NuGetPackages, since the visual studio contains the basic NuGetPackages by default, we just need to update the packages. For updating the package, right click->project and select Manage NuGetPackages.

Step 4

Browse for NuGetPackage named “Microsoft.Bot.Builder” and select the NuGetPackage and update the package to latest version for proceeding to  further steps.

Step 5

After updating the NuGetpackages, right click->project and select->Add->NewItem.

Step 6

Now we will create a class that contains the logic of FormFlow and provide a name for the class and click->Add.

Step 7

By creating a new class in the project, replace the c# class code in “form.cs” and save the file.

 
  1. using System;  
  2. using Microsoft.Bot.Builder.Dialogs;  
  3. using Microsoft.Bot.Builder.FormFlow;  
  4. namespace HelloFormFlowBot {  
  5.     [Serializable]  
  6.     public class ProfileForm {  
  7.         [Prompt("What is your first name? {||}")]  
  8.         public string FirstName;  
  9.         [Prompt("What is your last name? {||}")]  
  10.         public string LastName;  
  11.         [Prompt("What is your gender? {||}")]  
  12.         public Gender Gender;  
  13.         public static IForm < ProfileForm > BuildForm() {  
  14.             return new FormBuilder < ProfileForm > ().Message("Welcome to the profile bot!").OnCompletion(async(context, profileForm) => {  
  15.                 await context.PostAsync("Your profile is complete.");  
  16.             }).Build();  
  17.         }  
  18.     }  
  19.     [Serializable]  
  20.     public enum Gender {  
  21.         Male = 1, Female = 2  
  22.     };  
  23. }   

Step 8

So the code form.cs is indicated with the fields for gathering the FormFlow and the class in FormFlow and Gender enum states that it is serialized.

Step 9

Now open the MessagesController.cs file and add the following using statements for supporting the FormFlow file 

  1. using Microsoft.Bot.Builder.Dialogs;  
  2. using Microsoft.Bot.Builder.FormFlow;   

Step 10

After adding the using statements, we need to add the following MakeRootDialog method to MessagesController class for calling the form method in class. 

  1. internal static IDialog < ProfileForm > MakeRootDialog() {  
  2.     return Chain.From(() => FormDialog.FromForm(ProfileForm.BuildForm));  
  3. }   

Step 11

Now it involves  slightly altering the post method in MessagesController class in the following and saving the file

 
  1. public async Task < HttpResponseMessage > Post([FromBody] Activity activity) {  
  2.     // Detect if this is a Message activity  
  3.     if (activity.Type == ActivityTypes.Message) {  
  4.         // Call our FormFlow by calling MakeRootDialog  
  5.         await Conversation.SendAsync(activity, MakeRootDialog);  
  6.     } else {  
  7.         // This was not a Message activity  
  8.         HandleSystemMessage(activity);  
  9.     }  
  10.     // Return response  
  11.     var response = Request.CreateResponse(HttpStatusCode.OK);  
  12.     return response;  
  13. }  
 
 

Step 12

Debug the application using F5 and the port number with web address will be opened in browser.

Step 13

Start the BotFramework emulator and connect to the bot by adding the address with port number as indicated in web browser and don’t forget to add /api/messages at the end.

For example :http://your_bot name/api/messages. And click->connect.

Step 14

The bot will be connected and now we can type a message and hit “enter” for conversation with bot for filling the FormFlow. This article just explains about a basic converse with bot and filling the FormFlow this doesn’t mean this can save the responses from the user. I mean you need to fill again the FormFlow again and again.

In my next article I will explain about the FormFlow application that ask to fill the form for one time and displays the values received using bot state service, Thanks for reading and have a nice day.