Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
BotBuilder -Custom Dialog
internal static IDialog MakeRootDialog()
{
return new SandwichDialog(SandwichOrder.BuildForm);
}
public async Task<Message> Post([FromBody]Message message)
{
if (message.Type == "Message")
{
return await Conversation.SendAsync(message, MakeRootDialog);
}
else
{
return HandleSystemMessage(message);
}
}
[Serializable]
public class SandwichDialog : IDialog
{
private readonly BuildForm<SandwichOrder> SandwichOrderForm;
internal SandwichDialog(BuildForm<SandwichOrder> SandwichOrderForm)
{
this.SandwichOrderForm = SandwichOrderForm;
}
public async Task StartAsync(IDialogContext context)
{
context.Wait(MessageReceivedAsync);
}
public async Task MessageReceivedAsync(IDialogContext context, IAwaitable<Message> argument)
{
var message = await argument;
var sandwichForm = new FormDialog<SandwichOrder>(new SandwichOrder(), this.SandwichOrderForm, FormOptions.PromptInStart);
context.Call<SandwichOrder>(sandwichForm, FormComplete);
}
private async Task FormComplete(IDialogContext context, IAwaitable<SandwichOrder> result)
{
SandwichOrder order = null;
try
{
order = await result;
}
catch (OperationCanceledException)
{
await context.PostAsync("You canceled the form!");
return;
}
catch (Exception ex)
{
await context.PostAsync(ex.Message);
return;
}
if (order != null)
{
await context.PostAsync(order.ToString());
}
else
{
await context.PostAsync("Form returned empty response!");
}
context.Wait(MessageReceivedAsync);
}
}
using Microsoft.Bot.Builder.FormFlow;
using System;
using System.Collections.Generic;
using System.Text;
#pragma warning disable 649
// 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 Bot_Application1
{
public enum SandwichOptions
{
BLT, BlackForestHam, BuffaloChicken, ChickenAndBaconRanchMelt, ColdCutCombo, MeatballMarinara,
OverRoastedChicken, 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]
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();
}
public override string ToString()
{
var builder = new StringBuilder();
builder.Append("You have place an order!!");
return builder.ToString();
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment