0

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

这篇博文在解释表单流时非常方便,但是在保存数据时,我对我正在尝试做的项目有疑问。

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

感谢您的任何帮助,您可以提供。

4

1 回答 1

0

找到了正确的方法。我在网上阅读的很多资料都说FormFlows不保存用户输入。它保存在状态中。例如状态。值。所以代码:

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

问题比我想象的更容易解决。希望这对将来的某人有所帮助。

于 2017-10-16T08:28:15.053 回答