Những tính năng nâng cao của FormFlow - Advanced features of FormFlow
Bài Basic features of FormFlow mô tả việc hiện thực FormFlow và tạo ra một trải nghiệm cơ bản cho người dùng. Để tạo ra user experience theo cách tuỳ chỉnh hơn bằng việc sử dụng Form Flow, bạn có thể chỉ rõ trạng thái ban đầu của form, thêm các bussiness logic để quản lý những interdependencies giữa các field và quá trình lúc người dùng nhập thông tin, và sử dụng attributes để tuỳ chỉnh các prompts, override templates, tạo các optional fileds, match user input, và validate user input.
Specify initial form state and entities
Khi bạn chạy một FormDialog, bạn có thể (optional) truyền vào một instance của state của bạn. Nếu bạn truyền vào một instance của state của bạn, thì mặc định, FormFlow sẽ bỏ qua các bước mà các fields đã chứa giá trị; người dùng sẽ không nhận được các prompts cho các fields đó. Để bắt form hiển thị prompt cho tất các các fields (kể cả các fields đã có giá trị trong initial state), thì ta truyền vào FormOptions.PromptFieldsWithValues khi bạn chạy FormDialog. Nếu một field chứa một inital value, prompt sẽ sử dụng giá trị đó cho default value.
Bạn cũng có thể truyền vào LUIS entities để liên kết với state. Nếu EntityRecommendation.Type là một đường dẫn đến một field trong C# class của bạn, EntityRecommendation.Entity sẽ được truyền thông qua recognizer để liên kết với field của bạn. FormFlow sẽ bỏ qua bước này cho mọi field mà được ràng buộc với entity; người dùng sẽ không được hiển thị prompt cho các field đó.
Add business logic
Để handle interdependencies giữa form field hoặc áp dụng các logic cụ thể trong quá trình getting hoặc setting một field value, bạn có thể chỉ rõ business logic trong một validation function. Một validation function cho bạn điều khiển state và return một ValidateResul object, ValidateResult có thể chứa:
- Một chuỗi feedback mà mô tả nguyên nhân mà value đó không hợp lệ
- Một transformed value
- Một tập các lựa chọn cho việc làm rõ value
Đoạn code ví dụ sau cho thấy một validation function cho Topping field. Nếu input cho field này chứa ToppingOptions.Everything enumeration value, validation function đảm bảo rằng Topping field value chứa full list toppings.
public static IForm<sandwichorder> BuildForm()
{
...
.Message("Welcome to the sandwich order bot!")
return new FormBuilder<sandwichorder>()
.Field(nameof(Length))
.Field(nameof(Sandwich))
.Field(nameof(Bread))
validate: async (state, value) =>
.Field(nameof(Cheese))
.Field(nameof(Toppings),
{
var result = new ValidateResult { IsValid = true, Value = values };
var values = ((List<object>)value).OfType<toppingoptions>();
result.Value = (from ToppingOptions topping in Enum.GetValues(typeof(ToppingOptions))
if (values != null && values.Contains(ToppingOptions.Everything))
{
select topping).ToList();
where topping != ToppingOptions.Everything && !values.Contains(topping)
}
return result;
})
}
.Message("For sandwich toppings you have selected {Toppings}.")
...
.Build();
Ngoài ra với validation function, bạn có thể thêm Term attribute để match user expressions như "everything" hoặc "not".
public enum ToppingOptions{// This starts at 1 because 0 is the "no value" value[Terms("except", "but", "not", "no", "all", "everything")]Everything = 1, ...}
Sử dụng validation function ở trên, đoạn mã này cho thấy sự tương tác giữa bot và người dùng khi người dùng gửi yêu cầu "everything ngoại trừ Jalapenos".
Please select one or more toppings (current choice: No Preference)1. Everything 2. Avocado 3. Banana Peppers 4. Cucumbers9. Pickles5. Green Bell Peppers 6. Jalapenos 7. Lettuce 8. Olives 10. Red OnionFor sandwich toppings you have selected Avocado, Banana Peppers, Cucumbers, Green Bell Peppers, Lettuce, Olives, Pickles, Red Onion, Spinach, and Tomatoes.11. Spinach 12. Tomatoes > everything but jalapenos
FormFlow attributes
Bạn có thể thêm những C# attribute vào class của bạn để customize hành vì của một FormFlow dialog.Attribute | Purpose |
---|---|
Describe | Thay đổi cách mà một field hoặc một value được hiển thị trong một template hoặc card |
Numeric | Hạn chế số lượng của các numeric field được chấp nhận. |
Optional | Đánh dấu một filed là optional |
Pattern | Định nghĩa một regular expression để validate một string field |
Prompt | Định nghĩa prompt cho một field |
Template | Định nghĩa template để sử dụng cho việc tạo prompts hoặc values trong prompts |
Terms | Định nghĩa input terms cho field hoặc value |
Customize prompts using the Prompt attribute
Default prompts được tạo tự động cho mối field trong form của bạn, nhưng bạn có thể chỉ định một custom prompt cho mọi field sử dụng Prompt attribute. Ví dụ, nếu default prompt cho SandwichOrder.Sandwich field là "Please select một sandwich", bạn có thể thêm Prompt attribute để chỉ rõ một custom prompt cho field đó.[Prompt("What kind of {&} would you like? {||}")]
public SandwichOptions? Sandwich;
Ví dụ trên sử dụng pattern language để tạo prompt một cách "động" với form data tại runtime:
{&} được thay thế với description của field và {||} được thay thế với list của các lựa chọn trong enumeration.
Mặc định, description của một field được tạo ra từ field name. Để chỉ định một custom description cho một field, thêm Description attribute.
Đoạn snippet sau cho thấy một customized prompt từ example ở trên.
What kind of sandwich would you like?
1. BLT
2. Black Forest Ham
3. Buffalo Chicken
4. Chicken And Bacon Ranch Melt
5. Cold Cut Combo
6. Meatball Marinara
7. Oven Roasted Chicken
8. Roast Beef
9. Rotisserie Style Chicken
10. Spicy Italian
11. Steak And Cheese
12. Sweet Onion Teriyaki
13. Tuna
14. Turkey Breast
15. Veggie
>
Một Prompt attribute có thể chỉ rõ parameters mà ảnh hưởng đến cách mà form hiển thị prompt. Ví dụ, ChoiceFormat parameter xác định cách mà form render danh sách các lựa chọn.
[Prompt("What kind of {&} would you like? {||}", ChoiceFormat="{1}")]
public SandwichOptions? Sandwich;
In this example, the value of the ChoiceFormat parameter indicates that the choices should be displayed as a bulleted list (instead of a numbered list).Trong ví dụ này, giá trị của ChoiceFormat parameter cho biết các lựa chọn nên được hiển thị theo kiểu gạch đầu dòng thay vì là danh sách đánh số.
What kind of sandwich would you like?
- BLT
- Black Forest Ham
- Buffalo Chicken
- Chicken And Bacon Ranch Melt
- Cold Cut Combo
- Meatball Marinara
- Oven Roasted Chicken
- Roast Beef
- Rotisserie Style Chicken
- Spicy Italian
- Steak And Cheese
- Sweet Onion Teriyaki
- Tuna
- Turkey Breast
- Veggie
>
Customize prompts using the Template attribute
Trong khi Prompt attribute cho phép bạn tuỳ chỉnh prompt cho một field, Template attribute cho phép bạn thay thế các default templates mà FormFlow sử dụng để tự động tạo các prompts. Đoạn code ví dụ sau sử dụng Template attribute để định nghĩa lại cách mà form handle tất cả các enumeration field. Attribute trong đoạn code cho biết người dùng chỉ được chọn một item, nó cũng thiết đặt prompt text bằng việc sử dụng pattern language, và chỉ rõ form nên hiển thị chỉ một item trong một dòng.
[Prompt("What kind of {&} would you like? {||}", ChoiceFormat="{1}")] public SandwichOptions? Sandwich;
What kind of bread would you like on your sandwich?
1. Nine Grain Wheat
2. Nine Grain Honey Oat
3. Italian
4. Italian Herbs And Cheese
5. Flatbread
>
What kind of cheese would you like on your sandwich?
1. American
2. Monterey Cheddar
3. Pepperjack
>
Nếu bạn sử dụng Template attribute để thay thế các default templates mà FormFlow sử dụng để tạo ra các prompts, bạn có thể muốn chèn vài biến thể vào prompts và messages mà form tạo ra. Để làm điều đó, bạn có thể định nghĩa các multiple text strings sử dụng pattern language, và form sẽ ngẫu nhiên chọn từ các option sẵn có mỗi lần nó cần hiển thị prompt hoặc message.
Đoạn code mẫu này định nghĩa lại TemplateUsage.NotUnderstood template để chỉ rõ là sẽ có hai biến thể của message. Khi bot cần giao tiếp mà nó không hiểu user input, nó sẽ xác định message content bằng việc chọn ngẫu nhiên một trong hai text string đó.
[Template(TemplateUsage.NotUnderstood, "I do not understand \"{0}\".", "Try again, I don't get \"{0}\".")] [Template(TemplateUsage.EnumSelectOne, "What kind of {&} would you like on your sandwich? {||}")] public class SandwichOrderĐoạn snippet sau là một ví dụ cho việc giao tiếp giữa bot và người dùng
What size of sandwich do you want? (1. Six Inch, 2. Foot Long) > two feet I do not understand "two feet". > two feet Try again, I don't get "two feet" >
Designate a field as optional using the Optional attribute
Để chỉ định một field là optional, sử dụng Optional attribute. Đoạn code sau chỉ định field Chese là optional.[Optional] public CheeseOptions? Cheese;Nếu một field là optional và không có giá trị, current choice sẽ được hiển thị là "No preference".
What kind of cheese would you like on your sandwich? (current choice: No Preference) 1. American 2. Monterey Cheddar 3. Pepperjack >Nếu một field là optional và người dùng đã đặt giá trị cho nó, "No Preference" sẽ được hiển thị là lựa chọn cuối cùng trong list.
What kind of cheese would you like on your sandwich? (current choice: American)
1. American
2. Monterey Cheddar
3. Pepperjack
4. No Preference
>
Match user input using the Terms attribute
Khi người dùng gửi một message đến bot mà được xây dựng sử dụng FormFlow, bot sẽ cố gắng xác định ý nghĩa của user input bằng việc matching input với một list các term. Mặc định, list of term được xây dựng bằng việc áp dụng các bước sau cho field hoặc value:- Loại bỏ các chữ hoa và underscore (_).
- Tạo mỗi n-gram lên đến độ dài tối đa.
Thêm "s?" vào cuối mỗi từ (để hỗ trợ số nhiều).
Ví dụ, value "AngusBeefAndGarlicPizza" sẽ tạo ra các term sau:
'angus?'
'beefs?'
'garlics?'
'pizzas?'
'angus? beefs?'
'garlics? pizzas?'
'angus beef and garlic pizza'
Để override các hành vi mặc định này và định nghĩa list of term mà được sử dụng để match user input đến một field hoặc một value trong field, sử dụng Term attribute. Ví dụ, bạn có thể sử dụng Term attribute (với một regular expression) để tính đến việc người dùng có thể gõ sai chính tả từ "rotisserie".
[Terms(@"rotis\w* style chicken", MaxPhrase = 3)]
RotisserieStyleChicken, SpicyItalian, SteakAndCheese, SweetOnionTeriyaki, Tuna,...
Bằng việc sử dụng Term attribute, bạn tăng khả năng cho việc match user input với mội lựa chọn đúng. Parameter Terms.MaxPhrase trong ví dụ này khiến cho Langiage.GenerateTerrms tạo ra các terms biến thể khác.
Đoạn snippet sau cho thấy kết quả của việc tương tác giữa bot và user khi user viết sai từ "Rotisserie".
What kind of sandwich would you like? 1. BLT 2. Black Forest Ham 3. Buffalo Chicken 4. Chicken And Bacon Ranch Melt 5. Cold Cut Combo 6. Meatball Marinara 7. Oven Roasted Chicken 8. Roast Beef 9. Rotisserie Style Chicken 10. Spicy Italian 11. Steak And Cheese 12. Sweet Onion Teriyaki 13. Tuna 14. Turkey Breast 15. Veggie > rotissary checkin For sandwich I understood Rotisserie Style Chicken. "checkin" is not an option.
Nhận xét
Đăng nhận xét