Microsoft bot框架中的多个表单

时间:2016-12-26 06:29:43

标签: c# botframework luis

我有两种形式。我需要根据用户输入访问这两个表单。

表格如下

 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");
                }
            });
    }

第二个是这样的

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);
        });
    }

控制器方法如下

 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;
        }
    }

问题在于,当第一个表单完成时,以及通过luis intent触发第二个表单时,会弹出第一个表单。我正在寻找一些不使用Dialogs的东西。

感谢任何形式的帮助。 提前谢谢。

1 个答案:

答案 0 :(得分:0)

这一行是问题所在:

   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"));
                }

如果您看到上面的代码,BuildTravelForm或BuildLeaveForm可以清楚地生成错误并结束它而不调用Chain操作Do.如果一切正常,它将调用Do否则它将跳过它。因此,重置状态的最佳位置是在await语句之后,该语句确保在对话框完成后调用。

我会这样做:

   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"));
                }

检查它是否运作良好。