Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Microsoft Bot Framework: Forms Dialog From JSON Schema

DZone's Guide to

Microsoft Bot Framework: Forms Dialog From JSON Schema

The Form Dialog feature of Microsoft Bot Features creates a form for your bot to ask questions from field-by-field until it completes the form by providing a JSON Schema.

· AI Zone
Free Resource

I've already written few posts about Microsoft Bot Framework. If you missed them, you can check out my posts here. Today, we are going deeper and reviewing another really good feature of Form Dialog. It involves the possibility to create a form using a JObject. As before, Form Dialog is going to create a form and allow our bot to ask field-by-field until it completes the form but instead of using a static C# class to define our form we are going to provide a JSON Schema.

In order to utilize this feature, you need to ensure that you add the NuGet project Microsoft.Bot.Builder.FormFlow.Json to your project. This defines the new namespace Microsoft.Bot.Builder.FormFlow.Json that contains the code to allow using JSON Schema for FormFlow.

Creating the JSON Schema

Now we need to define our form. This time, we are going to create a JSON file to do it. In the References property, we are going to define all the dependencies for our form. In the same way, under the Imports property, we define all the namespaces to include. Another important property for our form is the OnCompletion property. Here, we are going to put a C# script to execute after our bot completes to fulfill the form. Then, we have the properties field where we are going to place the fields we want our bot to ask the customer.

{ 
  "References": [ "CityTimerBot.dll" ], 
  "Imports": [ "CityTimerBot.Models" ], 
  "type": "object", 
  "required": [ "SelectedPlace" ], 
  "Templates": { 
    "NotUnderstood": { 
      "Patterns": [ "I do not understand \"{0}\".", "Try again, I don't get \"{0}\"." ] 
    } 
  }, 
  "properties": { 
    "PlaceName": { 
      "Prompt": { 
        "Patterns": [ "Name a place to know the current date-time {||}" ] 
      }, 
      "Before": [ { 
        "Message": [ "Welcome to the City Timer bot!" ] 
      } ], 
      "Describe": "Name of the place", 
      "type": [ "string", "null" ] 
    }, 
    "SelectedPlace": { 
      "Prompt": { 
        "Patterns": [ "Select a place {||}" ] 
      }, 
      "Before": [ { 
        "Message": [ "Welcome to the City Timer bot!" ] 
      } ], 
      "Describe": "Place to find the current date time", 
      "type": [ "string", "null" ] 
    } 
  }, 
  "OnCompletion": "var businesLogic = new CityTimerBot.Models.LocationBL(); var response = businesLogic.GetCityInfoFromPlaceId(state.SelectedPlace);var reply = string.Empty;if (!string.IsNullOrEmpty(response.cityName) && !string.IsNullOrEmpty(response.convertedLocalTime)){ reply = $\"Current date time at {response.cityName} is {response.convertedLocalTime}\"; }else{ reply = $\"Sorry we could not find any location called {state.PlaceName}\"; } await context.PostAsync(reply);" 
}

Once we have defined the schema file, we need to create a method to return an object which implements the IForm interface as we did last time. As you can see in the code below, we need to use the FormBuilderJson object and we just pass the schema in the constructor.

public static IForm; BuildJsonForm() { 
using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("CityTimerBot.LocationTemplate.json")) 
{ 
    var schema = JObject.Parse(new StreamReader(stream).ReadToEnd()); 
      return new FormBuilderJson(schema) .AddRemainingFields() .Build(); 
    } 
}

As you can see, this feature gives us the flexibility to define custom forms and the availability to change it dynamically. Thinking out loud, I can imagine a use case where we want to provide to our customers with a way to model their forms so we can define the form as a JSON Schema and provide them with an admin screen to change it.

bot_fields

For mode details about the Microsoft Bot Framework, you can use this link.

If you found this post useful, please don't forget to press the like button and share it. If you are in doubt, don't hesitate to ask a question and, as always, thank you for reading.

Topics:
microsoft bot framework ,bot development ,ai ,tutorial ,json schema

Published at DZone with permission of Ezequiel Reyno. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}