2017-02-16 75 views
0

我正在爲BotFramework使用BotBuilder的C#SDK,並且想要執行以下操作。BotFramework在FormFlow中設置默認數據

我有一個FormFlow對話框,它收集預留表格的信息。 FormFlow中的一個項目是要求名稱。在另一個對話框中,我正在收集名稱並將其保存到userData。

context.UserData.SetValue<string>("Name", userName); 

我的Formflow看起來像這樣。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using Microsoft.Bot.Builder.FormFlow; 
using Microsoft.Bot.Builder.FormFlow.Advanced; 
using System.Text.RegularExpressions; 
using System.Threading.Tasks; 


namespace DinnerBot.Dialogs 
{ 
    [Serializable] 
    public class ReservationDialog 
    { 

     public enum SpecialOccasionOptions 
     { 
      Birthday, 
      Anniversary, 
      Engagement, 
      none 
     } 

     [Prompt(new string[] { "What is your name?" })] 
     public string Name { get; set; } 

     [Prompt(new string[] { "What is your email?" })] 
     public string Email { get; set; } 

     [Pattern(@"^(\+\d{1,2}\s)?\(?\d{3}\)?[\s.-]?\d{3}[\s.-]?\d{4}$")] 
     public string PhoneNumber { get; set; } 

     [Prompt("What date would you like to dine with us? example: today, tomorrow, or any date like 04-06-2017 {||}", AllowDefault = BoolDefault.True)] 
     [Describe("Reservation date, example: today, tomorrow, or any date like 04-06-2017")] 
     public DateTime ReservationDate { get; set; } 

     public DateTime ReservationTime { get; set; } 

     [Prompt("How many people will be joining us?")] 
     [Numeric(1, 20)] 
     public int? NumberOfDinners; 
     public SpecialOccasionOptions? SpecialOccasion; 

     [Numeric(1, 5)] 
     [Optional] 
     [Describe("for how you enjoyed your experience with Dinner Bot today (optional)")] 
     public double? Rating; 

     public static IForm<ReservationDialog> BuildForm() 
     { 
      return new FormBuilder<ReservationDialog>() 
       .Field(nameof(Name)) 
       .Field(nameof(Email), validate: ValidateContactInformation) 
       .Field(nameof(PhoneNumber)) 
       .Field(nameof(ReservationDate)) 
       .Field(new FieldReflector<ReservationDialog>(nameof(ReservationDialog.ReservationTime)) 
        .SetPrompt(PerLinePromptAttribute("What time would you like to arrive?")) 
        ).AddRemainingFields() 
       .Build(); 
     } 

     private static Task<ValidateResult> ValidateContactInformation(ReservationDialog state, object response) 
     { 
      var result = new ValidateResult(); 
      string contactInfo = string.Empty; 
      if (GetEmailAddress((string)response, out contactInfo)) 
      { 
       result.IsValid = true; 
       result.Value = contactInfo; 
      } 
      else 
      { 
       result.IsValid = false; 
       result.Feedback = "You did not enter valid email address."; 
      } 
      return Task.FromResult(result); 
     } 

     private static bool GetEmailAddress(string response, out string contactInfo) 
     { 
      contactInfo = string.Empty; 
      var match = Regex.Match(response, @"[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?"); 
      if (match.Success) 
      { 
       contactInfo = match.Value; 
       return true; 
      } 
      return false; 
     } 

     private static PromptAttribute PerLinePromptAttribute(string pattern) 
     { 
      return new PromptAttribute(pattern) 
      { 
       ChoiceStyle = ChoiceStyleOptions.PerLine 
      }; 
     } 


    } 
} 

在我的根對話框中,我這樣稱呼它。

     context.Call(FormDialog.FromForm<ReservationDialog>(ReservationDialog.BuildForm, 
         FormOptions.PromptInStart), this.ReservationFormComplete); 

我怎麼能有formflow跳過名稱字段,並從用戶數據取的名字,如果它的存在?

回答

0

爲Name屬性定義FieldReflector.SetActive。例如:

.Field(new FieldReflector<ReservationDialog>(nameof(ReservationDialog.Name)) 
    .SetActive((state) => SetFieldActive(state, nameof(ReservationDialog.Name))) 
    ... 

然後在你的SetFieldActive委託檢查,如果該名稱已經存在,如果是這樣,該值設置爲state並返回false。然後提示將不會顯示。如果找不到名稱,則返回true以顯示提示。

我寫a brief blog posta sample關於這個問題你可能會感興趣的。

0

你有一些簡單的選項。
1)使你的領域可以爲空,如果你從你保存的殭屍狀態設置他們,他們將默認跳過。 (除非您通過FormOptions.PromptFieldsWithValues。) 2)將您的值轉換爲EntityRecommendations。如果它們與表單中的字段匹配,則它們也將被分配並跳過。