Skip to content

Instantly share code, notes, and snippets.

@beachside-project
Created July 9, 2016 09:50
Show Gist options
  • Save beachside-project/802626a959030a65f6bb4aa268abb7d8 to your computer and use it in GitHub Desktop.
Save beachside-project/802626a959030a65f6bb4aa268abb7d8 to your computer and use it in GitHub Desktop.
SandwichOrder2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Builder.FormFlow;
using Microsoft.Bot.Builder.FormFlow.Advanced;
namespace FormFlow1.Dialogs
{
[Serializable]
[Template(TemplateUsage.None)]
[Template(TemplateUsage.NotUnderstood, "\"{0}\"は、理解できません。", "いやいや、\"{0}\"は知らんて。")]
[Template(TemplateUsage.EnumSelectOne, "どの{&}にしますか? {||}")]
public class SandwichOrder
{
#region public variables
[Describe("サンドイッチ")]
public SandwichOptions? Sandwich;
public LengthOptions? Length;
[Describe("パン")]
public BreadOptions? Bread;
[Optional]
public CheeseOptions? Cheese;
[Describe("トッピング")]
[Terms("except", "but", "not", "no", "all", "everything")]
public List<ToppingOptions> Toppings;
public List<SauceOptions> Sauces;
[Numeric(0, 5)]
[Optional]
[Describe("your experience today")]
public double? Rating;
[Optional]
[Template(TemplateUsage.NoPreference, "None")]
[Template(TemplateUsage.EnumSelectOne, "無料で以下のサービスを追加できます。 {||}")]
public string Specials;
[Optional]
[Describe("ご希望の配達時間")]
public DateTime? DeliveryTime;
[Describe("配送先の住所")]
public string DeliveryAddress;
[Describe("ご連絡先の電話番号")]
[Pattern("(\\(\\d{3}\\))?\\s*\\d{3}(-|\\s*)\\d{4}")]
public string PhoneNumber;
#endregion
#region functions
public static IForm<SandwichOrder> BuildForm()
{
OnCompletionAsyncDelegate<SandwichOrder> processOrder = async (context, state) =>
{
await context.PostAsync("ご注文を承りました。デモなので配送することはありませんよ!");
};
return new FormBuilder<SandwichOrder>()
.Message("サンドイッチボットへようこそ♪")
.Field(nameof(Sandwich))
.Field(nameof(Length))
.Field(nameof(Bread))
.Field(nameof(Cheese))
.Field(nameof(Toppings),
validate: async (state, value) =>
{
var values = ((List<object>)value).OfType<ToppingOptions>();
var result = new ValidateResult() { IsValid = true, Value = values };
if (values != null && values.Contains(ToppingOptions.Everything))
{
result.Value = Enum.GetValues(typeof(ToppingOptions)).Cast<ToppingOptions>()
.Where(t => t != ToppingOptions.Everything && !values.Contains(t)).ToList();
}
return result;
})
.Message("{Toppings}を選択しました。")
.Field(nameof(Sauces))
.Field(new FieldReflector<SandwichOrder>(nameof(Specials))
.SetType(null)
.SetActive((state) => state.Length == LengthOptions.FootLong)
.SetDefine(async (stateBag, field) =>
{
field.AddDescription("cookie", "Free cookie")
.AddTerms("cookie", "cookie", "free cookie")
.AddDescription("drink", "Free large drink")
.AddTerms("drink", "drink", "free drink");
return true;
}))
.Confirm(async (state) =>
{
int cost = 0;
switch (state.Length)
{
case LengthOptions.SixInch: cost = 500; break;
case LengthOptions.FootLong: cost = 6500; break;
}
return new PromptAttribute($"合計{cost:C}円です。よろしいですか。");
})
.Field(nameof(DeliveryAddress),
validate: async (state, response) =>
{
var result = new ValidateResult { IsValid = true, Value = response };
var address = (response as string).Trim();
if (address.Length > 0 && (address[0] < '0' || address[0] > '9'))
{
result.Feedback = "住所は、アメリカの形式で、数字から入力してください。";
result.IsValid = false;
}
return result;
})
.Field(nameof(DeliveryTime), "何時に配達を希望されますか? {||}")
.Confirm("ヒャッフー!: {Length} {Sandwich} {Bread} {&Bread}に、 {[{Cheese} {Toppings} {Sauces}]} です。 {DeliveryAddress} {?配達希望時間 {DeliveryTime:t}}にお届けしますがよろしいですか?")
.AddRemainingFields()
.Message("ご注文あざっしたー♪")
.OnCompletion(processOrder)
.Build();
}
#endregion
}
#region enum
public enum SandwichOptions
{
[Describe("ボーイズラブトラブル")]
BLT,
[Describe("シュヴァルツヴェルダー・シンケン")]
BlackForestHam,
[Describe("バッファローチキン")]
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
{
[Terms("except", "but", "not", "no", "all", "everything")]
Everything = 1,
Avocado,
BananaPeppers,
Cucumbers,
GreenBellPeppers,
Jalapenos,
Lettuce,
Olives,
Pickles,
RedOnion,
Spinach,
Tomatoes
}
public enum SauceOptions
{
ChipotleSouthwest,
HoneyMustard,
LightMayonnaise,
RegularMayonnaise,
Mustard,
Oil,
Pepper,
Ranch,
SweetOnion,
Vinegar
}
#endregion
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment