Botdata.SetProperty不保存其价值

时间:2017-04-26 08:18:37

标签: c# bots botframework

我正在使用formflow,我试图设置一个属性来知道用户是否已经受到欢迎,它设置数据没有任何问题(userData.SetProperty(" Greet",true) );)下, 我通常会在设置后检查属性,但是当您尝试在下次运行期间获取该属性的值时( userData.GetProperty(" Greet")),似乎它没有得救。我需要这个,以便在用户受到欢迎后,它将退出表单流并尝试qnadialog。

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();
    }
}

1 个答案:

答案 0 :(得分:0)

在控制器中,首先获取私人对话数据存储,然后将其设置为Userdata数据存储。这样,您对私人对话存储的更改永远不会保存在私人对话数据存储区中。

更改

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

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