FormFlow:使用重复问题添加多个实体
FormFlow: add multiple entities using recurring questions
我一直在玩弄机器人框架并创建一个聊天机器人来取乐,它可以让您详细介绍 family/pets 的成员。
有没有办法重复同一组问题直到用户满意?示例代码如下:
[Prompt("What is your family name?")]
public string familyName{ get; set; }
[Prompt("What is your postcode?")]
public string postcode { get; set; }
[Prompt("Would you like to add a family member? {||}")]
public bool AddPerson { get; set; }
[Prompt("What is their name?")]
public string PersonName { get; set; }
[Prompt("How old are they?")]
public string PersonAge{ get; set; }
[Prompt("How are they related to you?")]
public string PersonRelation{ get; set; }
[Prompt("Would you like to add another family member? {||}")]
public bool addAnotherPerson { get; set; }
public IForm<Family> BuildForm()
{
return new FormBuilder<GetQuoteDialog>()
.Field(nameof(familyName))
.Field(nameof(postcode))
//Choose to add a person to the family
.Field(nameof(AddPerson))
//Details of that person.
.Field(new FieldReflector<Family>(nameof(PersonName))
.SetActive((state) => state.AddPerson== true))
.Field(new FieldReflector<Family>(nameof({PersonAge))
.SetActive((state) => state.AddPerson== true))
.Field(new FieldReflector<Family>(nameof({PersonRelation))
.SetActive((state) => state.AddPerson== true))
//Prompts the user to add another if they wish
//Recurs to the PersonName field and lets them go through the
//process of adding another member
.Field(new FieldReflector<Family>(nameof({AddAnotherMember))
.SetActive((state) => state.AddPerson== true))
.Confirm("Is this your family? {*}")
.Build();
}
}
有没有人知道如何做到这一点?
我这样调用表单流:
public async Task confirmAdd(IDialogContext context, IAwaitable<bool> result)
{
if (await result)
{
// builds and calls the form from here
var myform = new FormDialog<BuildFamily>(new BuildFamily(), BuildForm, FormOptions.PromptInStart, null);
context.Call<BuildFamily>(myform, End);
}
}
private async Task End(IDialogContext context, IAwaitable<BuildFamily> result)
{
BuildFamily data = null;
try
{
data = await result;
await context.PostAsync("Nice family you got there :)");
}
catch (OperationCanceledException)
{
await context.PostAsync("You canceled the form!");
return;
}
}
我不确定如何在 FormFlow 对话框中"recur over the same set of questions until the user is satisfied"。但是,您可以在调用对话框中向用户提出问题"Would you like to add more family members?",并实现相同类型的对话流程。删除 PostalCode 和 FamilyName 类型的问题,并将它们放在单独的对话框中。然后,在添加家庭成员对话框中执行如下操作:
[Serializable]
public class AddFamilyMembersDialog : IDialog<object>
{
List<Family> _familyMembers = new List<Family>();
public Task StartAsync(IDialogContext context)
{
context.Wait(MessageReceivedAsync);
return Task.CompletedTask;
}
private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<object> result)
{
PromptAddMembers(context);
}
private void PromptAddMembers(IDialogContext context)
{
PromptDialog.Text(context, AfterPromptAdd, "Would you like to add more family members?", null, 1);
}
private async Task AfterPromptAdd(IDialogContext context, IAwaitable<string> result)
{
var yesno = await result;
if (yesno.ToLower() == "yes")
{
await context.Forward(FormDialog.FromForm(Family.BuildForm), AfterAdded, null, CancellationToken.None);
}
else
{
//_familyMembers contains everyone the user wanted to add
context.Done(true);
}
}
private async Task AfterAdded(IDialogContext context, IAwaitable<Family> result)
{
var member = await result;
if (member != null)
_familyMembers.Add(member);
PromptAddMembers(context);
}
[Serializable]
public class Family
{
[Prompt("What is their name?")]
public string PersonName { get; set; }
[Prompt("How old are they?")]
public string PersonAge { get; set; }
[Prompt("How are they related to you?")]
public string PersonRelation { get; set; }
public static IForm<Family> BuildForm()
{
return new FormBuilder<Family>()
.AddRemainingFields()
.Build();
}
}
}
我一直在玩弄机器人框架并创建一个聊天机器人来取乐,它可以让您详细介绍 family/pets 的成员。
有没有办法重复同一组问题直到用户满意?示例代码如下:
[Prompt("What is your family name?")]
public string familyName{ get; set; }
[Prompt("What is your postcode?")]
public string postcode { get; set; }
[Prompt("Would you like to add a family member? {||}")]
public bool AddPerson { get; set; }
[Prompt("What is their name?")]
public string PersonName { get; set; }
[Prompt("How old are they?")]
public string PersonAge{ get; set; }
[Prompt("How are they related to you?")]
public string PersonRelation{ get; set; }
[Prompt("Would you like to add another family member? {||}")]
public bool addAnotherPerson { get; set; }
public IForm<Family> BuildForm()
{
return new FormBuilder<GetQuoteDialog>()
.Field(nameof(familyName))
.Field(nameof(postcode))
//Choose to add a person to the family
.Field(nameof(AddPerson))
//Details of that person.
.Field(new FieldReflector<Family>(nameof(PersonName))
.SetActive((state) => state.AddPerson== true))
.Field(new FieldReflector<Family>(nameof({PersonAge))
.SetActive((state) => state.AddPerson== true))
.Field(new FieldReflector<Family>(nameof({PersonRelation))
.SetActive((state) => state.AddPerson== true))
//Prompts the user to add another if they wish
//Recurs to the PersonName field and lets them go through the
//process of adding another member
.Field(new FieldReflector<Family>(nameof({AddAnotherMember))
.SetActive((state) => state.AddPerson== true))
.Confirm("Is this your family? {*}")
.Build();
}
}
有没有人知道如何做到这一点?
我这样调用表单流:
public async Task confirmAdd(IDialogContext context, IAwaitable<bool> result)
{
if (await result)
{
// builds and calls the form from here
var myform = new FormDialog<BuildFamily>(new BuildFamily(), BuildForm, FormOptions.PromptInStart, null);
context.Call<BuildFamily>(myform, End);
}
}
private async Task End(IDialogContext context, IAwaitable<BuildFamily> result)
{
BuildFamily data = null;
try
{
data = await result;
await context.PostAsync("Nice family you got there :)");
}
catch (OperationCanceledException)
{
await context.PostAsync("You canceled the form!");
return;
}
}
我不确定如何在 FormFlow 对话框中"recur over the same set of questions until the user is satisfied"。但是,您可以在调用对话框中向用户提出问题"Would you like to add more family members?",并实现相同类型的对话流程。删除 PostalCode 和 FamilyName 类型的问题,并将它们放在单独的对话框中。然后,在添加家庭成员对话框中执行如下操作:
[Serializable]
public class AddFamilyMembersDialog : IDialog<object>
{
List<Family> _familyMembers = new List<Family>();
public Task StartAsync(IDialogContext context)
{
context.Wait(MessageReceivedAsync);
return Task.CompletedTask;
}
private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<object> result)
{
PromptAddMembers(context);
}
private void PromptAddMembers(IDialogContext context)
{
PromptDialog.Text(context, AfterPromptAdd, "Would you like to add more family members?", null, 1);
}
private async Task AfterPromptAdd(IDialogContext context, IAwaitable<string> result)
{
var yesno = await result;
if (yesno.ToLower() == "yes")
{
await context.Forward(FormDialog.FromForm(Family.BuildForm), AfterAdded, null, CancellationToken.None);
}
else
{
//_familyMembers contains everyone the user wanted to add
context.Done(true);
}
}
private async Task AfterAdded(IDialogContext context, IAwaitable<Family> result)
{
var member = await result;
if (member != null)
_familyMembers.Add(member);
PromptAddMembers(context);
}
[Serializable]
public class Family
{
[Prompt("What is their name?")]
public string PersonName { get; set; }
[Prompt("How old are they?")]
public string PersonAge { get; set; }
[Prompt("How are they related to you?")]
public string PersonRelation { get; set; }
public static IForm<Family> BuildForm()
{
return new FormBuilder<Family>()
.AddRemainingFields()
.Build();
}
}
}