2017-10-13 61 views
0

我一直在嘗試使用幾個教程學習機器人的工作方式。如何以相同的方法從FormFLow訪問保存的數據

This blog post在解釋formflows時非常方便,但是當涉及到保存數據時,我對我正在嘗試執行的項目有個疑問。

他處理它保存在.OnCompletion數據如下所示的方式:

public static IForm<ProfileForm> BuildForm() 
{ 
    return new FormBuilder<ProfileForm>() 
      .Message("Welcome to the profile bot!") 
      .OnCompletion(async (context, profileForm) => 
      { 
       // Set BotUserData 
       context.PrivateConversationData.SetValue<bool>(
        "ProfileComplete", true); 
       context.PrivateConversationData.SetValue<string>(
        "FirstName", profileForm.FirstName); 
       context.PrivateConversationData.SetValue<string>(
        "LastName", profileForm.LastName); 
       context.PrivateConversationData.SetValue<string>(
        "Gender", profileForm.Gender.ToString()); 
       // Tell the user that the form is complete 
       await context.PostAsync("Your profile is complete."); 
      }) 
      .Build(); 
} 

但是他的方式訪問設定的變量是在消息控制器使用:

// Get any saved values 
     StateClient sc = activity.GetStateClient(); 
     BotData userData = sc.BotState.GetPrivateConversationData(
      activity.ChannelId, activity.Conversation.Id, activity.From.Id); 
     var boolProfileComplete = userData.GetProperty<bool>("ProfileComplete"); 
     if (!boolProfileComplete) 
     { 
      // Call our FormFlow by calling MakeRootDialog 
      await Conversation.SendAsync(activity, MakeRootDialog); 
     } 
     else 
     { 
      // Get the saved profile values 
      var FirstName = userData.GetProperty<string>("FirstName"); 
      var LastName = userData.GetProperty<string>("LastName"); 
      var Gender = userData.GetProperty<string>("Gender"); 

無論如何,要在同一個課堂上做這個和formflow一樣嗎?我試圖用它作爲比較指導正在進行的對話:

public IForm<DVLADialog> BuildForm() 
    { 

     OnCompletionAsyncDelegate<DVLADialog> completeForm = async(context, state) => 
     { 

      try 
      {      
       if (dvla.AbiGroupOneToFifty <= 30 && Value <= 50000) 
       { 
        await context.PostAsync($"Success!."); 
       } 
       else 
       { 
        await context.PostAsync("Uh Oh"); 
       } 
      } 
      catch (Exception ex) 
      { 
       throw; 
      } 

     }; 

     return new FormBuilder<DVLADialog>() 
      .Field(nameof(Value)) 
      .Field(nameof(DateOfPurchase)) 
      .Field(nameof(Modifications)) 
      .Field(nameof(Stored)) 
      .Field(nameof(Postcode)) 
      .Confirm("Is this all correct? {*}") 
      .OnCompletion(async (context, save) => 
      { 
       // Set BotUserData 
       context.PrivateConversationData.SetValue<bool>(
        "ProfileComplete", true); 
       context.PrivateConversationData.SetValue<float>(
        "Value", save.Value); 
       context.PrivateConversationData.SetValue<DateTime>(
        "Date Of Purchase", save.DateOfPurchase); 
       context.PrivateConversationData.SetValue<string>(
        "Mods", save.Modifications.ToString()); 
       context.PrivateConversationData.SetValue<string>(
        "Stored", save.Stored.ToString()); 
       context.PrivateConversationData.SetValue<string>(
        "Postcode", save.Postcode.ToString()); 

       // Tell the user that the form is complete 
       await context.PostAsync("Your profile is complete."); 
      }) 
      .OnCompletion(completeForm) 
      .Build(); 
    } 

感謝您提供任何幫助。

+0

爲什麼不簡單地在'.OnCompletion(async(context,save)=> ...'中添加你的測試呢? –

回答

0

找到了正確的方法。我在網上閱讀的很多信息都表示FormFlows不會保存用戶輸入。它保存在狀態中。例如state.Value。所以代碼:

public IForm<DVLADialog> BuildForm() 
    { 

     OnCompletionAsyncDelegate<DVLADialog> completeForm = async(context, state) => 
     { 

      try 
      {     

       if (dvla.AbiGroupOneToFifty <= 30 && state.Value <= 50000) 
       { 
        await context.PostAsync($"Yay"); 
       } 
       else 
       { 
        await context.PostAsync("Uh Oh"); 
       } 
      } 
      catch (Exception ex) 
      { 
       throw; 
      } 

     }; 

     return new FormBuilder<DVLADialog>() 
      .Field(nameof(Value)) 
      .Field(nameof(DateOfPurchase)) 
      .Field(nameof(Modifications)) 
      .Field(nameof(Stored)) 
      .Field(nameof(Postcode)) 
      .Confirm("Is this all correct? {*}") 

      .OnCompletion(completeForm) 
      .Build(); 
    } 
} 

問題比我想象的要容易得多。希望這有助於未來的人。

相關問題