Microsoft Bot框架,路易斯,当消息没有意图时采取一些措施



我最近开始学习Microsoft Bot框架,所以我开始做一个聊天机器人,我想我把它做错了

我这样做聊天机器人:

->我得到了消息的用户
->发送到路易斯
->获得意图和实体
->选择我的答案并发送。

没关系,但是得到以下情况:

用户:我想更改我的电子邮件。 ->意图:changeinfo实体: 电子邮件/价值:电子邮件

聊天机器人:请告诉我您的新电子邮件。 ->意图:Nointent 实体:Noentities

用户:email@email.com->意图:idon'twownge enstities: 电子邮件/值:电子邮件@email.com

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

我在github上的代码,我知道这是一个丑陋的代码,但是我只是为了理解bot框架,在我让此代码更加美丽

之后

这应该像使用luisdialog和一组提示来管理用户流动一样简单。在下面,您会找到一些我编写的快速代码,以向您展示如何完成。您不需要额外的步骤或添加额外的实体,也不需要用用户提供的电子邮件去Luis。

我建议您更多地阅读有关Luisdialog和对话的更多信息,因为您在控制器中使用Luis的方式,我认为这是不可能的。

这是一个很好的路易斯样本,在这里是多边形的好样本。

示例代码

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

在我的机器人流中,我使用的是从前端更改的step变量。以及我从机器人更改的另一个step变量。这有助于我确定对话中的哪一步。您可以做同样的事情来确定机器人的询问。

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

如果您不想将特定步骤发送给路易斯以识别识别,则可以在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--- fefault Handlers

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

最新更新