Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
169 views
in Technique[技术] by (71.8m points)

c# - How to forward from RootDialog to LuisDialog in Bot Framework

I'm creating a bot for FAQ . When bot start conversations send a PromptDialog with 2 options: english, french.

I want to forward the dialog to EnglishLuis when user chooses English button, and FrenchLuis when choosing French.

Here is my code :

Rootdialog.cs

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 :

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

My problem is that when i choose English ( even French ) i got this error : here was an error sending this message to your bot: HTTP status code InternalServerError

Can you please help me how to start luis dialog ? P.S if i start from MessagesController.cs directly works good...but my intentions is to let people choose between two languages.

I try to call the luis using : await context.Forward(new EnglishLuis(), ResumeAfterDialog, message, CancellationToken.None); but without result .

new file RootDialog.cs ( updated ) :

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

}

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

First, doing

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

it's wrong. You need to choose: or you wait for a new message in the EnglishDialog or you end the EnglishDialog (with Done)

Then, you are trying to send a string in the context.Forward and you need to forward an IMessageActivity. And I suspect you want to send the original message, so you will need to save that in global variable before continuing with the prompt. Try with:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...