Những tính năng cơ bản của FormFlow - Basic features of FormFlow
Dialogs đã rất mạnh mẽ và linh hoạt, nhưng việc xử lý một conversation theo khuôn mẫu như mua một chiếc bánh sandwich cần rất nhiều nỗ lực. Tại mỗi điểm của conversation, có rất nhiều tình huống có thể xảy ra tiếp theo. Ví dụ, bạn có thể cần phải làm rõ những mơ hồ, cung cấp sự hỗ trợ, quay lại hoặc show ra quá trình. Bằng việc sử dụng FormFlow trong Bot Builder SDK cho .NET, bạn có thể đơn giản hoá quá trình quản lý một conversation theo khuôn mẫu như vậy.
Bài viết này được dịch từ bài cùng tên tại https://docs.microsoft.com/en-us/azure/bot-service/dotnet/bot-builder-dotnet-formflow.
FormFlow tự động tạo ra các dialogs cần thiết để quản lý một conversation theo khuôn mẫu, dựa trên các guidelines ở trên mà bạn chỉ ra. Mặc dù sử dụng FormFlow sẽ hy sinh một số sự linh hoạt mà bạn có thể có bằng việc tạo và quản lý dialogs thủ công, thiết kế một guided conversation sử dụng FormFlow có thể giảm đáng kể thời gian cần để phát triển bot của bạn. Ngoài ra, bạn có thể xây dựng bot của bạn bằng cách kết hợp FormFlow và dialogs khác. Ví dụ, FormFlow dialog có thể hướng dẫn người dùng thông qua các process để hoàn thành một form, trong khi LuisDialog có thể phân tích user input thông qua intent.
Bài này mô tả cách tạo một bot sử dụng các tính năng cơ bản của FormFlow để collect thông tin của user.
Forms and fields
Để tạo một bot sử dụng FormFlow, bạn phải chỉ định thông tin mà bot cần để thu thập từ user. Ví dụ, nếu mục tiêu là để có sandwich order, thì bạn phải định nghĩa một form mà chứa các trường dữ liệu mà bot cần để điền vào order. Bạn có thể định nghĩa một form bằng việc tạo một C# class chứa một hoặc nhiều các properties mà biểu diễn dữ liệu mà bot sẽ thu thập từ user. Mỗi property phải là một trong các loại data types sau:
Integral (sbyte, byte, short, ushort, int, uint, long, ulong)
Floating point (float, double)
String
DateTime
Enumeration
List of enumerations
Mọi data type có thể là nullable, nghĩa là bạn có thể định nghĩa là field đó có giá trị hoặc không. Nếu một form field dựa trên enumeration property mà không nullable, giá trị 0 trong enumeration biểu diễn null (i.e., indicates that the field does not have a value), và bạn nên bắt đầu enumeration value tại 1. FormFlow bỏ qua tất cả các data type khác và các methods.
Với các object phức tạp, bạn phải tạo một form cho top-level C# class và các form các cho complex object đó. Bạn có thể kết hợp các form với nhau sử dụng các dialog ngữ nghĩa điển hình. Điều này cũng có thể được cho việc định nghĩa một form trực tiếp bằng việc implement Advance.IField hoặc sử dụng Advance.Field và tạo các từ điển trong nó.
Để sử dụng FormFlow, bạn phải import Microsoft.Bot.Builder.FormFlow namespace.
Nếu user input không trùng với bất kỳ lựa chọn nào, bot sẽ tự động hỏi lại user để làm rõ lựa chọn.
Nếu user input chỉ định nhiều lựa chọn cho một propery và bot không hiểu bất kỳ một lựa chọn nào được chỉ định, nó sẽ hỏi lại để làm rõ lựa chọn.
Nếu user phản hồi bằng việc nhập "no" bot cho phép user cập nhật bất kỳ lựa chọn nào trước đó. Nếu user phản hồi bằng việc nhập "yes", form sẽ được hoàn thành và control được trở lại dialog mà call form.
Simple sandwich bot
Xét ví dụ đơn giản sau về sandwich bot được thiết kế để lấy dữ liệu về order sandwich của người dùng.Create the form
SandwichOrder class định nghĩa form và enumerations định nghĩa các option để xây dựng một sandwich. Class này cũng chứa một static method là BuildForm mà sử dụng FormBuilder để tạo form và định nghĩa một welcome message đơn giản.Để sử dụng FormFlow, bạn phải import Microsoft.Bot.Builder.FormFlow namespace.
using Microsoft.Bot.Builder.FormFlow; using System; using System.Collections.Generic; // The SandwichOrder class represents the form that you want to complete // using information that is collected from the user. // It must be serializable so the bot can be stateless. // The order of fields defines the default sequence in which the user is asked questions. // The enumerations define the valid options for each field in SandwichOrder, and the order // of the values represents the sequence in which they are presented to the user 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 ListToppings; public List Sauce; public static IForm BuildForm() { return new FormBuilder () .Message("Welcome to the simple sandwich order bot!") .Build(); } }; }
Connect the form to the framework
Để connect form với framework, bạn phải add nó vào controller. Trong ví dụ này, Conversation.SendAsync method gọi static method MakeRootDialog, MakeRootDialog sẽ lần lượt gọi FormDialog.FormFrom method để tạo SandwichOrder form.internal static IDialogMakeRootDialog() { return Chain.From(() => FormDialog.FromForm(SandwichOrder.BuildForm)); } [ResponseType(typeof(void))] public virtual async Task Post([FromBody] Activity activity) { if (activity != null) { 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()}"); break; } } ... }
See it in action
Chỉ bằng việc định nghĩa form với một C# class và connect nó với framework, bạn có thể cho phép FormFlow tự động quản lý conversation giữa bot và user. Ví dụ tương tác được show bên dưới demo khả năng của bot mà được tạo bởi việc sử dụng tính năng cơ bản của FormFlow. Trong mỗi lần tương tác, một dấu ">" cho biết nơi mà user nhập vào phản hồi.Please select a sandwich 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 >
Provide guidance
Người dùng có thể gõ "help" bất cứ lúc nào trong conversation để nhận hướng dẫn trong việc điền vào form. Ví dụ, nếu user nhập "help" tại sandwich prompt, bot sẽ gửi về hướng dẫn.> help * You are filling in the sandwich field. Possible responses: * You can enter a number 1-15 or words from the descriptions. (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, and Veggie) * Back: Go back to the previous question. * Help: Show the kinds of responses you can enter. * Quit: Quit the form without completing it. * Reset: Start over filling in the form. (With defaults from your previous entries.) * Status: Show your progress in filling in the form so far. * You can switch to another field by entering its name. (Sandwich, Length, Bread, Cheese, Toppings, and Sauce).
Advance to the next prompt
Nếu người dùng nhập "2" trong response để bắt đầu sandwich prompt, bot sẽ hiển thị một prompt cho property tiếp theo mà được định nghĩa bởi form SandwichOrder.Length.Please select a sandwich 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 > 2 Please select a length (1. Six Inch, 2. Foot Long) >
Return to the previous prompt
Nếu người dùng nhập "back" tại thời điểm này trong conversation, bot sẽ trở lại prompt trước. Prompt hiển thị lựa chọn hiện tại của người dùng, người dùng có thể thay đổi lựa chọn này bằng việc nhập một số khác hoặc confirm lựa chọn đó bằng việc nhập "c".> back Please select a sandwich(current choice: Black Forest Ham) 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 > c Please select a length (1. Six Inch, 2. Foot Long) >
Clarify user input
Nếu người dùng phản hồi với text thay vì số để cho biết lựa chọn của họ, bot sẽ hỏi lại để làm rõ nếu user input giống với nhiều hơn 1 lựa chọn.Please select a bread 1. Nine Grain Wheat 2. Nine Grain Honey Oat 3. Italian 4. Italian Herbs And Cheese 5. Flatbread > nine grain By "nine grain" bread did you mean (1. Nine Grain Honey Oat, 2. Nine Grain Wheat) > 1
Nếu user input không trùng với bất kỳ lựa chọn nào, bot sẽ tự động hỏi lại user để làm rõ lựa chọn.
Please select a cheese (1. American, 2. Monterey Cheddar, 3. Pepperjack) > amercan "amercan" is not a cheese option. > american smoked For cheese I understood American. "smoked" is not an option.
Nếu user input chỉ định nhiều lựa chọn cho một propery và bot không hiểu bất kỳ một lựa chọn nào được chỉ định, nó sẽ hỏi lại để làm rõ lựa chọn.
Please select one or more toppings 1. Banana Peppers 2. Cucumbers 3. Green Bell Peppers 4. Jalapenos 5. Lettuce 6. Olives 7. Pickles 8. Red Onion 9. Spinach 10. Tomatoes > peppers, lettuce and tomato By "peppers" toppings did you mean (1. Green Bell Peppers, 2. Banana Peppers) > 1
Show current status
Nếu user nhập "status" tại bất kỳ thời điểm nào trong quá trình order, bot sẽ reponse những giá trị đã được chỉ định và những giá trị còn lại cần được chỉ định.Please select one or more sauce 1. Honey Mustard 2. Light Mayonnaise 3. Regular Mayonnaise 4. Mustard 5. Oil 6. Pepper 7. Ranch 8. Sweet Onion 9. Vinegar > status * Sandwich: Black Forest Ham * Length: Six Inch * Bread: Nine Grain Honey Oat * Cheese: American * Toppings: Lettuce, Tomatoes, and Green Bell Peppers * Sauce: Unspecified
Confirm selections
Khi người dùng hoàn thành form, bot sẽ hỏi người dùng để xác nhận lựa chọn của họ.Please select one or more sauce 1. Honey Mustard 2. Light Mayonnaise 3. Regular Mayonnaise 4. Mustard 5. Oil 6. Pepper 7. Ranch 8. Sweet Onion 9. Vinegar > 1 Is this your selection? * Sandwich: Black Forest Ham * Length: Six Inch * Bread: Nine Grain Honey Oat * Cheese: American * Toppings: Lettuce, Tomatoes, and Green Bell Peppers * Sauce: Honey Mustard >
Nếu user phản hồi bằng việc nhập "no" bot cho phép user cập nhật bất kỳ lựa chọn nào trước đó. Nếu user phản hồi bằng việc nhập "yes", form sẽ được hoàn thành và control được trở lại dialog mà call form.
Is this your selection? * Sandwich: Black Forest Ham * Length: Six Inch * Bread: Nine Grain Honey Oat * Cheese: American * Toppings: Lettuce, Tomatoes, and Green Bell Peppers * Sauce: Honey Mustard > no What do you want to change? 1. Sandwich(Black Forest Ham) 2. Length(Six Inch) 3. Bread(Nine Grain Honey Oat) 4. Cheese(American) 5. Toppings(Lettuce, Tomatoes, and Green Bell Peppers) 6. Sauce(Honey Mustard) > 2 Please select a length (current choice: Six Inch) (1. Six Inch, 2. Foot Long) > 2 Is this your selection? * Sandwich: Black Forest Ham * Length: Foot Long * Bread: Nine Grain Honey Oat * Cheese: American * Toppings: Lettuce, Tomatoes, and Green Bell Peppers * Sauce: Honey Mustard > y
Handling quit and exceptions
Nếu user nhập "quit" trong form hoặc một exception xảy ra tại một vài điểm trong conversation, bot của bạn sẽ cần biết bước nào xảy ra lỗi, trạng thái của form khi xảy ra lỗi, và bước nào hoàn thành. Form return thông tin này thông qua FormCanceledException class. Đoạn code bên dưới thể hiện cách bắt exception và hiển thị message theo sự kiện xảy ra.internal static IDialogMakeRootDialog() { return Chain.From(() => FormDialog.FromForm(SandwichOrder.BuildLocalizedForm)) .Do(async (context, order) => { try { var completed = await order; // Actually process the sandwich order... await context.PostAsync("Processed your order!"); } catch (FormCanceledException e) { string reply; if (e.InnerException == null) { reply = $"You quit on {e.Last} -- maybe you can finish next time!"; } else { reply = "Sorry, I've had a short circuit. Please try again."; } await context.PostAsync(reply); } }); }
Summary
This article has described how to use the basic features of FormFlow to create a bot that can:
Automatically generate and manage the conversation
Provide clear guidance and help
Understand both numbers and textual entries
Provide feedback to the user regarding what is understood and what is not
Ask clarifying questions when necessary
Allow the user to navigate between steps
Although basic FormFlow functionality is sufficient in some cases, you should consider the potential benefits of incorporating some of the more advanced features of FormFlow into your bot. For more information, see Advanced features of FormFlow and Customize a form using FormBuilder.
Sample code
For complete samples that show how to implement FormFlow using the Bot Builder SDK for .NET, see the Multi-Dialog Bot sample and the Contoso Flowers Bot sample in GitHub.
Next steps
FormFlow simplifies dialog development. The advanced features of FormFlow let you customize how a FormFlow object behaves.
Nhận xét
Đăng nhận xét