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

时间:2017-06-14 16:54:48

标签: c# botframework formflow

我正在使用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();

        }

有什么想法吗?

1 个答案:

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