Search code examples
c#botframework

Custom FormDialog Description with form state value : MS BOT C#


Bot: Select any leave type? LeaveQueryRequest.LeaveType
User: Annual leave
Bot: would you like to give the reason for applying annual leave? LeaveQueryRequest.isComment

Here annual leave is what user selected as leave type. So, for isComment prompt/description value is based on user input.

.Field(new FieldReflector<LeaveRequestQuery>(nameof(LeaveRequestQuery.isComment)) .SetFieldDescription($"Would you like to give comments/reasons for taking {LeaveType} ") .SetNext((value, state) => {

am trying to set dynamic prompt/description for the form fields based on the previous input for the form. Like already user has chosen leave type, from date and to date. So, those values should prompt in next questions.


Solution

  • You have a few choices. It would be very easy to use pattern language, and you've almost stumbled upon how to do that. But you'd want to put the pattern in a prompt instead of a description, and you'd want to not use an interpolated string because FormFlow handles the formatting:

    .Field(new FieldReflector<LeaveRequestQuery>(nameof(LeaveRequestQuery.isComment)).SetPrompt("Would you like to give comments/reasons for taking {LeaveType} ")
    

    This can also be simplified with an overload of the Field method:

    .Field(nameof(LeaveRequestQuery.isComment), "Would you like to give comments/reasons for taking {LeaveType} ")
    

    You can also use a delegate that defines your field dynamically and can therefore draw on the current state of your form when that field is reached:

    .Field(new FieldReflector<LeaveRequestQuery>(nameof(LeaveRequestQuery.isComment)).SetDefine(async (state, field) =>
    {
        field.SetPrompt(new PromptAttribute($"Would you like to give comments/reasons for taking {state.LeaveType} "));
        return true;
    }))
    

    You can also use a prompt attribute where you declare your field instead of where you build your form:

    [Prompt("Would you like to give comments/reasons for taking {LeaveType} ")]
    public bool isComment { get; set; }