|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.Linq; |
| 6 | +using System.Threading; |
| 7 | +using System.Threading.Tasks; |
| 8 | +using Microsoft.Azure.CognitiveServices.Language.LUIS.Runtime.Models; |
| 9 | +using Microsoft.Bot.Builder; |
| 10 | +using Microsoft.Bot.Builder.Dialogs; |
| 11 | +using Microsoft.Bot.Schema; |
| 12 | +using Microsoft.Extensions.Logging; |
| 13 | + |
| 14 | +namespace Microsoft.BotBuilderSamples |
| 15 | +{ |
| 16 | + public class DispatchBot : ActivityHandler |
| 17 | + { |
| 18 | + private ILogger<DispatchBot> _logger; |
| 19 | + private IBotServices _botServices; |
| 20 | + |
| 21 | + public DispatchBot(IBotServices botServices, ILogger<DispatchBot> logger) |
| 22 | + { |
| 23 | + _logger = logger; |
| 24 | + _botServices = botServices; |
| 25 | + } |
| 26 | + |
| 27 | + protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken) |
| 28 | + { |
| 29 | + var dc = new DialogContext(new DialogSet(), turnContext, new DialogState()); |
| 30 | + // Top intent tell us which cognitive service to use. |
| 31 | + var allScores = await _botServices.Dispatch.RecognizeAsync(dc, (Activity)turnContext.Activity, cancellationToken); |
| 32 | + var topIntent = allScores.Intents.First().Key; |
| 33 | + |
| 34 | + // Detected entities which could be used to help with intent dispatching, for example, when top intent score is too low or |
| 35 | + // when there are multiple top intents with close scores. |
| 36 | + var entities = allScores.Entities.Children().Where(t => t.Path != "$instance"); |
| 37 | + |
| 38 | + // Next, we call the dispatcher with the top intent. |
| 39 | + await DispatchToTopIntentAsync(turnContext, topIntent, cancellationToken); |
| 40 | + } |
| 41 | + |
| 42 | + protected override async Task OnMembersAddedAsync(IList<ChannelAccount> membersAdded, ITurnContext<IConversationUpdateActivity> turnContext, CancellationToken cancellationToken) |
| 43 | + { |
| 44 | + const string WelcomeText = "Type a greeting, or a question about the weather to get started."; |
| 45 | + |
| 46 | + foreach (var member in membersAdded) |
| 47 | + { |
| 48 | + if (member.Id != turnContext.Activity.Recipient.Id) |
| 49 | + { |
| 50 | + await turnContext.SendActivityAsync(MessageFactory.Text($"Welcome to Dispatch bot {member.Name}. {WelcomeText}"), cancellationToken); |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + private async Task DispatchToTopIntentAsync(ITurnContext<IMessageActivity> turnContext, string intent, CancellationToken cancellationToken) |
| 56 | + { |
| 57 | + switch (intent) |
| 58 | + { |
| 59 | + case "HomeAutomation": |
| 60 | + await ProcessHomeAutomationAsync(turnContext, cancellationToken); |
| 61 | + break; |
| 62 | + case "Weather": |
| 63 | + await ProcessWeatherAsync(turnContext, cancellationToken); |
| 64 | + break; |
| 65 | + case "QnAMaker": |
| 66 | + await ProcessSampleQnAAsync(turnContext, cancellationToken); |
| 67 | + break; |
| 68 | + default: |
| 69 | + _logger.LogInformation($"Dispatch unrecognized intent: {intent}."); |
| 70 | + await turnContext.SendActivityAsync(MessageFactory.Text($"Dispatch unrecognized intent: {intent}."), cancellationToken); |
| 71 | + break; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + private async Task ProcessHomeAutomationAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken) |
| 76 | + { |
| 77 | + _logger.LogInformation("ProcessHomeAutomationAsync"); |
| 78 | + |
| 79 | + // Retrieve LUIS result for HomeAutomation. |
| 80 | + var recognizerResult = await _botServices.LuisHomeAutomationRecognizer.RecognizeAsync(turnContext, cancellationToken); |
| 81 | + var result = recognizerResult.Properties["luisResult"] as LuisResult; |
| 82 | + |
| 83 | + var topIntent = result.TopScoringIntent.Intent; |
| 84 | + |
| 85 | + await turnContext.SendActivityAsync(MessageFactory.Text($"HomeAutomation top intent {topIntent}."), cancellationToken); |
| 86 | + await turnContext.SendActivityAsync(MessageFactory.Text($"HomeAutomation intents detected:\n\n{string.Join("\n\n", result.Intents.Select(i => i.Intent))}"), cancellationToken); |
| 87 | + if (result.Entities.Count > 0) |
| 88 | + { |
| 89 | + 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); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + private async Task ProcessWeatherAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken) |
| 94 | + { |
| 95 | + _logger.LogInformation("ProcessWeatherAsync"); |
| 96 | + |
| 97 | + // Retrieve LUIS result for Weather. |
| 98 | + var recognizerResult = await _botServices.LuisWeatherRecognizer.RecognizeAsync(turnContext, cancellationToken); |
| 99 | + var result = recognizerResult.Properties["luisResult"] as LuisResult; |
| 100 | + var topIntent = result.TopScoringIntent.Intent; |
| 101 | + |
| 102 | + await turnContext.SendActivityAsync(MessageFactory.Text($"ProcessWeather top intent {topIntent}."), cancellationToken); |
| 103 | + await turnContext.SendActivityAsync(MessageFactory.Text($"ProcessWeather Intents detected::\n\n{string.Join("\n\n", result.Intents.Select(i => i.Intent))}"), cancellationToken); |
| 104 | + if (result.Entities.Count > 0) |
| 105 | + { |
| 106 | + 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); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + private async Task ProcessSampleQnAAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken) |
| 111 | + { |
| 112 | + _logger.LogInformation("ProcessSampleQnAAsync"); |
| 113 | + |
| 114 | + var results = await _botServices.SampleQnA.GetAnswersAsync(turnContext); |
| 115 | + if (results.Any()) |
| 116 | + { |
| 117 | + await turnContext.SendActivityAsync(MessageFactory.Text(results.First().Answer), cancellationToken); |
| 118 | + } |
| 119 | + else |
| 120 | + { |
| 121 | + await turnContext.SendActivityAsync(MessageFactory.Text("Sorry, could not find an answer in the Q and A system."), cancellationToken); |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | +} |
0 commit comments