I am trying to build a form using FormBuilder with Microsoft Bot framework. I have an requirement of dynamically adding options.
I was able to add it (States) but when I select one of the state, flow ends there itself.
Please find the sample code below:
public enum StatesOptions
{
}
public enum BranchOptions
{
}
[Serializable]
public class AppointmentQuery
{
[Prompt("Please enter {&}")]
[Required(AllowEmptyStrings = false, ErrorMessage = "Please enter your Name, it is a mandatory field")]
public string Name;
[Prompt("Please enter {&}")]
[Required(AllowEmptyStrings = false, ErrorMessage = "Please enter your Email, it is a mandatory field")]
[EmailAddressAttribute(ErrorMessage = "Email Id is invalid")]
public string Email;
[Prompt("Please enter {&}")]
[Required(AllowEmptyStrings = false, ErrorMessage = "Please enter appointment date, it is a mandatory field")]
[EmailAddressAttribute(ErrorMessage = "Appointment date is invalid")]
public DateTime ApptDate;
[Prompt("Please enter {&}")]
public StatesOptions States;
public string SelectedState;
[Prompt("Please enter {&}")]
[Required(AllowEmptyStrings = false, ErrorMessage = "Please enter your Email, it is a mandatory field")]
[EmailAddressAttribute(ErrorMessage = "Email Id is invalid")]
public string NewEmail;
[Prompt("Please enter {&}")]
public BranchOptions? Branches;
public string SelectedBranch;
public static IForm<AppointmentQuery> BuildAppointmentBookingForm()
{
OnCompletionAsyncDelegate<AppointmentQuery> bookAppointment = async (context, state) =>
{
};
return new FormBuilder<AppointmentQuery>()
.Field(nameof(Name))
.Field(nameof(Email))
.Field(nameof(ApptDate))
//.Field(nameof(States))
.Field(new FieldReflector<AppointmentQuery>(nameof(States))
.SetActive((state) => true)
.SetPrompt(new PromptAttribute("Please select one of the state from the following options: {||}")
{
ChoiceStyle = ChoiceStyleOptions.Buttons
})
.SetDefine((state, field) =>
{
var data = new FeedStateData().feedData();
var states = data.Select(r => r.StateName);
foreach (var item in states)
{
field
.AddDescription(item, item)
.AddTerms(item, item);
//return true;
}
return Task.FromResult(true);
})
.SetNext((value, state) =>
{
var selection = (States)value;
state.SelectedState = selection.StateName.ToString();
return new NextStep(new[] { nameof(Branches) });
}))
.OnCompletion(bookAppointment)
.Build();
}
}
Also, I need to add another dynamic field Branches to it, but code doesn't reach it i.e. Branches, no exception is thrown but Form does not work.
Please find the sample code for Branches field as well
.Field(new FieldReflector<AppointmentQuery>(nameof(Branches))
.SetActive((state) => true)
.SetPrompt(new PromptAttribute("Please select branch from following options: {||}")
{
ChoiceStyle = ChoiceStyleOptions.Buttons
})
.SetDefine((state, field) =>
{
var data = new FeedStateData().feedData();
var states = data.Select(r => r.StateName);
var bracnhList = data.Where(s => s.StateName == state.SelectedState).Select(a => a.Branches).FirstOrDefault().Select(n => n.BranchName).ToList();
foreach (var item in bracnhList)
{
field
.AddDescription(item, item)
.AddTerms(item);
}
return Task.FromResult(true);
})
.SetNext((value, state) =>
{
var selection = (Branch)value;
state.SelectedBranch = selection.ToString();
return new NextStep(new[] { nameof(Branches) });
}))
Can someone help me with this?