2017-04-26 31 views
0

即時通訊使用formflow,即時嘗試設置屬性以知道用戶是否已經迎接已經,它設置數據沒有任何問題(userData.SetProperty(「Greet」, true);), 我通常會在設置後檢查屬性,但是當您試圖在下次運行時獲取該屬性的值(userData.GetProperty(「Greet」))時,看起來它不是'保存。我需要這樣做,以便在用戶迎接後,它將退出表單流並嘗試qnadialog。Botdata.SetProperty不保存它的值

MessageController

public async Task<HttpResponseMessage> Post([FromBody]Activity activity) 
    { 
     if (activity.Type == ActivityTypes.Message) 
     { 
      //ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl)); 
      //await Conversation.SendAsync(activity,() => new QnADialog()); 

      #region Formflow 

      // 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 
      { 
       //Check if Personalized Greeting is done 
       if (userData.GetProperty<bool>("Greet")) 
       { 
        ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl)); 
        await Conversation.SendAsync(activity,() => new QnADialog()); 
       } 
       else 
       { 
        // Get the saved profile values 
        var Name = userData.GetProperty<string>("Name"); 
        userData.SetProperty<bool>("Greet", true); 
        sc.BotState.SetUserData(activity.ChannelId, activity.From.Id, userData); 
        ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl)); 
        Activity replyMessage = activity.CreateReply(string.Format("Hi {0}!", Name)); 
        await connector.Conversations.ReplyToActivityAsync(replyMessage); 
       } 
      } 
      #endregion 
     } 
     else 
     { 
      HandleSystemMessage(activity); 
     } 
     var response = Request.CreateResponse(HttpStatusCode.OK); 
     return response; 
    } 

FormFlow

[Serializable] 
public class ProfileForm 
{ 
    // these are the fields that will hold the data 
    // we will gather with the form 
    [Prompt("What is your name? {||}")] 
    public string Name; 

    // This method 'builds' the form 
    // This method will be called by code we will place 
    // in the MakeRootDialog method of the MessagesControlller.cs file 
    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>("Name", profileForm.Name); 
        // Tell the user that the form is complete 
        await context.PostAsync("Your profile is complete."); 
       }) 
       .Build(); 
    } 
} 
+0

添加setUserData來。但仍然錯誤持續 – anonymous1110

回答

0

在你的控制器首先獲得私人談話數據存儲,然後將其設置爲使用用戶數據的數據存儲。這樣,您對私人會話存儲的更改永遠不會保存在私人會話數據存儲中。

變化

sc.BotState.SetUserData(activity.ChannelId, activity.From.Id, userData); 

sc.BotState.SetPrivateConversationData(activity.ChannelId, activity.Conversation.Id, activity.From.Id, userData); 
+0

這工作就像一個魅力!如果這是將對話轉交給qnadialog的正確方法,你能幫助我嗎?它似乎沒有工作。它仍然顯示窗體對話框(這是您的選擇?) await Conversation.SendAsync(activity,()=> new QnADialog()); – anonymous1110

+0

它似乎沒有工作。它仍然建立窗體對話框。所以它一直在問我的名字 – anonymous1110

+0

控制器中應該只有一個'Conversation.SendAsync(..)'。 第一次調用此方法時,它將在對話堆棧上創建一個根對話框。在第一次通話之後,它所做的唯一事情就是將活動轉發到對話堆棧的頂部(這仍然是您的形式)。要做到這一點,你應該在表單調用的ResumeAfter方法中啓動QnADialog。 –