Bridge Communications

Saturday, November 19, 2016

Building a Bot with Microsoft Bot Framework - Part IV

As you would quickly discover, trying to write a bot to react to random queries using if..then, or case statements can quickly spiral out of control, and leave your users put off, and your dev team hiding under their desks.  In my opinion bots are best when they are task specific.  To that end, Microsoft has a neat way to interact with a user on a specific task, when there are a finite number of options.

The Sandwich Bot

Microsoft's example of bot to take a sandwich order is a perfect example of how to use forms.  There are only a finite number of things you can put on a sandwich from a given restaurant so it makes sense to build a form to guide a user through the order.  The way this works is it takes a serialized class and basically presents a user with a series of questions with clickable answers to keep them within the bounds of the service you offer.

Here is their example class;

using System;
using System.Collections.Generic;

// The SandwichOrder is the simple form you want to fill out. It must be serializable so the bot can be stateless.
// The order of fields defines the default order in which questions will be asked.
// Enumerations shows the legal options for each field in the SandwichOrder and the order is the order values will be presented
// in a conversation.
namespace Microsoft.Bot.Sample.SimpleSandwichBot
{
public enum SandwichOptions
{
BLT, BlackForestHam, BuffaloChicken, ChickenAndBaconRanchMelt, ColdCutCombo, MeatballMarinara,
OvenRoastedChicken, RoastBeef, RotisserieStyleChicken, SpicyItalian, SteakAndCheese, SweetOnionTeriyaki, Tuna,
TurkeyBreast, Veggie
};
public enum LengthOptions { SixInch, FootLong };
public enum BreadOptions { NineGrainWheat, NineGrainHoneyOat, Italian, ItalianHerbsAndCheese, Flatbread };
public enum CheeseOptions { American, MontereyCheddar, Pepperjack };
public enum ToppingOptions
{
Avocado, BananaPeppers, Cucumbers, GreenBellPeppers, Jalapenos,
Lettuce, Olives, Pickles, RedOnion, Spinach, Tomatoes
};
public enum SauceOptions
{
ChipotleSouthwest, HoneyMustard, LightMayonnaise, RegularMayonnaise,
Mustard, Oil, Pepper, Ranch, SweetOnion, Vinegar
};
[Serializable]
public class SandwichOrder
{
public SandwichOptions? Sandwich;
public LengthOptions? Length;
public BreadOptions? Bread;
public CheeseOptions? Cheese;
public List<ToppingOptions> Toppings;
public List<SauceOptions> Sauce;
public static IForm<SandwichOrder> BuildForm()
{
return new FormBuilder<SandwichOrder>()
.Message("Welcome to the simple sandwich order bot!")
.Build();
}
};
}


Showing the form on a message

internal static IDialog<SandwichOrder> MakeRootDialog()
{
return Chain.From(() => FormDialog.FromForm(SandwichOrder.BuildForm));
}
[ResponseType(typeof(void))]
public virtual async Task<HttpResponseMessage> Post([FromBody] Activity activity)
{
if (activity != null)
{
// one of these will have an interface and process it
switch (activity.GetActivityType())
{
case ActivityTypes.Message:
await Conversation.SendAsync(activity, MakeRootDialog);
break;
case ActivityTypes.ConversationUpdate:
case ActivityTypes.ContactRelationUpdate:
case ActivityTypes.Typing:
case ActivityTypes.DeleteUserData:
default:
Trace.TraceError($"Unknown activity type ignored: {activity.GetActivityType()}");

The key here is how it makes your SandwichOrder class into a form using the FormDialog class.  As I mentioned before, you will get slightly different visual appearances between say a sms message, a web chat bot, and skype chat bot.  But the idea is the same, the bot framework makes a cool form that goes question by question and records the users input.

Key concept

Getting the user to work through selection choices is very cool, and huge time saver, but you need to remember to use last weeks blog to save the data, so when you get through the questions, you know which sandwich and what options the user has selected, then you can take them to a payment website, and deliver the sandwich.

Next week we are going to use some machine learning and show you how to handle types of free text users questions.  For this we will employee Microsoft LUIS.


Doug Routledge, C# Lync, Skype for Business, SQL, Exchange, UC, 
Full Stack Developer  BridgeOC Bridge Operator Console
Twitter - @droutledge @ndbridge



No comments:

Post a Comment