首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在Bot框架下从RootDialog转发到LuisDialog

如何在Bot框架下从RootDialog转发到LuisDialog
EN

Stack Overflow用户
提问于 2017-08-02 23:03:36
回答 2查看 2.1K关注 0票数 0

我正在为FAQ创建一个机器人。当机器人开始对话时,发送一个带有两个选项的PromptDialog :英语,法语。

当用户选择英语按钮时,我想将对话框转发到EnglishLuis,而当用户选择法语时,我想将对话框转发到FrenchLuis。

下面是我的代码:

Rootdialog.cs

代码语言:javascript
复制
public class RootDialog : IDialog<object>
{
    private const string EnglishMenu = "English";
    private const string FrenchMenu = "French";
    private const string QAMenu = "Q&A";

    private List<string> mainMenuList = new List<string>() { EnglishMenu, FrenchMenu, QAMenu };
    private string location;

    public async Task StartAsync(IDialogContext context)
    {
        await context.PostAsync("Welcome to Root Dialog");
        context.Wait(MessageReceiveAsync);
    }

    private async Task MessageReceiveAsync(IDialogContext context, IAwaitable<IMessageActivity> result)
    {
        var reply = await result;
        if (reply.Text.ToLower().Contains("help"))
        {
            await context.PostAsync("You can implement help menu here");
        }
        else
        {
            await ShowMainmenu(context);
        }
    }

    private async Task ShowMainmenu(IDialogContext context)
    {
        //Show menues
        PromptDialog.Choice(context, this.CallDialog, this.mainMenuList, "What do you want to do?");
    }

    private async Task CallDialog(IDialogContext context, IAwaitable<string> result)
    {
        //This method is resume after user choise menu
       // this.luisResult = result;
       // var message = await result;
        var selectedMenu = await result;
        var message = await result;
        switch (selectedMenu)
        {
            case EnglishMenu:
                //Call child dialog without data
               //  context.Call(new EnglishLuis(),ResumeAfterDialog);
                //  context.Call(new EnglishLuis(), ResumeAfterDialog);

               await Conversation.SendAsync(context.MakeMessage(), () => new EnglishLuis());
                break;
            case FrenchMenu:
                //Call child dialog with data
                context.Call(new HotelDialog(location), ResumeAfterDialog);
                break;
            case QAMenu:
                context.Call(new LuisCallDialog(),ResumeAfterDialog);
                break;
        }

    }



    private async Task ResumeAfterDialog(IDialogContext context, IAwaitable<object> result)
    {
        //Resume this method after child Dialog is done.
        var test = await result;
        if (test != null)
        {
            location = test.ToString();
        }
        else
        {
            location = null;
        }
        await this.ShowMainmenu(context);
    }
}

}

EnglishLuis.cs:

代码语言:javascript
复制
 public class EnglishLuis : LuisDialog<object>
{
    private string location;



    //   string message = $"welcome to english dialog";

    public async Task None(IDialogContext context, LuisResult result)
    {
        string message = $"Sorry, I did not understand '{result.Query}'. Please try again";

        await context.PostAsync(message);

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


    [LuisIntent("gretting")]
    [LuisIntent("intentfr")]
    public async Task Greeting(IDialogContext context, IAwaitable<IMessageActivity> activity, LuisResult result)
    {


        await context.PostAsync("Welcome :) ");


        context.Wait(MessageReceived);
        context.Done(true);
    }



    [LuisIntent("test")]
    public async Task test(IDialogContext context, IAwaitable<IMessageActivity> activity, LuisResult result)
    {
        await context.PostAsync("Do you want to test our bot ? We suggest to type : hi or who are you, help etc..");
        // context.Done(true);
        context.Wait(MessageReceived);
        context.Done(true);
    }

我的问题是,当我选择英语(甚至法语)时,我得到了这样的错误:向你的机器人发送以下消息时出错: HTTP状态码InternalServerError

你能告诉我如何启动luis dialog吗?附注:如果我从MessagesController.cs directly good...but开始,我的意图是让人们在两种语言之间进行选择。

我尝试使用以下命令调用luis : await context.Forward(new EnglishLuis(),ResumeAfterDialog,message,CancellationToken.None);但没有结果。

新文件RootDialog.cs (已更新):

代码语言:javascript
复制
   using System;
   using System.Collections.Generic;
   using System.Threading.Tasks;
    using Microsoft.Bot.Builder.Dialogs;
   using Microsoft.Bot.Connector;
    using System.Threading;

   namespace TeamsBot.Dialogs
    {
[Serializable]
public class RootDialog : IDialog<object>
{
    private const string EnglishMenu = "English";
    private const string FrenchMenu = "French";
    private const string QAMenu = "Q&A";

    private List<string> mainMenuList = new List<string>() { EnglishMenu, 
        FrenchMenu, QAMenu };
    private string location;

    private string originalMessage;

    public async Task StartAsync(IDialogContext context)
    {
        await context.PostAsync("Welcome to Root Dialog");
        context.Wait(MessageReceiveAsync);
    }

    private async Task MessageReceiveAsync(IDialogContext context, 
     IAwaitable<IMessageActivity> result)
    {
        var reply = await result;

        this.originalMessage = reply.Text;




        if (reply.Text.ToLower().Contains("help"))
        {
            await context.PostAsync("You can implement help menu here");
        }
        else
        {
            await ShowMainmenu(context);
        }
    }

    private async Task ShowMainmenu(IDialogContext context)
    {
        //Show menues
        PromptDialog.Choice(context, this.CallDialog, this.mainMenuList, 
   "What do you want to do?");
    }

    private async Task CallDialog(IDialogContext context, IAwaitable<string> 
    result)
    {

        var selectedMenu = await result;
        switch (selectedMenu)
        {
            case EnglishMenu:
                //Call child dialog without data
                var newMessage = context.MakeMessage();
                newMessage.Text = reply.Text; 
                 await context.Forward(new EnglishLuis(), ResumeAfterDialog, newMessage, CancellationToken.None);
                break;
            case FrenchMenu:
                //Call child dialog with data
                //   context.Call(new HotelDialog(location), ResumeAfterDialog);

                var frenchLuis = new FrenchLuis();
                var messageToForward = await result;
             //   await context.Forward(new FrenchLuis(), ResumeAfterDialog, messageToForward, CancellationToken.None);
                break;
            case QAMenu:
                context.Call(new LuisCallDialog(),ResumeAfterDialog);
                break;
        }

    }



    private async Task ResumeAfterDialog(IDialogContext context, IAwaitable<object> result)
    {
        //Resume this method after child Dialog is done.
        var test = await result;
        if (test != null)
        {
            location = test.ToString();
        }
        else
        {
            location = null;
        }
        await this.ShowMainmenu(context);
    }
}

}

EN

回答 2

Stack Overflow用户

发布于 2017-08-02 23:51:58

首先,做

代码语言:javascript
复制
context.Wait(this.MessageReceived);
context.Done(true);

这是错误的。您需要选择:或者等待EnglishDialog中的新消息,或者结束EnglishDialog (使用Done)

然后,您尝试在context.Forward中发送一个字符串,并且需要转发一个IMessageActivity。我怀疑您想要发送原始消息,因此在继续提示之前,您需要将其保存在全局变量中。尝试使用:

代码语言:javascript
复制
var newMessage = context.MakeMessage();
newMessage.Text = this.originalMessageText //the variable that contains the text of the original message that you will have to save at MessageReceiveAsync
await context.Forward(new EnglishLuis(), ResumeAfterDialog, newMessage, CancellationToken.None);


MessageReceivedAsync in RootDialog should looks like:

 private async Task MessageReceiveAsync(IDialogContext context, IAwaitable<IMessageActivity> result)
    {
        var reply = await result;
        if (reply.Text.ToLower().Contains("help"))
        {
            await context.PostAsync("You can implement help menu here");
        }
        else
        {
            this.originalMessage = reply.Text;
            await ShowMainmenu(context);
        }
    }
票数 2
EN

Stack Overflow用户

发布于 2018-06-04 04:35:03

这就是我实现调用不同对话框的方法的方式。我倾向于使用对话框的依赖注入,所以我不需要不断地更新它们。

代码语言:javascript
复制
private async Task CallDialog(IDialogContext context, IAwaitable<string> result)
{
    //These two variables will be exactly the same, you only need one     
    //var selectedMenu = await result;
    var message = await result;
    switch (selectedMenu)
    {
        case EnglishMenu:
            // Forward the context to the new LuisDialog to bring it to the top of the stack.  
            // This will also send your message to it so it gets processed there.
            await context.Forward<object>(new EnglishLuis(), ResumeAfterDialog, message , CancellationToken.None);
            break;
        case FrenchMenu:
             await context.Forward<object>(new HotelDialog(location), ResumeAfterDialog, message , CancellationToken.None);
            break;
        case QAMenu:
             await context.Forward<object>(new LuisCallDialog(), ResumeAfterDialog, message , CancellationToken.None);
            context.Call(new LuisCallDialog(),ResumeAfterDialog);
            break;
    }

}

您正在使用的EnglishLuis对话框中存在问题:

代码语言:javascript
复制
context.Wait(this.MessageReceived);
context.Done(true);

问题是这两行代码在通过对话框时都会执行。context.Done将导致此对话框离开堆栈,因此您最终将转到前一个对话框,这与您正在尝试等待响应的事实相冲突。

除非您想返回到上一个对话框,否则您的luis对话框中实际上不应该有context.Done。因此,如果您选择使用context.Done,请将其放在resumeAfter方法中,并设置一个适当的条件,或者使用一个退出程序这一部分的单一意图。

您没有包含堆栈跟踪,但是如果您使用的是来自美国以外地区的Luis,那么在使用Luis时可能会出现问题。在这种情况下,您需要相应地设置域指向正确的Luis服务的属性。

代码语言:javascript
复制
public EnglishLuis(ConstructorParameters parameters) 
    : base(new LuisService(new LuisModelAttribute(
        "<AppId>",
        "<SubscriptionKey>",
        domain: "westeurope.api.cognitive.microsoft.com")))
    {
        // Constructor Stuff...
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45464455

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档