2017-06-15 47 views
1

我正在使用FormFlow使用botFrameWork(C#)构建我的bot。我想要求用户选择四个报告中的一个,并根据选择我想打开和关闭某些字段,并只询问与选择相关的问题。如何根据FormFlow中其他字段的值设置和关闭不同的字段

Follwoing是报告类型的枚举:

public enum ReportType 
    { 
     Application = 1, 
     Emotion, 
     AppVsEmotion, 
     Help 
    } 

这里是所有领域:

public bool AskToChooseReport = true; 

[Prompt("What kind of report you would like? {||}")] 
public ReportType? Report { get; set; } 

[Prompt("What is the application name?")] 
public string ReportApplicationName { get; set; } 


[Prompt("Please enter the emotion name? {||}")] 
public string EmotionName { get; set; } 

[Prompt("What is starting date (MM-DD-YYYY) for report?{||}")] 
public string StartDate { get; set; } 

[Prompt("What is the end date (MM-DD-YYYY) for report? {||}")] 
public string EndDate { get; set; } 

public string ReportRequest = string.Empty; 

我有四种情况:

案例1:如果用户slects 应用 ,然后我只想问用户约ReportApplicationName,起始日期结束日期

案例2:如果用户选择情感,那么我只想问用户约EmotionName起始日期结束日期

案例3:如果用户选择AppVsEmotion,我想问问用户关于ReportApplicationName,EmotionName起始日期结束日期

案例4:如果用户选择帮助,然后我只想问ReportApplicationName起始日期结束日期

我试着做以下,但不起作用:

public static IForm<StandardInfoForm> BuildForm() 
     { 
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously 

      var parser = new Parser(); 
      return new FormBuilder<StandardInfoForm>() 
       .Message("Welcome to reporting information!!") 
       .Field(new FieldReflector<StandardInfoForm>(nameof(Report)) 
        .SetActive(state => state.AskToChooseReport) 
        .SetNext((value, state) => 
        { 
         var selection = (ReportType)value; 
         if (selection == ReportType.Application) 
         { 
          state.ReportRequest = "application"; 
          return new NextStep(new[] { nameof(ReportApplicationName) }); 
         } 
         else if (selection == ReportType.Emotion) 
         { 
          state.ReportRequest = "emotion"; 
          return new NextStep(new[] { nameof (EmotionName) }); 
         } 
         else if (selection == ReportType.AppVsEmotion) 
         { 
          state.ReportRequest = "application,emotion"; 
          return new NextStep(new[] { nameof (ReportApplicationName), nameof(EmotionName) }); 
         } 
         else if (selection == ReportType.Help) 
         { 
          state.ReportRequest = "help"; 
          return new NextStep(new[] { nameof(ReportApplicationName) }); 
         } 
         else 
         { 
          return new NextStep(); 
         } 
        }))    
       .Field(nameof(StartDate)) 
       .Field(nameof(EndDate), EndReportDateActive)        
       .Confirm("Would you like to confirm.Yes or No") 
#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously 
       .Build(); 

     } 

请帮我,如果我太天真了。我试图按照此: Change Flow Of Message In Microsoft Bot FrameWork

+0

为什么SetNext不工作? –

+0

@EzequielJadib它不会提示用户输入下一个字段,并退出表单,但是会出现异常** Microsoft.Bot.Builder.FormFlow。FormCanceledException ** –

+0

@EzequielJadib下面是mscorlib.dll' –

回答

1

Microsoft.Bot.Builder.FormFlow.FormCanceledException正在发生的事情,因为ReportApplicationName不在表单生成器的领域之一。如果将.Field(nameof(ReportApplicationName))添加到构建中,则不会发生异常。

您还需要将.Field的ActiveDelegate用于ReportApplicationNameEmotionName的步骤,因为有时您想跳过它们。从ActiveDelegate返回false将完成此操作。

注:我改变情绪在枚举感情,因为FormBuilder是具有EmotionAppVsEmotion之间的相似度。这应该让你在正确的方向前进,虽然麻烦:

public enum ReportType 
    { 
     Application = 1, 
     Feelings, 
     AppVsEmotion, 
     Help 
    } 

    [Serializable] 
    public class StandardInfoForm 
    { 
     public bool AskToChooseReport = true; 

     [Prompt("What kind of report you would like? {||}")] 
     public ReportType? Report { get; set; } 

     [Prompt("What is the application name? {||}")] 
     public string ReportApplicationName { get; set; } 


     [Prompt("Please enter the emotion name? {||}")] 
     public string EmotionName { get; set; } 

     [Prompt("What is starting date (MM-DD-YYYY) for report?{||}")] 
     public string StartDate { get; set; } 

     [Prompt("What is the end date (MM-DD-YYYY) for report? {||}")] 
     public string EndDate { get; set; } 

     public string ReportRequest = string.Empty; 

     public static IForm<StandardInfoForm> BuildForm() 
     { 

      var parser = new Parser(); 
      return new FormBuilder<StandardInfoForm>() 
       .Message("Welcome to reporting information!!") 
       .Field(new FieldReflector<StandardInfoForm>(nameof(Report)) 
          .SetActive(state => state.AskToChooseReport) 
          .SetNext(SetNext)) 
       .Field(nameof(ReportApplicationName), state => state.ReportRequest.Contains("application")) 
       .Field(nameof(EmotionName), state => state.ReportRequest.Contains("emotion")) 
       .Field(nameof(StartDate)) 
       .Field(nameof(EndDate), EndReportDateActive) 
       .Confirm("Would you like to confirm.Yes or No") 
      .Build(); 

     } 

     private static NextStep SetNext(object value, StandardInfoForm state) 
     { 
      var selection = (ReportType)value; 
      if (selection == ReportType.Application) 
      { 
       state.ReportRequest = "application"; 
       return new NextStep(new[] { nameof(ReportApplicationName) }); 
      } 
      else if (selection == ReportType.Feelings) 
      { 
       state.ReportRequest = "emotion"; 
       return new NextStep(new[] { nameof(EmotionName) }); 
      } 
      else if (selection == ReportType.AppVsEmotion) 
      { 
       state.ReportRequest = "application,emotion"; 
       return new NextStep(new[] { nameof(ReportApplicationName) }); 
      } 
      else if (selection == ReportType.Help) 
      { 
       state.ReportRequest = "help"; 
       return new NextStep(new[] { nameof(ReportApplicationName) }); 
      } 
      else 
      { 
       return new NextStep(); 
      } 
     } 
+0

中完整的异常'Microsoft.Bot.Builder.FormFlow.FormCanceledException'1非常感谢您抽出时间并以清晰的方式解释。它解决了我的问题:) –

相关问题