Multiple forms in Microsoft bot framework

441 views Asked by At

I have two forms. I need to access both the forms based on the user Input.

The forms are as follows

 internal static IDialog<JObject> BuildTravelForm()
    {
        travelstatus = 1;
        leaveStatus = 0;
        return Chain.From(() => FormDialog.FromForm(TravelForm.BuildForm))
            .Do(async (context, order) =>
            {
                try
                {
                    travelstatus = 0;
                    var completed = await order;
                    string source = (string)completed.GetValue("Question1");
                    string destination = (string)completed.GetValue("Question2");
                    await context.PostAsync("Your travel request is awaiting approval" + " " + "from" + " " + source + " " + "to" + " " + destination);
                }
                catch (Exception)
                {

                    await context.PostAsync("Thank you");
                }
            });
    }

The second one goes like this

internal static IDialog<JObject> BuildLeaveForm()
    {
        leaveStatus = 1;
        travelstatus = 0;
        return Chain.From(() => FormDialog.FromForm(LeaveForm.BuildForm))
            .Do(async (context,order)=>
        {
            leaveStatus = 0;
            var completed = await order;
            string startDate = (string)completed.GetValue("Question1");
            string endDate = (string)completed.GetValue("Question2");
            await context.PostAsync("Your leave is applied" + " " + "from" + " " + startDate + " " + "to" + " " + endDate);
        });
    }

The controller method is as follows

 public async Task<Activity> Post([FromBody]Activity activity)
    {

        try
        {
            if (activity.Type == ActivityTypes.Message)
            {
                if (leaveStatus == 1 && travelstatus==0)
                { 
                    //nested if to check intents to follow
                    await Conversation.SendAsync(activity, BuildLeaveForm);
                }
                else if(travelstatus == 1 && leaveStatus==0)
                {
                    await Conversation.SendAsync(activity, BuildTravelForm);
                }
                else
                {
                    ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl));
                    StateClient stateClient = activity.GetStateClient();
                    string replyMessage = "";
                    Luis stluis = await GetEntityFromLUIS(activity.Text);
                    if (stluis.intents.Count() > 0)
                    {
                        Activity reply;
                        ///await Conversation.SendAsync(activity, MakeGreetings);
                        using (var file = Assembly.GetExecutingAssembly().GetManifestResourceStream("Javis_V2.IntentLibrary.json"))
                        {
                            o2 = JObject.Parse(new StreamReader(file).ReadToEnd());
                            string luisIntent = stluis.intents[0].intent;
                            if (luisIntent == "LeaveManager")
                            {
                                await Conversation.SendAsync(activity, BuildLeaveForm);
                            }
                            else if(luisIntent=="TravelManager")
                            {
                                await Conversation.SendAsync(activity, BuildTravelForm);
                            }
                            else
                            {
                                leaveStatus = 0;
                                travelstatus = 0;
                                replyMessage = (string)o2.GetValue(luisIntent);
                                if(replyMessage=="")
                                {
                                    replyMessage = "Sorry! Not getting you";
                                }
                                reply = activity.CreateReply(replyMessage);
                                await connector.Conversations.ReplyToActivityAsync(reply);
                            }
                        }
                    }
                }
            }
            else
            {
                HandleSystemMessage(activity);
            }
            return null;

        }
        catch (Exception exp)
        {
            Debug.WriteLine(exp);
            return null;
        }
    }

The problem is that when the first form is completed, and when the second form is triggered through luis intent, the first form pops up. I am looking for something without the use of Dialogs.

Any kind of help is appreciated. Thanks in advance.

1

There are 1 answers

0
abhishek On

This line is the issue :

   if (leaveStatus == 1 && travelstatus==0)
                { 
                    //nested if to check intents to follow
                    await Conversation.SendAsync(activity, BuildLeaveForm);
                    await connector.Conversations.ReplyToActivityAsync(activity.CreateReply("Thanks"));
                }
                else if(travelstatus == 1 && leaveStatus==0)
                {
                    await Conversation.SendAsync(activity, BuildTravelForm);
                    await connector.Conversations.ReplyToActivityAsync(activity.CreateReply("Thanks"));
                }

If you see on the above code, the BuildTravelForm or BuildLeaveForm can clearly generate an error and end it without calling the Chain operation Do. If everything executes ok, it will call Do otherwise it will simply skip it. So the best place to reset the status is after the await statement which is ensured to be called after the completion of the dialog.

I will do like this :

   if (leaveStatus == 1 && travelstatus==0)
                { 
                    //nested if to check intents to follow
                    await Conversation.SendAsync(activity, BuildLeaveForm);
                    travelstatus =0; leaveStatus=0;
                    await connector.Conversations.ReplyToActivityAsync(activity.CreateReply("Thanks"));
                }
                else if(travelstatus == 1 && leaveStatus==0)
                {
                    await Conversation.SendAsync(activity, BuildTravelForm);
                    travelstatus =0; leaveStatus=0;
                    await connector.Conversations.ReplyToActivityAsync(activity.CreateReply("Thanks"));
                }

Do check if it works well.