如何以相同的方法从FormFLow访问已保存的数据

时间:2017-10-13 16:05:21

标签: c# bots botframework formflow

我一直在尝试使用多个教程来了解机器人是如何工作的。

在解释表单流时,

This blog post非常方便,但是在保存数据方面,我对我正在尝试的项目提出了一个问题。

他处理它的方法是将数据保存在.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");

无论如何都要在与表单流相同的类中执行此操作?我正在尝试将其作为比较来指导正在进行的对话:

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

感谢您提供的任何帮助。

1 个答案:

答案 0 :(得分: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();
    }
}

问题比我想象的要容易得多。希望这可以帮助将来的某个人。