简体   繁体   中英

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

I am using FormFlow to build my bot using botframework (C#). And i have two optional fields. Based on the value of these two fields i want to set the value of third field ReportRequest as "application" if value in ApplicationName is present, or "project" if only value in PojectName is present, or "application,project" if value in both fields is present. I do not want to prompt user to ask ReportRequest field. I want to set it internally

[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;

I tried to do following but it doesn't seem to work

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

        }

Any ideas?

Given that the ReportRequest field is not being used within the form, I believe that you should move the logic either out of the Form (meaning, basically to the ResumeAfter<T> method that you specified at the time of calling the FormDialog ) or to the OnCompletion delegate.

Out of the form option

You probably are doing something like the following to call your FormDialog:

  var form = new FormDialog<StandardInfoForm>(new StandardInfoForm(), StandardInfoForm.BuildForm, FormOptions.PromptInStart);
  context.Call(form, this.AfterForm);

The ResumeAfter<T> method (in this example: AfterForm ) will receive the state of the form as the Awaitable parameter:

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";
    }
}

On completion option

When defining your form you could take advantage of the OnCompletion delegate

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM