如何从LUIS意图调用对话框类

时间:2019-09-16 16:42:35

标签: botframework luis

我正在使用Bot Framework V4调度模型来调用LUIS和QnA服务。 我可以在得分最高的意图类中调用代码。

但是,我找不到从中调用外部对话框的方法。我该怎么办?

// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

    namespace Microsoft.BotBuilderSamples
    {
        public class DispatchBot : ActivityHandler
        {
            private ILogger<DispatchBot> _logger;
            private IBotServices _botServices;

            public DispatchBot(IBotServices botServices, ILogger<DispatchBot> logger)
            {
                _logger = logger;
                _botServices = botServices;
            }

            protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken)
            {
                // First, we use the dispatch model to determine which cognitive service (LUIS or QnA) to use.
                var recognizerResult = await _botServices.Dispatch.RecognizeAsync(turnContext, cancellationToken);

                // Top intent tell us which cognitive service to use.
                var topIntent = recognizerResult.GetTopScoringIntent();

                // Next, we call the dispatcher with the top intent.
                await DispatchToTopIntentAsync(turnContext, topIntent.intent, recognizerResult, cancellationToken);
            }

            protected override async Task OnMembersAddedAsync(IList<ChannelAccount> membersAdded, ITurnContext<IConversationUpdateActivity> turnContext, CancellationToken cancellationToken)
            {
                const string WelcomeText = "Type a greeting, or a question about the weather to get started.";

                foreach (var member in membersAdded)
                {
                    if (member.Id != turnContext.Activity.Recipient.Id)
                    {
                        await turnContext.SendActivityAsync(MessageFactory.Text($"Welcome to Dispatch bot {member.Name}. {WelcomeText}"), cancellationToken);
                    }
                }
            }

            private async Task DispatchToTopIntentAsync(ITurnContext<IMessageActivity> turnContext, string intent, RecognizerResult recognizerResult, CancellationToken cancellationToken)
            {
                switch (intent)
                {
                    case "l_HomeAutomation":
                        //WANT TO CALL THE EXTERNAL DIALOG CLASS FROM HERE
                        //await ProcessHomeAutomationAsync(turnContext, recognizerResult.Properties["luisResult"] as LuisResult, cancellationToken);
                        break;
                    case "l_Weather":
                        await ProcessWeatherAsync(turnContext, recognizerResult.Properties["luisResult"] as LuisResult, cancellationToken);
                        break;
                    case "q_sample-qna":
                        await ProcessSampleQnAAsync(turnContext, cancellationToken);
                        break;
                    default:
                        _logger.LogInformation($"Dispatch unrecognized intent: {intent}.");
                        await turnContext.SendActivityAsync(MessageFactory.Text($"Dispatch unrecognized intent: {intent}."), cancellationToken);
                        break;
                }
            }

            private async Task ProcessHomeAutomationAsync(ITurnContext<IMessageActivity> turnContext, LuisResult luisResult, CancellationToken cancellationToken)
            {
                _logger.LogInformation("ProcessHomeAutomationAsync");

                // Retrieve LUIS result for Process Automation.
                var result = luisResult.ConnectedServiceResult;
                var topIntent = result.TopScoringIntent.Intent; 

                await turnContext.SendActivityAsync(MessageFactory.Text($"HomeAutomation top intent {topIntent}."), cancellationToken);
                await turnContext.SendActivityAsync(MessageFactory.Text($"HomeAutomation intents detected:\n\n{string.Join("\n\n", result.Intents.Select(i => i.Intent))}"), cancellationToken);
                if (luisResult.Entities.Count > 0)
                {
                    await turnContext.SendActivityAsync(MessageFactory.Text($"HomeAutomation entities were found in the message:\n\n{string.Join("\n\n", result.Entities.Select(i => i.Entity))}"), cancellationToken);
                }
            }

            private async Task ProcessWeatherAsync(ITurnContext<IMessageActivity> turnContext, LuisResult luisResult, CancellationToken cancellationToken)
            {
                _logger.LogInformation("ProcessWeatherAsync");

                // Retrieve LUIS results for Weather.
                var result = luisResult.ConnectedServiceResult;
                var topIntent = result.TopScoringIntent.Intent;
                await turnContext.SendActivityAsync(MessageFactory.Text($"ProcessWeather top intent {topIntent}."), cancellationToken);
                await turnContext.SendActivityAsync(MessageFactory.Text($"ProcessWeather Intents detected::\n\n{string.Join("\n\n", result.Intents.Select(i => i.Intent))}"), cancellationToken);
                if (luisResult.Entities.Count > 0)
                {
                    await turnContext.SendActivityAsync(MessageFactory.Text($"ProcessWeather entities were found in the message:\n\n{string.Join("\n\n", result.Entities.Select(i => i.Entity))}"), cancellationToken);
                }
            }

            private async Task ProcessSampleQnAAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken)
            {
                _logger.LogInformation("ProcessSampleQnAAsync");

                var results = await _botServices.SampleQnA.GetAnswersAsync(turnContext);
                if (results.Any())
                {
                    await turnContext.SendActivityAsync(MessageFactory.Text(results.First().Answer), cancellationToken);
                }
                else
                {
                    await turnContext.SendActivityAsync(MessageFactory.Text("Sorry, could not find an answer in the Q and A system."), cancellationToken);
                }
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

您需要在DispatchBot中初始化所需对话框的实例,然后像这样调用它们: await this.weatherDialog.RunAsync(turnContext, dialogStateAccessor, cancellationToken)

这里是some sample code。这与您的设计稍有不同,因为我将意图检测委托给父对话框,然后从那里进行逻辑分支(以使bot类逻辑保持简单),但是如何调用对话框是相同的。

根据以下讨论进行更新: 如果您使用多匝对话框,建议您遵循代码中的模式。否则,您将不得不自己继续对话,祝您好运。