我一直在尝试使用几个教程来学习机器人是如何工作的。
这篇博客文章在解释表单流时非常方便,但当涉及到保存数据时,我对我正在尝试做的项目有一个问题。
他处理它的方式是将数据保存在。完成时如下所示:
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();
}
感谢您提供的任何帮助。
找到了正确的方法。我在网上读到的很多信息都说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();
}
}
这个问题比我想象的要容易得多。希望这对将来的人有所帮助。