/// <summary>
        /// Get Ask Question Results from Video Indexer Search Result
        /// </summary>
        public static AskQuestionResult[] GetAskQuestionResults(VideoIndexerSearchResult searchResult)
        {
            List <AskQuestionResult> results = new List <AskQuestionResult>();

            foreach (VideoIndexerResult videoResult in searchResult.Results.Take(SettingsUtility.MaxResultsCount))
            {
                AskQuestionResult result = new AskQuestionResult()
                {
                    Title          = videoResult.Name,
                    ImageUrl       = videoResult.ThumbnailUrl,
                    Source         = "Video Indexer",
                    Url            = videoResult.VideoUrl,
                    UrlDisplayName = "Open Video"
                };

                if (videoResult.SearchMatches?.Count() > 0 == true)
                {
                    VideoIndexerSearchMatch match =
                        videoResult.SearchMatches.FirstOrDefault(s => s.Type.Contains("Transcript") == true) ?? videoResult.SearchMatches.First();

                    result.Url += "?t=" + TimeSpan.Parse(match.StartTime).TotalSeconds.ToString();

                    result.Answer = match.text;
                }

                results.Add(result);
            }

            return(results.ToArray());
        }
        public async Task <VideoIndexerSearchResult> SearchAsync(string query = "", string face = "")
        {
            if (string.IsNullOrEmpty(query) == true && string.IsNullOrEmpty(face) == true)
            {
                return(default(VideoIndexerSearchResult));
            }

            Dictionary <string, string> queryParams = new Dictionary <string, string>()
            {
                { "query", WebUtility.UrlEncode(query) },
                { "face", WebUtility.UrlEncode(face) },
            };

            IEnumerable <string> queryString = queryParams
                                               .Where(p => string.IsNullOrEmpty(p.Value) == false)
                                               .Select(p => p.Key + "=" + p.Value);

            string searchQuery = queryString.Count() == 1 ? queryString.FirstOrDefault() : string.Join("&", queryString);

            // Get request uri
            string searchUrl  = this.BaseServiceUrl + "?" + searchQuery;
            Uri    requestUri = new Uri(searchUrl);

            // Get response
            VideoIndexerSearchResult result = await HttpClientUtility.GetAsync <VideoIndexerSearchResult>(requestUri, this.RequestHeaders);

            return(result);
        }
            public static async Task <AskQuestionResponse> AnswerQuestion(AskQuestionRequest request, ICollector <SessionTableEntity> sessionTable)
            {
                // Get unique identifier
                string   id           = Guid.NewGuid().ToString();
                DateTime timestampUtc = DateTime.UtcNow;

                // Get key phrases extraction
                request.Topics = await ServicesUtility.GetTopics(request.Question, request.Topics);

                string queryWithTopics = request.Topics?.Count() > 0 ? string.Join(" ", request.Topics).Trim() : request.Question;

                // Create video indexer task
                VideoIndexerSearchResult videoIndexerResult = await ServicesUtility.VideoIndexer.SearchAsync(query : queryWithTopics);


                // Process results
                AskQuestionResponse response = new AskQuestionResponse()
                {
                    Id      = id,
                    Results = new AskQuestionResult[0]
                };

                if (videoIndexerResult.Results?.Count() > 0)
                {
                    response.Results = ServicesUtility.GetAskQuestionResults(videoIndexerResult);
                }
                else
                {
                    // Create Bing Search tasks
                    BingWebSearchResult bingWebSearchResult =
                        await ServicesUtility.BingSearch.SearchWebAsync(query : request.Question, count : SettingsUtility.MaxResultsCount);

                    // Process Bing search results
                    if (bingWebSearchResult.WebPagesResult?.Values?.Count() > 0)
                    {
                        response.Results = ServicesUtility.GetAskQuestionResults(bingWebSearchResult);
                    }
                }

                // Write to session table
                sessionTable.Add(new SessionTableEntity(id, timestampUtc, "LearnMore", request, response));

                // Return response
                return(response);
            }