我正在使用FormFlow使用botframework(C#)构建我的机器人。我有两个可选字段。根据这两个字段的值,我想将第三个字段 ReportRequest 的值设置为“应用程序”,如果 ApplicationName 中的值存在,或者“project”(如果只有值)存在 PojectName ,如果存在两个字段中的值,则为“application,project”。我不想提示用户询问ReportRequest字段。我想在内部设置
[Optional]
[Prompt("What is the application name? {||}")]
public string ApplicationName { get; set; }
[Optional]
[Prompt("What is the project name? {||}")]
public string PojectName { 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(nameof(ApplicationName))
.Field(nameof(ProjectName))
.Confirm(async (state) =>
{
if (!string.IsNullOrEmpty(state.ApplicationName) && !string.IsNullOrEmpty(state.PojectName))
{
state.ReportRequest = "application,project";
}
else if (!string.IsNullOrEmpty(state.ApplicationName))
{
state.ReportRequest = "application";
}
else (!string.IsNullOrEmpty(state.PojectName))
{
state.ReportRequest = "project";
}
return new PromptAttribute("Would you like to confirm.Yes or No");
})
#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
.Build();
}
有什么想法吗?
答案 0 :(得分:2)
鉴于ReportRequest
字段未在表单中使用,我相信您应该将逻辑从表单中移出(意味着,基本上是您在表单中指定的ResumeAfter<T>
方法。调用FormDialog
)或OnCompletion
代表的时间。
您可能正在执行以下操作来调用FormDialog:
var form = new FormDialog<StandardInfoForm>(new StandardInfoForm(), StandardInfoForm.BuildForm, FormOptions.PromptInStart);
context.Call(form, this.AfterForm);
ResumeAfter<T>
方法(在此示例中为AfterForm
)将以Awaitable
参数的形式接收表单状态:
private async Task AfterForm(IDialogContext context, IAwaitable<StandardInfoForm> result)
{
var state = await result;
if (!string.IsNullOrEmpty(state.ApplicationName) && !string.IsNullOrEmpty(state.PojectName))
{
state.ReportRequest = "application,project";
}
else if (!string.IsNullOrEmpty(state.ApplicationName))
{
state.ReportRequest = "application";
}
else (!string.IsNullOrEmpty(state.PojectName))
{
state.ReportRequest = "project";
}
}
在定义表单时,您可以利用OnCompletion
委托
public static IForm<StandardInfoForm> BuildForm()
{
var parser = new Parser();
return new FormBuilder<StandardInfoForm>()
.Message("Welcome to reporting information!!")
.Field(nameof(ApplicationName))
.Field(nameof(ProjectName))
.Confirm("Would you like to confirm.Yes or No")
.OnCompletion(async (context, state) =>
{
if (!string.IsNullOrEmpty(state.ApplicationName) && !string.IsNullOrEmpty(state.PojectName))
{
state.ReportRequest = "application,project";
}
else if (!string.IsNullOrEmpty(state.ApplicationName))
{
state.ReportRequest = "application";
}
else (!string.IsNullOrEmpty(state.PojectName))
{
state.ReportRequest = "project";
}
return new PromptAttribute("Would you like to confirm.Yes or No");
})
.Build();
}