4

介绍

目前我正在尝试使用 Microsoft Bot Framework v4 创建一个 Bot Framework 应用程序。

程序结构

我们目前有以下设置:

机器人类的根被命名为:SubDialogBotBot

在其中SubDialogBot我们创建一个名为 的新对话框ParentDialog。此 Dialog 负责对特定 Intent 做出反应。

然后我们从ParentDialog命名的ChildDialog. 此子对话框将负责根据传递的参数向用户询问问题ParentDialog

完成此问题后,我们要返回ParentDialog并继续流程。

在这个例子中,我们想要重用ChildDialog来自各种不同意图的,因为这里的代码是完全相同的。唯一改变的是必须向用户提出的问题。

问题

ChildDialog完成时,“流程”永远不会返回到ParentDialog.

我们还尝试在ChildDialogID 设置为特定内容之后让 Dialog 跟随,然后使用 Context.BeginDialog(....) 从ChildDialog. 但是,因为显然对话框被添加到ParentDialog而不是ChildDialog它无法通过 id 找到它。

Github 仓库重现问题

https://github.com/devedse/SubDialogBotReproduction

4

1 回答 1

2

首先,这是一个准备充分的问题,谢谢……特别是分享代码。

现在,好消息是我认为您的对话没有任何问题。问题实际上出在您的机器人的OnTurnAsync. 你只需要调用BeginDialogAsync你的ParentDialog. 每一个活动都将通过你进入OnTurnAsync,这意味着你负责处理重新进入对话框堆栈。这意味着,您需要检查是否有活动对话,如果有,则需要调用ContinueDialogAsync以从讨论中断的地方继续。这是您当前OnTurnAsync添加的额外检查:

public async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken = default(CancellationToken))
{
    // Create a dialog context
    var dc = await Dialogs.CreateContextAsync(turnContext);

    // Handle Message activity type, which is the main activity type for shown within a conversational interface

    // Message activities may contain text, speech, interactive cards, and binary or unknown attachments.

    // see https://aka.ms/about-bot-activity-message to learn more about the message and other activity types

    if (turnContext.Activity.Type == ActivityTypes.Message)
    {
        // If there's no active dialog, begin the parent dialog
        if(dc.ActivDialog == null)
        {
           await dc.BeginDialogAsync(nameof(ParentDialog));
        }
        else
        {
           await dc.ContinueDialogAsync();
        }


        // Save the new turn count into the conversation state.
        await _accessors.ConversationState.SaveChangesAsync(turnContext);
    }
    else
    {

        await turnContext.SendActivityAsync($"{turnContext.Activity.Type} event detected");
    }
}
于 2019-02-22T20:27:35.750 回答