如何退出qnadialog并继续其他luis意图

时间:2017-08-22 11:37:19

标签: c# botframework luis

我使用luis和qnamaker对话框创建了一个机器人。在我的问题中是LuisDialog.cs代码的一部分。在对话期间,如果用户提出的问题是qna意图的一部分(机器人jumb到QnADialog),但我想在用户向机器人提出另一个问题时传递给其他意图。

这里的LuisDialog.cs是我用其他意图更新的代码。当用户输入与测试意图相对应的问题时,我想从qnadialog退出,例如

   using Microsoft.Bot.Builder.Dialogs;
  using System;
  using System.Collections.Generic;
  using System.Linq;
   using System.Web;
  using Microsoft.Bot.Builder.Luis;
   using System.Threading.Tasks;
  using Microsoft.Bot.Builder.Luis.Models;
   using Microsoft.Bot.Connector;
   using MultiDialogsBot.Dialogs;
    using System.Threading;

  namespace MultiDialogsBot
  {

[LuisModel("xxxxxxx", "yyyyyyyyyyy")]
[Serializable]
public class LuisDialog : LuisDialog<object>
{
    private object activity;


    public async Task StartAsync(IDialogContext context)
    {
        context.Wait(MessageReceived);
    }


    [LuisIntent("None")]
    [LuisIntent("")]
    public async Task None(IDialogContext context, LuisResult result)
    {
        string message = $"Désolé je n'ai pas compris '{result.Query}'. Veuillez formuler votre question";

        await context.PostAsync(message);

        context.Wait(this.MessageReceived);


    }

    [LuisIntent("test")]

    public async Task test(IDialogContext context, LuisResult result)
    {
        await context.PostAsync("nous testons");
        context.Wait(MessageReceived);

    }



    [LuisIntent("qna")]
    public async Task qna(IDialogContext context, IAwaitable<IMessageActivity> activity, LuisResult result)
    {
        var msg = await activity;

        //  await context.Forward(new QnADialog(), ResumeAfterOptionDialog, msg, CancellationToken.None);
        context.Call(new QnADialog(), this.ResumeAfterOptionDialog);

    }

    public async Task ResumeAfterOptionDialog(IDialogContext context, IAwaitable<object> result)
    {
        var messageHandled = await result;
        if (messageHandled != null)
        {
            await context.PostAsync("Désolé je n'ai pas compris");
            context.Wait(MessageReceived);
        }
    }

2 个答案:

答案 0 :(得分:1)

如果您想首先处理QnA数据库中的用户输入,您应该更改问题中描述的逻辑并设置一个覆盖的QnADialog,它将首先获得您的用户输入,并且当没有回复时,调用LuisDialog以尝试用1个或几个有趣的意图来处理这个案例。

您可以查看here QnAMakerDialog是如何制作的。您将看到您可能需要重写该类以更改MessageReceivedAsync方法以避免来自QnAMakerDialog的回复:

if (sendDefaultMessageAndWait)
{
    // The following line should be removed if you don't want that the QnADialog replies if no answer found
    await context.PostAsync(qnaMakerResults.ServiceCfg.DefaultMessage);
    await this.DefaultWaitNextMessageAsync(context, message, qnaMakerResults);
}

你的QnAOverriddenDialog必须从之前调用LuisDialog的地方调用(我猜你的MessageController,因为我没有你的实现细节)。

您的LuisDialog将如下所示:

[LuisModel("xxxxxxx", "yyyyyyyyyyy")]
[Serializable]
public class LuisDialog : LuisDialog<object>
{
    public async Task StartAsync(IDialogContext context)
    {
        context.Wait(MessageReceived);
    }

    [LuisIntent("None")]
    [LuisIntent("")]
    public async Task None(IDialogContext context, LuisResult result)
    {
        string message = $"Désolé je n'ai pas compris '{result.Query}'. Veuillez formuler votre question";

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

    [LuisIntent("test")]
    public async Task test(IDialogContext context, LuisResult result)
    {
        await context.PostAsync("nous testons");
        context.Wait(MessageReceived);
    }

    [LuisIntent("yourOtherIntent1")]
    public async Task OtherIntent1(IDialogContext context, LuisResult result)
    {
        await context.PostAsync("fallback 1");
        context.Wait(MessageReceived);
    }

    [LuisIntent("yourOtherIntent2")]
    public async Task OtherIntent1(IDialogContext context, LuisResult result)
    {
        await context.PostAsync("fallback 2");
        context.Wait(MessageReceived);
    }

    public async Task ResumeAfterOptionDialog(IDialogContext context, IAwaitable<object> result)
    {
        var messageHandled = await result;
        if (messageHandled != null)
        {
            await context.PostAsync("Désolé je n'ai pas compris");
            context.Wait(MessageReceived);
        }
    }
}

答案 1 :(得分:0)

尝试:

[LuisIntent("qna")]
public async Task qna(IDialogContext context, IAwaitable<IMessageActivity> activity, LuisResult result)
{
    var msg = await activity;
    await context.Forward(new QnADialog(), ResumeAfterQnA, context.Activity, CancellationToken.None);
    await this.ShowLuisResult(context, result);
}

private async Task ResumeAfterQnA(IDialogContext context, IAwaitable<object> result)
        {
            context.Done<object>(null);
        }

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