Azka Gilani
Azka Gilani

Reputation: 45

How to switch between multiple formflows in Microsoft bot framework?

i have a basic form. Depending on the choices of the user, it will direct it to various formflows. But i am unable to achieve this. It repeats first formFlow again and again in Microsoft BOT framework ?

//These are two forms that i have initiated. If a count is 1 then it must open first formflow otherwise the second formflow.
    internal static IDialog<General> MakeRootDialog()
    {
        return Chain.From(() => FormDialog.FromForm(General.BuildForm));
    }
    internal static IDialog<ProfileForm> MakeRootDialog1()
    {
        return Chain.From(() => FormDialog.FromForm(ProfileForm.BuildForm));
    }
      public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
    {
        if (activity.Type == ActivityTypes.Message && General.count == 0)
        {
            await Conversation.SendAsync(activity, MakeRootDialog);
            General.count = 1;

        }
        else if(activity.Type == ActivityTypes.Message && General.count >= 1)
        {
            await Conversation.SendAsync(activity, MakeRootDialog1);
        }
        else
        {
            HandleSystemMessage(activity);
        }
        var response = Request.CreateResponse(HttpStatusCode.OK);
        return response;
    }

Upvotes: 1

Views: 672

Answers (1)

Ezequiel Jadib
Ezequiel Jadib

Reputation: 14787

I was able to repro the problem but I'm still thinking how that could be solved using Chains in the MessageController.

My suggestion to unblock you is to move the "IF" logic for the Forms into a separate dialog. Something like the following:

Controller

public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
{
    if (activity.Type == ActivityTypes.Message)
    {
        await Conversation.SendAsync(activity, () => new RootDialog());
    }
    else
    {
        HandleSystemMessage(activity);
    }

    var response = Request.CreateResponse(HttpStatusCode.OK);
    return response;
}

RootDialog

[Serializable]
public class RootDialog : IDialog<object>
{
    public async Task StartAsync(IDialogContext context)
    {
        context.Wait(this.MessageReceivedAsync);
    }

    private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> result)
    {
        if (General.count == 0)
        {
            General.count = 1;

            context.Call(FormDialog.FromForm<General>(General.BuildForm, FormOptions.PromptInStart), async (ctx, formResult) => ctx.Wait(this.MessageReceivedAsync));
        }
        else if (General.count >= 1)
        {
            context.Call(FormDialog.FromForm<ProfileForm>(ProfileForm.BuildForm, FormOptions.PromptInStart), async (ctx, formResult) => ctx.Wait(this.MessageReceivedAsync));
        }
    }
}

This is a personal opinion => I prefer to use Dialogs since once the bot start to grow it's easier to follow the logic and to separate the components.

Upvotes: 2

Related Questions