コード例 #1
0
        private async Task<DialogTurnResult> ShowResult(WaterfallStepContext stepContext, CancellationToken cancellationToken)
        {
            var state = await _stateAccessor.GetAsync(stepContext.Context);
            var intent = state.LuisResult.TopIntent().intent;

            GetEntityFromLuis(stepContext);
            var userInput = string.Empty;
            if (string.IsNullOrWhiteSpace(state.SearchEntityName))
            {
                stepContext.Context.Activity.Properties.TryGetValue("OriginText", out var content);
                userInput = content != null ? content.ToString() : stepContext.Context.Activity.Text;

                state.SearchEntityName = userInput;
                state.SearchEntityType = SearchResultModel.EntityType.Unknown;
            }

            var bingSearchKey = Settings.Properties[BingSearchApiKeyIndex] ?? throw new Exception("The BingSearchKey must be provided to use this dialog. Please provide this key in your Skill Configuration.");
            var bingAnswerSearchKey = Settings.Properties[BingAnswerSearchApiKeyIndex] ?? throw new Exception("The BingSearchKey must be provided to use this dialog. Please provide this key in your Skill Configuration.");
            var client = new BingSearchClient(bingSearchKey, bingAnswerSearchKey);
            var entitiesResult = await client.GetSearchResult(state.SearchEntityName, state.SearchEntityType);

            Activity prompt = null;
            if (entitiesResult != null && entitiesResult.Count > 0)
            {
                var tokens = new StringDictionary
                {
                    { "Name", entitiesResult[0].Name },
                };

                if (entitiesResult[0].Type == SearchResultModel.EntityType.Movie)
                {
                    var movieInfo = MovieHelper.GetMovieInfoFromUrl(entitiesResult[0].Url);
                    tokens["Name"] = movieInfo.Name;
                    var movieData = new MovieCardData()
                    {
                        Name = movieInfo.Name,
                        Description = movieInfo.Description,
                        Image = movieInfo.Image,
                        Rating = $"{movieInfo.Rating}",
                        GenreArray = string.Join(" ▪ ", movieInfo.Genre),
                        ContentRating = movieInfo.ContentRating,
                        Duration = movieInfo.Duration,
                        Year = movieInfo.Year,
                    };

                    tokens.Add("Speak", movieInfo.Description);

                    prompt = ResponseManager.GetCardResponse(
                                SearchResponses.EntityKnowledge,
                                new Card(GetDivergedCardName(stepContext.Context, "MovieCard"), movieData),
                                tokens);
                }
                else if (entitiesResult[0].Type == SearchResultModel.EntityType.Person)
                {
                    var celebrityData = new PersonCardData()
                    {
                        Name = entitiesResult[0].Name,
                        Description = entitiesResult[0].Description,
                        IconPath = entitiesResult[0].ImageUrl,
                        Link_View = entitiesResult[0].Url,
                        EntityTypeDisplayHint = entitiesResult[0].EntityTypeDisplayHint
                    };

                    tokens.Add("Speak", entitiesResult[0].Description);

                    prompt = ResponseManager.GetCardResponse(
                                SearchResponses.EntityKnowledge,
                                new Card(GetDivergedCardName(stepContext.Context, "PersonCard"), celebrityData),
                                tokens);
                }
                else
                {
                    if (userInput.Contains("president"))
                    {
                        prompt = ResponseManager.GetResponse(SearchResponses.AnswerSearchResultPrompt, new StringDictionary()
                        {
                            { "Answer", "Sorry I do not know this answer yet."},
                            { "Url", "www.bing.com" }
                        });
                    }
                    else
                    {
                        prompt = ResponseManager.GetResponse(SearchResponses.AnswerSearchResultPrompt, new StringDictionary()
                        {
                            { "Answer", entitiesResult[0].Description},
                            { "Url", entitiesResult[0].Url}
                        });
                    }
                }
            }
            else
            {
                prompt = ResponseManager.GetResponse(SearchResponses.NoResultPrompt);
            }

            await stepContext.Context.SendActivityAsync(prompt);
            return await stepContext.NextAsync();
        }
コード例 #2
0
        private async Task <DialogTurnResult> ShowResultAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
        {
            var state = await StateAccessor.GetAsync(stepContext.Context, cancellationToken : cancellationToken);

            GetEntityFromLuis(stepContext);
            var userInput = string.Empty;

            if (string.IsNullOrWhiteSpace(state.SearchEntityName))
            {
                userInput = stepContext.Context.Activity.Text ?? string.Empty;

                state.SearchEntityName = userInput;
                state.SearchEntityType = SearchResultModel.EntityType.Unknown;
            }

            var bingSearchKey       = Settings.BingSearchKey ?? throw new Exception("The BingSearchKey must be provided to use this dialog. Please provide this key in your Skill Configuration.");
            var bingAnswerSearchKey = Settings.BingAnswerSearchKey ?? throw new Exception("The BingSearchKey must be provided to use this dialog. Please provide this key in your Skill Configuration.");
            var client = new BingSearchClient(bingSearchKey, bingAnswerSearchKey);

            // https://github.com/MicrosoftDocs/azure-docs/blob/master/articles/cognitive-services/Labs/Answer-Search/overview.md
            var entitiesResult = await client.GetSearchResult(state.SearchEntityName, "en-us", state.SearchEntityType);

            var actionResult = new ActionResult(false);

            if (entitiesResult != null && entitiesResult.Count > 0)
            {
                actionResult.Url           = entitiesResult[0].Url;
                actionResult.Description   = entitiesResult[0].Description;
                actionResult.ActionSuccess = true;
            }

            Activity prompt = null;

            if (entitiesResult != null && entitiesResult.Count > 0)
            {
                var tokens = new Dictionary <string, object>()
                {
                    { "Name", entitiesResult[0].Name },
                };

                if (entitiesResult[0].Type == SearchResultModel.EntityType.Movie)
                {
                    var movieInfo = MovieHelper.GetMovieInfoFromUrl(entitiesResult[0].Url);
                    if (movieInfo != null)
                    {
                        actionResult.Description = movieInfo.Description;
                        tokens["Name"]           = movieInfo.Name;
                        var movieData = new MovieCardData()
                        {
                            Name          = movieInfo.Name,
                            Description   = StringHelper.EscapeCardString(movieInfo.Description),
                            Image         = movieInfo.Image,
                            Rating        = $"{movieInfo.Rating}",
                            GenreArray    = string.Join(" ▪ ", movieInfo.Genre),
                            ContentRating = movieInfo.ContentRating,
                            Duration      = movieInfo.Duration,
                            Year          = movieInfo.Year,
                        };

                        if (Channel.GetChannelId(stepContext.Context) == Channels.DirectlineSpeech || Channel.GetChannelId(stepContext.Context) == Channels.Msteams)
                        {
                            movieData.Image = ImageToDataUri(movieInfo.Image);
                        }

                        tokens.Add("Speak", StringHelper.EscapeCardString(movieInfo.Description));

                        prompt = TemplateManager.GenerateActivity(
                            SearchResponses.EntityKnowledge,
                            new Card(GetDivergedCardName(stepContext.Context, "MovieCard"), movieData),
                            tokens);
                    }
                    else
                    {
                        prompt = TemplateManager.GenerateActivity(SearchResponses.AnswerSearchResultPrompt, new Dictionary <string, object>()
                        {
                            { "Answer", StringHelper.EscapeCardString(entitiesResult[0].Description) },
                            { "Url", entitiesResult[0].Url }
                        });
                    }
                }
                else if (entitiesResult[0].Type == SearchResultModel.EntityType.Person)
                {
                    var celebrityData = new PersonCardData()
                    {
                        Name                  = entitiesResult[0].Name,
                        Description           = StringHelper.EscapeCardString(entitiesResult[0].Description),
                        IconPath              = entitiesResult[0].ImageUrl,
                        Title_View            = TemplateManager.GetString(CommonStrings.View),
                        Link_View             = entitiesResult[0].Url,
                        EntityTypeDisplayHint = entitiesResult[0].EntityTypeDisplayHint
                    };

                    if (Channel.GetChannelId(stepContext.Context) == Channels.DirectlineSpeech || Channel.GetChannelId(stepContext.Context) == Channels.Msteams)
                    {
                        celebrityData.IconPath = ImageToDataUri(entitiesResult[0].ImageUrl);
                    }

                    tokens.Add("Speak", StringHelper.EscapeCardString(entitiesResult[0].Description));

                    prompt = TemplateManager.GenerateActivity(
                        SearchResponses.EntityKnowledge,
                        new Card(GetDivergedCardName(stepContext.Context, "PersonCard"), celebrityData),
                        tokens);
                }
                else
                {
                    if (userInput.Contains("president"))
                    {
                        prompt = TemplateManager.GenerateActivity(SearchResponses.AnswerSearchResultPrompt, new Dictionary <string, object>()
                        {
                            { "Answer", TemplateManager.GetString(CommonStrings.DontKnowAnswer) },
                            { "Url", BingSiteUrl }
                        });

                        actionResult.Description   = TemplateManager.GetString(CommonStrings.DontKnowAnswer);
                        actionResult.Url           = BingSiteUrl;
                        actionResult.ActionSuccess = false;
                    }
                    else
                    {
                        prompt = TemplateManager.GenerateActivity(SearchResponses.AnswerSearchResultPrompt, new Dictionary <string, object>()
                        {
                            { "Answer", entitiesResult[0].Description },
                            { "Url", entitiesResult[0].Url }
                        });
                    }
                }
            }
            else
            {
                prompt = TemplateManager.GenerateActivity(SearchResponses.NoResultPrompt);
            }

            await stepContext.Context.SendActivityAsync(prompt, cancellationToken);

            if (state.IsAction == true)
            {
                return(await stepContext.NextAsync(actionResult, cancellationToken));
            }

            return(await stepContext.NextAsync(cancellationToken : cancellationToken));
        }