如何以相同的方法从 FormFLow 访问保存的数据
How to access saved Data from the FormFLow in the same method
我一直在尝试使用多个教程来学习机器人的工作原理。
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");
是否可以在与表单流相同的 class 中执行此操作?我试图用它作为比较来指导正在进行的对话:
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不保存用户输入。它保存在状态中。例如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();
}
}
问题比我想象的要容易解决。希望这对以后的人有帮助。
我一直在尝试使用多个教程来学习机器人的工作原理。
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");
是否可以在与表单流相同的 class 中执行此操作?我试图用它作为比较来指导正在进行的对话:
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不保存用户输入。它保存在状态中。例如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();
}
}
问题比我想象的要容易解决。希望这对以后的人有帮助。