BotFramework:使用Intents

时间:2016-07-04 11:32:46

标签: bots botframework

关于Microsoft Bot Framework,我们都知道Microsoft提供的samples。然而,这些样品通常具有“单一目的”,即Pizzabot仅用于订购比萨饼,等等。

事情是,我希望创造一个更复杂的Bot,它实际上回答了一系列事情。为此,我创建了一个“大厅”对话框,其中所有消息都使用此MessageController:

 return await Conversation.SendAsync(message, () => new LobbyDialog());

在“Lobby”对话框中,我有一系列LUIS意图用于不同的事情,并且因为它根据意图选择任务,所以它很有效。

然而,对于更复杂的操作,我希望使用FormFlow机制,所以我可以使用像PizzaBot示例中的表单。问题是所有采样的“表单机器人”始终使用此消息控制器类型:

return Chain.From(() => new PizzaOrderDialog(BuildForm)

在同一个MessagesController中,建立构建器流程,如下所示:

var builder = new FormBuilder<PizzaOrder>();

        ActiveDelegate<PizzaOrder> isBYO = (pizza) => pizza.Kind == PizzaOptions.BYOPizza;
        ActiveDelegate<PizzaOrder> isSignature = (pizza) => pizza.Kind == PizzaOptions.SignaturePizza;
        ActiveDelegate<PizzaOrder> isGourmet = (pizza) => pizza.Kind == PizzaOptions.GourmetDelitePizza;
        ActiveDelegate<PizzaOrder> isStuffed = (pizza) => pizza.Kind == PizzaOptions.StuffedPizza;

        return builder
            // .Field(nameof(PizzaOrder.Choice))
            .Field(nameof(PizzaOrder.Size))
            .Field(nameof(PizzaOrder.Kind))
            .Field("BYO.Crust", isBYO)
            .Field("BYO.Sauce", isBYO)
            .Field("BYO.Toppings", isBYO)
            .Field(nameof(PizzaOrder.GourmetDelite), isGourmet)
            .Field(nameof(PizzaOrder.Signature), isSignature)
            .Field(nameof(PizzaOrder.Stuffed), isStuffed)
            .AddRemainingFields()
            .Confirm("Would you like a {Size}, {BYO.Crust} crust, {BYO.Sauce}, {BYO.Toppings} pizza?", isBYO)
            .Confirm("Would you like a {Size}, {&Signature} {Signature} pizza?", isSignature, dependencies: new string[] { "Size", "Kind", "Signature" })
            .Confirm("Would you like a {Size}, {&GourmetDelite} {GourmetDelite} pizza?", isGourmet)
            .Confirm("Would you like a {Size}, {&Stuffed} {Stuffed} pizza?", isStuffed)
            .Build()
            ;

我的重要问题是,是否可以使用我使用的MessagesController开始对话,然后在LobbyDialog中,使用触发Form的Intent并返回它?也就是说,从对话框开始流程?或者最好使用DialogChains吗?

因为,根据我的尝试,似乎我只能使用我描述的方法从MessagesController类调用它们,即Microsoft在Pizzabot中如何对其进行采样。

感谢您对此事的任何帮助或意见。谢谢你的时间。

1 个答案:

答案 0 :(得分:7)

当然可以!从对话框中实例化表单是一种非常常见的情况。为此,您可以在LUIS intent方法中执行以下操作:

var form = new FormDialog<YourFormModel>(
                <ExistingModel>,
                <TheMethodThatBuildTheForm>,
                FormOptions.PromptInStart,
                result.Entities);

context.Call(form, <ResumeAfterCallback>);

使用PizzaBot示例,它应该如下所示:

var form = new FormDialog<PizzaOrder>(
                    null,
                    BuildForm,
                    FormOptions.PromptInStart,
                    result.Entities);

    context.Call(form, <ResumeAfterCallback>);

在ResumeAfterCallback中,您通常会获得表单的结果,捕获异常并执行上下文。等等,以便对话框可以继续接收消息。下面是一个简单的例子:

    private async Task ResumeAfterCallback(IDialogContext context, 
                                           IAwaitable<PizzaOrder> result)
    {
        try
        {
            var pizzaOrder = await result;
            // do something with the pizzaOrder

            context.Wait(this.MessageReceived);
        }
        catch (FormCanceledException<PizzaOrder> e)
        {
            string reply;

            if (e.InnerException == null)
            {
                 reply = "You have canceled the operation. What would you like to do next?";
            }
            else
            {
                  reply = $"Oops! Something went wrong :(. Technical Details: {e.InnerException.Message}";
            }

             await context.PostAsync(reply);
             context.Wait(this.MessageReceived);
        }
}