DZone
AI Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > AI Zone > Microsoft Bot Framework: Forms Dialog From JSON Schema

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.

Ezequiel Reyno user avatar by
Ezequiel Reyno
·
Jun. 24, 17 · AI Zone · Tutorial
Like (4)
Save
Tweet
3.55K Views

Join the DZone community and get the full member experience.

Join For Free

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.

JSON Form (document) Schema Dialog (software) Framework

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

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • What Is URL Rewriting? | Java Servlets
  • The Evolution of Configuration Management: IaC vs. GitOps
  • Real-Time Supply Chain With Apache Kafka in the Food and Retail Industry
  • How to Optimize MySQL Queries for Speed and Performance

Comments

AI Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo