Change flow of messages in Microsoft Bot Framework

匿名 (未验证) 提交于 2019-12-03 07:50:05

问题:

Hello I'm new to Microsoft Bot Framework and I have a question that I couldn't find an answer to. I have a FormFlow that ask the user for some question, after a specific question I want the bot to do some logic and show messages accordingly (for example if the user selected option 1 then show message X and if the user selected option 2 show message Y).

Here is my code:

using Microsoft.Bot.Builder.FormFlow; using Microsoft.Bot.Builder.Dialogs; using System; using System.Collections.Generic; using System.Linq; using System.Web;  namespace Bot_CRM.FormFlow {     public enum RequestOptions { Unknown, CheckStatus, CreateCase };      [Serializable]     public class CaseFormFlow     {         public RequestOptions RequestType;         [Prompt("What is your first name?")]         public string FirstName;         public string LastName;         public string ContactNumber;         [Prompt("Please enter your id")]         public string Id;          public static IForm BuildForm()         {             OnCompletionAsyncDelegate processRequest = async (context, state) =>             {                 await context.PostAsync($@"Thanks for your request");             };              return new FormBuilder()                    .Message("Hello and welcom to my service desk bot")                    .Field(nameof(FirstName))                    .Message("hello {FirstName}")                    .Field(nameof(Id))                    .Field(nameof(RequestType)) =>  //here if user select 1 start flow of check status and if user select 2 start flow of create case                    .AddRemainingFields()                    .Message("Thank you request. Our help desk team will get back to you shortly.")                    .OnCompletion(processRequest)                    .Build();         }     } }

Updated code after Ezequiel's suggestion:

 return new FormBuilder()                .Message("Hello and welcom to my service desk bot")                .Field(nameof(FirstName))                .Message("hello {FirstName}")                .Field(new FieldReflector(nameof(RequestType))                 .SetActive(state => state.AskUserForRequestType)                 .SetNext((value, state) =>                 {                     var selection = (RequestOptions)value;                      if (selection == RequestOptions.CheckStatus)                     {                          return new NextStep(new[] { nameof(Id) });                     }                     else                     {                         return new NextStep();                     }                 }))

Thanks in advance for the help

回答1:

This is a great question.The key thing is to use the SetActive and SetNext methods of the Field class. You should consider using the FieldReflector class; though you can implement your own IField.

SetActive is described in the Dynamic Fields section of the FormFlow documentation. Basically it provides a delegate that enables the field based on a condition.

SetNext will allow you to decide what step of the form should come next based on your custom logic.

You can take a look to the ContosoFlowers sample. In the Order form; something similar is being done.

 public static IForm         
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!