2017-06-15 54 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非常感謝您抽出時間並以清晰的方式解釋。它解決了我的問題:) –

相關問題