最佳实践是否总是在bot中启动对话框? [英] Is the best practice alwayas to start a dialog in bot?

查看:91
本文介绍了最佳实践是否总是在bot中启动对话框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将机器人V3迁移到V4,并且运行良好,但是该机器人具有一些规则,并且在实施这些规则时遇到了一些困难.

I am migrating a bot V3 to V4 and it is working well, but the bot has some rules and I am having some difficulties to implement these rules.

其中一个规则会在致电Luis之后启动对话框,因为Luis意识到了这一需求.

One of these rules starts dialog after call Luis, in this Luis recognizes the need.

我的疑问是:bot的最佳实践是什么?总是启动对话框还是仅在必要时启动?

My doubt is: what is the best practice to bot? Always starts a dialog or only starts if necessary?

PS:在我看来,如有必要,我必须重新开始,但我对此表示怀疑.

PS: in my mind, I have to start if necessary, but I am in doubt.

if (Luis.CheckDialogNeed)
{
   await Dialog.RunAsync(turnContext, ConversationState.CreateProperty<DialogState>(nameof(DialogState)), cancellationToken)
}

如果答案是在必要时启动对话框,那么如何在对话框类之外获取对话框的文本.例如,在机器人课程中?

If the answer is to start dialog if necessary, how to get dialog's texts outside dialog class. For example, in bot class?

推荐答案

对话框使用对话框上下文启动,继续和结束,该对话框上下文是打开上下文和对话框集.通过对话框状态属性访问器创建一个对话框集.请熟悉对话框文档.我想您会对此内容特别感兴趣在使用漫游器状态和创建属性访问器时. 也包含一些使用对话框的代码示例.

Dialogs are started, continued, and ended using a dialog context, which is a combination of a turn context and a dialog set. A dialog set is created from a dialog state property accessor. Please familiarize yourself with the dialogs documentation. I think you'll be particularly interested in this when it comes to using bot state and creating property accessors. This also contains some code examples for using dialogs.

就像我说的那样,当前大多数 Bot Builder示例的构建方式使所有bot逻辑都可以在对话框中执行.从一个对话框中开始一个新对话框很容易,因为新对话框刚刚被添加到对话框堆栈中.如果您使用的是组件对话框,则甚至可以在对话框中包含对话框.

Like I said, most of the current Bot Builder samples are built in such a way that all bot logic gets executed inside a dialog. Starting a new dialog from within a dialog is easy because the new dialog just gets added to the dialog stack. If you're using a component dialog then you can even have dialogs inside your dialog.

旧样本和模板的工作方式略有不同. Bot状态对象(例如对话状态和用户状态)使用依赖项注入传递给bot类,并且这些bot状态对象用于创建状态属性访问器,然后创建对话框集.然后,bot类将在其OnTurnAsync处理程序中创建一个对话框上下文,并尝试继续当前对话框或开始一个新对话框,或者仅发送一个单向消息,具体取决于对话框堆栈和传入消息.您可以在git仓库中检出较早的提交,以查看实际操作,也可以检出新的

The old samples and templates worked a bit differently. Bot state objects (like conversation state and user state) were passed to the bot class using dependency injection, and those bot state objects were used to create state property accessors and then dialog sets. The bot class would then create a dialog context in its OnTurnAsync handler and try to continue the current dialog or begin a new one or just send a one-turn message depending on the dialog stack and the incoming message. You can check out older commits in the git repo to see this in action, or you can check out the new active learning sample because it hasn't been updated to match the pattern of the other samples yet. The bot's constructor looks like this:

public ActiveLearningBot(ConversationState conversationState, UserState userState, IBotServices botServices)
{
    botServices = botServices ?? throw new ArgumentNullException(nameof(botServices));
    if (botServices.QnAMakerService == null)
    {
        throw new ArgumentException($"Invalid configuration. Please check your '.bot' file for a QnA service.");
    }

    ConversationState = conversationState;
    UserState = userState;

    // QnA Maker dialog options
    QnaMakerOptions = new QnAMakerOptions
    {
        Top = 3,
        ScoreThreshold = 0.03F,
    };

    _dialogs = new DialogSet(ConversationState.CreateProperty<DialogState>(nameof(DialogState)));

    _dialogHelper = new DialogHelper(botServices);

    _dialogs.Add(_dialogHelper.QnAMakerActiveLearningDialog);
}

然后这是OnTurnAsync中的相关代码:

And then this is the relevant code in OnTurnAsync:

var dialogContext = await _dialogs.CreateContextAsync(turnContext, cancellationToken);
var results = await dialogContext.ContinueDialogAsync(cancellationToken);
switch (results.Status)
{
    case DialogTurnStatus.Cancelled:
    case DialogTurnStatus.Empty:
        await dialogContext.BeginDialogAsync(_dialogHelper.ActiveLearningDialogName, QnaMakerOptions, cancellationToken);
        break;
    case DialogTurnStatus.Complete:
        break;
    case DialogTurnStatus.Waiting:
        // If there is an active dialog, we don't need to do anything here.
        break;
}

// Save any state changes that might have occured during the turn.
await ConversationState.SaveChangesAsync(turnContext, false, cancellationToken);
await UserState.SaveChangesAsync(turnContext, false, cancellationToken);

这篇关于最佳实践是否总是在bot中启动对话框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆