Microsoft Bot Framework: How can I populate form flow field values based on the user's input for a previous field

57 views Asked by At

I have the following attributes for a form flow case:

public enum Offices{}

[Describe("Country")]
public string Country;
[Prompt("Which office are you working in?{||}")]
public Offices Office; 

I want to populate the offices according to the specified country. For example if the user enters India as a Country field, I want the offices to be Mumbai, New Delhi and Pune. If the user enters UAE, I want the offices to be Dubai and Abou Dhabi, etc...

How can I achieve this?

1

There are 1 answers

0
Ed Boykin On BEST ANSWER

This is a similar questions to "How to use enum category and subcategory in bot framework C#?" at least in how to do what you need.

Using FormBuilder you can dynamically define your form. Full docs on FormBuilder are here.

Recapping the previous StackOverlfow answer, you use a FieldReflector and that will allow you to setup an async delegate. In that delegate, you would build the list of Cities based on the state.Country value. It will look something like this:

public static IForm<Offices> BuildForm()
{
    return new FormBuilder<Offices>()
          .Message("Welcome!")
          .Field(nameof(Country))
          .Field(new FieldReflector<Offices>(nameof(Office))
              .SetType(null)
              .SetDefine(async (state, field) =>
              {
                   //// Define your Officelogic here
                  switch (state.Country)
                  {
                      Country.Dubai:
                          ////logic to add Dubai city
                        break;
                      Country.UAE:
                          ////logic to add UAE cities
                        break;
                      default:
                          break;
                  }


                  return true;
              }))              
          .Build();
}