Last active
February 22, 2017 11:53
-
-
Save beachside-project/07c265b8f07e6eb330be56a46e479c73 to your computer and use it in GitHub Desktop.
FormFlow-Using the Form Builder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using Microsoft.Bot.Builder.Dialogs; | |
using Microsoft.Bot.Builder.FormFlow; | |
using Microsoft.Bot.Builder.FormFlow.Advanced; | |
namespace FormFlowV3.Dialogs | |
{ | |
[Serializable] | |
[Template(TemplateUsage.NotUnderstood, "\"{0}\"の入力は不正です。選択肢から選んでください。", "\"{0}\"なんて知らんで。ちゃんと入力して。")] | |
[Template(TemplateUsage.EnumSelectOne, "どの{&}にしますか?{||}", ChoiceStyle = ChoiceStyleOptions.PerLine)] | |
public class SandwichOrder | |
{ | |
private const string NoPreference = "指定なし"; | |
#region public variables | |
[Describe("サンドイッチ")] | |
public SandwichOptions? Sandwich; | |
public LengthOptions? Length; | |
[Describe("パン")] | |
public BreadOptions? Bread; | |
[Optional] | |
[Template(TemplateUsage.NoPreference, NoPreference)] | |
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{||}")] | |
[Template(TemplateUsage.NoPreference, NoPreference)] | |
public double? Rating; | |
[Optional] | |
[Template(TemplateUsage.NoPreference, NoPreference)] | |
[Template(TemplateUsage.EnumSelectOne, "無料で以下のサービスを追加できます。 {||}")] | |
public string Specials; | |
[Optional] | |
[Describe("ご希望の配達時間")] | |
[Template(TemplateUsage.NoPreference, NoPreference)] | |
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
hello , can you help me please how to call another method after processOrder completed ?