Microsoft Bot Framework,LUIS,在消息没有意图时采取一些行动

Microsoft Bot Framework, LUIS, take some action when the message dont have a intent

我最近开始学习Microsoft Bot Framework,所以我开始制作Chatbot,我想我做错了

我是这样制作聊天机器人的:

--> I get the message's user
--> send to LUIS
--> get the intent and the entities
--> select my answer and send it.

没问题,但是遇到以下情况:

USER: I wanna change my email. --> intent : ChangeInfo entities: email/value:email

CHATBOT: Tell me your new Email please. --> intent: noIntent entities: noEntities

USER: email@email.com --> intent: IDon'tKnow entities: email/value:email@email.com

我接受这种情况,当用户发送他的电子邮件时,我发送给 LUI,但是电子邮件没有意图,只有一个实体,但是电子邮件可以在很多不同的情况下使用,我的问题是,我的机器人如何知道对话的上下文以了解此电子邮件是用于更改电子邮件而不是发送电子邮件,或更新此电子邮件或其他内容。

我在gitHubhere上的代码,我知道它是一个丑陋的代码,但我这样做只是为了了解bot框架,之后我会让这段代码更漂亮

在我的机器人流程中,我使用了从前端更改的 step 变量。我从机器人更改的另一个 step 变量。这有助于我确定我在对话中的哪一步。您可以执行相同的操作来确定您的机器人向用户询问的内容。

var data = {step: "asked_email"};
var msg = builder.Message(session).addEntity(data).text("Your message.");
session.send(msg);

如果您不想将特定步骤发送到 LUIS 进行识别,您可以在 onBeginDialog 处理程序中进行处理:

intents.onBegin(function (session, args, next) {
  if (session.message.step !== "email") {
    next();
  } else {
    //Do something else and not go to LUIS.
    session.endDialog();
  }
});

您可以在此处找到对 LUIS onBeginDialog 的引用: https://docs.botframework.com/en-us/node/builder/chat/IntentDialog/#onbegin--ondefault-handlers

有关消息数据的详细信息可在此处找到: https://docs.botframework.com/en-us/node/builder/chat-reference/classes/_botbuilder_d_.message.html#entities

这应该像使用 LuisDialog 和一组提示来管理用户流一样简单。您将在下面找到一些我放在一起的快速代码,向您展示如何完成此操作。您不需要额外的步骤或添加额外的实体,或使用用户提供的电子邮件转到 Luis。

我建议您阅读更多关于 LuisDialog 和 Dialogs 的内容,因为我认为您在控制器中使用 Luis 的方式不正确。

Here is a good Luis Sample and here a good one around multi-dialogs.

示例代码

namespace MyNamespace
{
    using System;
    using System.Threading.Tasks;
    using Microsoft.Bot.Builder.Dialogs;
    using Microsoft.Bot.Builder.Internals.Fibers;
    using Microsoft.Bot.Builder.Luis;
    using Microsoft.Bot.Builder.Luis.Models;
    using Microsoft.Bot.Connector;

    [Serializable]
    [LuisModel("YourModelId", "YourSubscriptionKey")]
    public class MyLuisDialog : LuisDialog<object>
    {
        [LuisIntent("")]
        [LuisIntent("None")]
        public async Task None(IDialogContext context, LuisResult result)
        {
            string message = "Não entendi, me diga com outras palavras!";

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

        [LuisIntent("ChangeInfo")]
        public async Task ChangeInfo(IDialogContext context, IAwaitable<IMessageActivity> activity, LuisResult result)
        {
            // no need to go to luis again..
            PromptDialog.Text(context, AfterEmailProvided, "Tell me your new email please?");
        }

        private async Task AfterEmailProvided(IDialogContext context, IAwaitable<string> result)
        {
            try
            {
                var email = await result;

                // logic to store your email...
            }
            catch
            {
                // here handle your errors in case the user doesn't not provide an email
            }

          context.Wait(this.MessageReceived);
        }

        [LuisIntent("PaymentInfo")]
        public async Task Payment(IDialogContext context, IAwaitable<IMessageActivity> activity, LuisResult result)
        {
            // logic to retrieve the current payment info..
            var email = "test@email.com";

            PromptDialog.Confirm(context, AfterEmailConfirmation, $"Is it {email} your current email?");
        }

        private async Task AfterEmailConfirmation(IDialogContext context, IAwaitable<bool> result)
        {
            try
            {
                var response = await result;

                // if the way to store the payment email is the same as the one used to store the email when going through the ChangeInfo intent, then you can use the same After... method; otherwise create a new one
                PromptDialog.Text(context, AfterEmailProvided, "What's your current email?");
            }
            catch
            {
                // here handle your errors in case the user doesn't not provide an email
            }

            context.Wait(this.MessageReceived);
        }
    }
}