botframework中如何根据FormFlow中其他字段的值设置字段值?

How to set field value based on the value of other fields in FormFlow in botframework?

我正在使用 FormFlow 通过 botframework (C#) 构建我的机器人。我有两个可选字段。根据这两个字段的值,如果存在 ApplicationName 中的值,我想将第三个字段 ReportRequest 的值设置为 "application" ,或者 "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();

        }

有什么想法吗?

鉴于表单中未使用 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();
}