首页 > 解决方案 > 如何在 Bot Framework 中将数据从 RootDialog 传递到 LuisDialog

问题描述

我正在尝试将我在 Rootdialog 中的用户名传递给我的 LuisDialog。但是 LuisDialog 中的所有意图只接受两个参数(IDialogContext context, LuisResult result),我不知道如何检索我从 `LuisResult 结果传递的数据。转发到 luis 对话框的代码如下:

await context.Forward(new Luis(), Resume, UserName, CancellationToken.None);

请问我该怎么做?哪个意图会收到数据?如何从 LuisResult 对象中检索数据?

标签: .netbotframeworkazure-language-understanding

解决方案


我正在尝试将我在 Rootdialog 中的用户名传递给我的 LuisDialog。

您可以尝试在 LuisDialog 类中定义一个构造函数来接受字符串类型参数,以便将用户名从 Rootdialog 传递给 LuisDialog。以下代码片段适用于我,您可以参考它。

在 LuisDialog 中:

[Serializable]
public class BasicLuisDialog : LuisDialog<object>
{

    private string uname = "";

    public BasicLuisDialog(string UserName) : base(new LuisService(new LuisModelAttribute(
        "{your_modelID_here}",
        "{your_subscriptionKey_here}", 
        domain: "{domain_here}")))
    {
        uname = UserName;
    }

    //....

    // Go to https://luis.ai and create a new intent, then train/publish your luis app.
    // Finally replace "Gretting" with the name of your newly created intent in the following handler
    [LuisIntent("Greeting")]
    public async Task GreetingIntent(IDialogContext context, LuisResult result)
    {
        await this.ShowLuisResult(context, result);
    }

    //....
    //for other intents
    //.... 

    private async Task ShowLuisResult(IDialogContext context, LuisResult result) 
    {
        await context.PostAsync($"You have reached {result.Intents[0].Intent}. UserName is : {uname}");
        context.Wait(MessageReceived);
    }
}

在 RootDialog 中:

var UserName = "Fei Han";
await context.Forward(new BasicLuisDialog(UserName), AfterLuis, activity, CancellationToken.None);

测试结果:

在此处输入图像描述


推荐阅读