如何根据 FormFlow 中其他字段的值打开和关闭不同的字段



我正在使用FormFlow通过botFrameWork(C#)构建我的机器人。我想要求用户选择四个报告之一,在选择的基础上,我想打开和关闭某些字段,只问那些与选择相关的问题。

Follwoing是报告类型的枚举:

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:如果用户选择应用程序,那么我只想向用户询问报告应用程序名称开始日期,结束日期

案例 2:如果用户选择情感,那么我只想向用户询问有关情感名称和开始日期、结束日期的信息

案例 3:如果用户选择AppVsEmotion,则我想向用户询问有关 ReportApplicationNameEmotionNameStartDateEndDate的信息

案例4:如果用户选择帮助,那么我只想询问报告应用程序名称开始日期,结束日期

我尝试执行以下操作,但它不起作用:

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

Microsoft.Bot.Builder.FormFlow.FormCanceledException

正在发生,因为ReportApplicationName不是表单构建器中的字段之一。 如果添加 .字段(名称(报告应用程序名称))到构建中,不会发生异常。

您还需要使用 .字段的活动代表,用于ReportApplicationNameEmotionName步骤,因为有时您想跳过它们。 从活动委托返回 false 将完成此操作。

注意:我在枚举中将"情绪"更改为"感觉",因为FormBuilder在EmotionAppVsEmotion之间的相似性方面遇到了问题 不过,这应该会让你朝着正确的方向前进:

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

相关内容

  • 没有找到相关文章

最新更新