Manikanta Reddy D Manikanta Reddy D - 1 month ago 8
C# Question

Calling Forms from Dialogs

I have a simple form,

[Serializable]
class CreateNewLeadForm
{
public string FirstName;
public string LastName;
public static IForm<CreateNewLeadForm> BuildForm()
{

return new FormBuilder<CreateNewLeadForm>()
.Message("Lets create a New Lead")
.Field(nameof(FirstName))
.Field(nameof(LastName))
.Build();

}
};


And a simple Dialog,

public class GreetDialog : IDialog<object>
{
public async Task StartAsync(IDialogContext context)
{

context.Wait(MessageReceivedAsync);
}
public async Task MessageReceivedAsync(IDialogContext context, IAwaitable<Message> argument)
{
context.Wait(MessageReceivedAsync);
}
}


How do I call a Initiate a FormDialog from the main Dialog itself? In general how do we intiate new dialogs within a Dialog?

Answer

In order to initiate a FormDialog you can just do:

var myform = new FormDialog<CreateNewLeadForm>(new CreateNewLeadForm(), CreateNewLeadForm.BuildForm, FormOptions.PromptInStart, null);

context.Call<CreateNewLeadForm>(myform, FormCompleteCallback);

Take a look to the PizzaBot for an example.

To initiate new dialogs within a Dialog you can do:

  • context.Call passing the instance of the new dialog and the completion callback (as in the form)
  • context.Forward where you can forward the message to the child dialog

    context.Forward(new MyChildDialog(), ResumeAfterChildDialog, message, CancellationToken.None);

Comments