1

我正在使用 FormFlow 使用 botframework (C#) 构建我的机器人。我有两个可选字段。基于这两个字段的值,如果 ApplicationName 中的值存在,我想将第三个字段 ReportRequest 的值设置应用程序” ,如果仅存在PojectName中的值,则设置为“项目” ,或者如果值在这两个字段都存在。我不想提示用户询问 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();

        }

有任何想法吗?

4

1 回答 1

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();
}
于 2017-06-14T18:24:54.457 回答