首页 > 解决方案 > C# ChatBot 在对话框之间平滑切换

问题描述

我正在使用不同的对话框开发一个 ChatBot。对话从 MainDialog 开始,然后根据用户的需要进行分支。因此,假设 Dialog 分支是TechnicalSupportand ProductInformation

如果用户当前正在询问TechnicalSupport相关问题,但在对话过程中想了解有关某些产品的信息,是否有办法让ProductInformation对话捕获消息,发送快速响应,然后恢复TechnicalSupport对话流的状态?

标签: c#.net-coredialogbotframeworkchatbot

解决方案


是的,有办法。您可以按照示例为您的产品添加中断。您应该遵循“帮助”中断,因为它在发送消息后恢复对话,这与完全结束当前对话的“取消”不同。它可以由文本或 LUIS 意图触发。

 private async Task<DialogTurnResult> InterruptAsync(DialogContext innerDc, CancellationToken cancellationToken)
    {
        if (innerDc.Context.Activity.Type == ActivityTypes.Message)
        {
            var text = innerDc.Context.Activity.Text.ToLowerInvariant();

            switch (text)
            {
                case "help":
                case "?":
                    var helpMessage = MessageFactory.Text(HelpMsgText, HelpMsgText, InputHints.ExpectingInput);
                    await innerDc.Context.SendActivityAsync(helpMessage, cancellationToken);
                    return new DialogTurnResult(DialogTurnStatus.Waiting);

                case "cancel":
                case "quit":
                    var cancelMessage = MessageFactory.Text(CancelMsgText, CancelMsgText, InputHints.IgnoringInput);
                    await innerDc.Context.SendActivityAsync(cancelMessage, cancellationToken);
                    return await innerDc.CancelAllDialogsAsync(cancellationToken);
            }
        }

        return null;
    }

推荐阅读