【问题标题】:Support for interruptions in bot支持机器人中断
【发布时间】:2019-12-03 21:07:33
【问题描述】:

您知道是否有办法实现全局消息处理程序,以支持停止、再见、取消、退出Virtual assistant bot 等命令?我正在尝试实现这样的东西。

我已经建立了一个虚拟助手,它有几个Skills or Skill Bots. 当用户与技能进行多轮对话时,用户应该能够通过停止、再见、取消、退出等命令退出技能。

我找到了旧的 v3 doc,但没有找到 v4。

【问题讨论】:

    标签: azure botframework


    【解决方案1】:

    查看此处提供的文档Handling User Interruption 他们解释了如何处理 SDK v4 的用户中断

    在下面查找如何在虚拟助手中进行配置的示例。

    在您的 MainDialog.cs

    为您的 OnContinueDialogAsync 添加以下内容:请记住,您可以根据需要更改和编辑它,只需 请务必检查 OnInterruptDialogAsync 结果 (此示例中的状态)在您继续之前

      protected override async Task<DialogTurnResult> OnContinueDialogAsync(DialogContext innerDc, CancellationToken cancellationToken = default(CancellationToken))
        {
            var status = await OnInterruptDialogAsync(innerDc, cancellationToken).ConfigureAwait(false);
    
            if (status == InterruptionAction.Resume)
            {
                // Resume the waiting dialog after interruption
                await innerDc.RepromptDialogAsync().ConfigureAwait(false);
                return EndOfTurn;
            }
            else if (status == InterruptionAction.Waiting)
            {
                // Stack is already waiting for a response, shelve inner stack
                return EndOfTurn;
            }
            else
            {
                var activity = innerDc.Context.Activity;
    
                if (activity.IsStartActivity())
                {
                    await OnStartAsync(innerDc).ConfigureAwait(false);
                }
    
                switch (activity.Type)
                {
                    case ActivityTypes.Message:
                        {
                            // Note: This check is a workaround for adaptive card buttons that should map to an event (i.e. startOnboarding button in intro card)
                            if (activity.Value != null)
                            {
                                await OnEventAsync(innerDc).ConfigureAwait(false);
                            }
                            else
                            {
                                var result = await innerDc.ContinueDialogAsync().ConfigureAwait(false);
    
                                switch (result.Status)
                                {
                                    case DialogTurnStatus.Empty:
                                        {
                                            await RouteAsync(innerDc).ConfigureAwait(false);
                                            break;
                                        }
    
                                    case DialogTurnStatus.Complete:
                                        {
                                            // End active dialog
                                            await innerDc.EndDialogAsync().ConfigureAwait(false);
                                            break;
                                        }
    
                                    default:
                                        {
                                            break;
                                        }
                                }
                            }
    
                            // If the active dialog was ended on this turn (either on single-turn dialog, or on continueDialogAsync) run CompleteAsync method.
                            if (innerDc.ActiveDialog == null)
                            {
                                await CompleteAsync(innerDc).ConfigureAwait(false);
                            }
    
                            break;
                        }
    
                    case ActivityTypes.Event:
                        {
                            //do something for event activity
                            break;
                        }
    
                    case ActivityTypes.Invoke:
                        {
                            // Used by Teams for Authentication scenarios.
    
                            break;
                        }
    
                    default:
                        {
                            await OnSystemMessageAsync(innerDc).ConfigureAwait(false);
                            break;
                        }
                }
    
                return EndOfTurn;
            }
        }
    

    并覆盖 OnInterruptDialogAsync,如下例所示: 此示例包括 LUIS,但您可以在 OnInterruptDialogAsync

    中做任何您想做的事情
       protected override async Task<InterruptionAction> OnInterruptDialogAsync(DialogContext dc, CancellationToken cancellationToken = default(CancellationToken))
        {
            var result = InterruptionAction.NoAction;
    
            if (dc.Context.Activity.Type == ActivityTypes.Message && !string.IsNullOrEmpty(dc.Context.Activity.Text))
            {
                // get current activity locale
                var localeConfig = _services.GetCognitiveModels();
    
                // check general luis intent
                localeConfig.LuisServices.TryGetValue("General", out var luisService);
    
                if (luisService == null)
                {
                    throw new Exception("The specified LUIS Model could not be found in your Skill configuration.");
                }
                else
                {
                    var luisResult = await luisService.RecognizeAsync<GeneralLuis>(dc.Context, cancellationToken);
                    var topIntent = luisResult.TopIntent();
    
                    if (topIntent.score > 0.5)
                    {
                        switch (topIntent.intent)
                        {
                            case GeneralLuis.Intent.Cancel:
                                {
                                    result = await OnCancel(dc);
                                    break;
                                }
    
                            case GeneralLuis.Intent.Help:
                                {
                                    result = await OnHelp(dc);
                                    break;
                                }
    
                            case GeneralLuis.Intent.Logout:
                                {
                                    result = await OnLogout(dc);
                                    break;
                                }
                        }
                    }
                }
            }
    
            return result;
        }
    

    【讨论】:

    • 我已经对此进行了审查,但它适用于单个 v4 机器人。我解释的场景是针对虚拟助手机器人的,所以我正在寻找该文档或示例。
    • 检查我更新的答案。我使用虚拟助手模板并按照该示例进行用户中断处理
    猜你喜欢
    • 2013-02-07
    • 2016-09-06
    • 2012-08-04
    • 2019-02-20
    • 2021-09-13
    • 2018-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多