如何在FormFlow中基于其他字段的值来打开和关闭不同的字段 [英] How to set different fields on and off on basis of value of other field in FormFlow
问题描述
我正在使用FormFlow使用botFrameWork(C#)构建我的机器人.我想让用户从四个报告中选择一个,并希望根据选择来打开和关闭某些字段,并只询问与选择相关的那些问题.
I am using FormFlow to build my bot using botFrameWork(C#). I want to ask user to choose one of four reports and on the basis of selection I want to turn on and off certain fields and ask only those questions which are relevant to the selection.
Follwoing是报告类型的枚举:
Follwoing is the enum for report types:
public enum ReportType
{
Application = 1,
Emotion,
AppVsEmotion,
Help
}
以下是所有字段:
public bool AskToChooseReport = true;
[Prompt("What kind of report you would like? {||}")]
public ReportType? Report { get; set; }
[Prompt("What is the application name?")]
public string ReportApplicationName { get; set; }
[Prompt("Please enter the emotion name? {||}")]
public string EmotionName { get; set; }
[Prompt("What is starting date (MM-DD-YYYY) for report?{||}")]
public string StartDate { get; set; }
[Prompt("What is the end date (MM-DD-YYYY) for report? {||}")]
public string EndDate { get; set; }
public string ReportRequest = string.Empty;
我有四种情况:
案例1:如果用户选择 Application ,那么我只想问用户有关 ReportApplicationName , StartDate , EndDate
Case 1: If user slects Application, Then i only want to ask user about ReportApplicationName, StartDate, EndDate
案例2:如果用户选择 Emotion ,那么我只想问用户有关 EmotionName 和 StartDate , EndDate
Case 2: If user selects Emotion, Then i only want to ask user about EmotionName and StartDate, EndDate
案例3:如果用户选择 AppVsEmotion ,则我想向用户询问 ReportApplicationName , EmotionName 和 StartDate ,结束日期
Case 3: If user selects AppVsEmotion, The i want to ask user about both ReportApplicationName, EmotionName and StartDate, EndDate
案例4:如果用户选择 Help ,那么我只想问有关 ReportApplicationName , StartDate , EndDate >
Case 4: If user selects Help, then i only want to ask about ReportApplicationName, StartDate, EndDate
我尝试执行以下操作,但不起作用:
I tried to do following but it doesn't work:
public static IForm<StandardInfoForm> BuildForm()
{
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
var parser = new Parser();
return new FormBuilder<StandardInfoForm>()
.Message("Welcome to reporting information!!")
.Field(new FieldReflector<StandardInfoForm>( nameof(Report))
.SetActive( state => state.AskToChooseReport)
.SetNext( (value, state) =>
{
var selection = (ReportType)value;
if (selection == ReportType.Application)
{
state.ReportRequest = "application";
return new NextStep(new[] { nameof(ReportApplicationName) });
}
else if (selection == ReportType.Emotion)
{
state.ReportRequest = "emotion";
return new NextStep(new[] { nameof (EmotionName) });
}
else if (selection == ReportType.AppVsEmotion)
{
state.ReportRequest = "application,emotion";
return new NextStep(new[] { nameof (ReportApplicationName), nameof(EmotionName) });
}
else if (selection == ReportType.Help)
{
state.ReportRequest = "help";
return new NextStep(new[] { nameof(ReportApplicationName) });
}
else
{
return new NextStep();
}
}))
.Field(nameof(StartDate))
.Field(nameof(EndDate), EndReportDateActive)
.Confirm("Would you like to confirm.Yes or No")
#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
.Build();
}
如果我太天真,请帮助我.我试图遵循此: 更改Microsoft Bot FrameWork中的消息流
Please help me if I am being too naive. I have tried to follow this: Change Flow Of Message In Microsoft Bot FrameWork
推荐答案
Microsoft.Bot.Builder.FormFlow.FormCanceledException
,因为ReportApplicationName
不是表单构建器中的字段之一.如果将.Field(nameof(ReportApplicationName))添加到内部版本,则不会发生该异常.
Microsoft.Bot.Builder.FormFlow.FormCanceledException
is happening because ReportApplicationName
is not one of the fields in the form builder. If you add .Field(nameof(ReportApplicationName)) to the build, the exception will not occur.
您还需要将.Field的ActiveDelegate用于ReportApplicationName
和EmotionName
步骤,因为有时您想跳过它们.从ActiveDelegate返回false将完成此操作.
You'll also need to use the .Field's ActiveDelegate for the ReportApplicationName
and EmotionName
steps, since sometimes you want to skip them. Returning false from the ActiveDelegate will accomplish this.
注意:我在枚举中将情感"更改为感觉",因为FormBuilder遇到了Emotion
和AppVsEmotion
之间相似性的麻烦,但这应该可以使您朝正确的方向前进:
Note: I changed Emotions to Feelings in the enum, because FormBuilder was having trouble with the similarity between Emotion
and AppVsEmotion
This should get you going in the right direction though:
public enum ReportType
{
Application = 1,
Feelings,
AppVsEmotion,
Help
}
[Serializable]
public class StandardInfoForm
{
public bool AskToChooseReport = true;
[Prompt("What kind of report you would like? {||}")]
public ReportType? Report { get; set; }
[Prompt("What is the application name? {||}")]
public string ReportApplicationName { get; set; }
[Prompt("Please enter the emotion name? {||}")]
public string EmotionName { get; set; }
[Prompt("What is starting date (MM-DD-YYYY) for report?{||}")]
public string StartDate { get; set; }
[Prompt("What is the end date (MM-DD-YYYY) for report? {||}")]
public string EndDate { get; set; }
public string ReportRequest = string.Empty;
public static IForm<StandardInfoForm> BuildForm()
{
var parser = new Parser();
return new FormBuilder<StandardInfoForm>()
.Message("Welcome to reporting information!!")
.Field(new FieldReflector<StandardInfoForm>(nameof(Report))
.SetActive(state => state.AskToChooseReport)
.SetNext(SetNext))
.Field(nameof(ReportApplicationName), state => state.ReportRequest.Contains("application"))
.Field(nameof(EmotionName), state => state.ReportRequest.Contains("emotion"))
.Field(nameof(StartDate))
.Field(nameof(EndDate), EndReportDateActive)
.Confirm("Would you like to confirm.Yes or No")
.Build();
}
private static NextStep SetNext(object value, StandardInfoForm state)
{
var selection = (ReportType)value;
if (selection == ReportType.Application)
{
state.ReportRequest = "application";
return new NextStep(new[] { nameof(ReportApplicationName) });
}
else if (selection == ReportType.Feelings)
{
state.ReportRequest = "emotion";
return new NextStep(new[] { nameof(EmotionName) });
}
else if (selection == ReportType.AppVsEmotion)
{
state.ReportRequest = "application,emotion";
return new NextStep(new[] { nameof(ReportApplicationName) });
}
else if (selection == ReportType.Help)
{
state.ReportRequest = "help";
return new NextStep(new[] { nameof(ReportApplicationName) });
}
else
{
return new NextStep();
}
}
这篇关于如何在FormFlow中基于其他字段的值来打开和关闭不同的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!