/// <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());
        }
Exemple #2
0
        private static async Task <string> ProcessQueryIntent(IDialogContext context, Activity activity, BotTask task, string query, string[] topics)
        {
            // Prepare the request to invoke a bot task within the bot brain
            AskQuestionRequest request = new AskQuestionRequest()
            {
                ConversationId                   = activity.Conversation.Id,
                Question                         = query,
                SessionId                        = DefaultSessionId,
                Topics                           = topics != null?topics.ToArray() : new string[0],
                                          UserId = string.IsNullOrEmpty(activity.From.Name) == false ? activity.From.Name : activity.From.Id
            };

            // Invoke the bot task to process the request
            AskQuestionResponse askQuestionResponse =
                await HttpClientUtility.PostAsJsonAsync <AskQuestionResponse>(new Uri(BotBrainUrl + task.ToString()), RequestHeaders, request);

            // Handle the response returned from the bot task to be shown as cards depending on channel
            if (askQuestionResponse.Results?.Count() > 0 == true)
            {
                IMessageActivity foundMsg = context.MakeMessage();

                AskQuestionResult result = askQuestionResponse.Results[0];

                if (string.IsNullOrEmpty(result.Source) == false)
                {
                    foundMsg.Text = string.Format("Got it. Meanwhile, from {0}:", result.Source);
                }
                else
                {
                    foundMsg.Text = "Got it. Meanwhile, here's what I found:";
                }

                await context.PostAsync(foundMsg);

                IMessageActivity cardMessage;

                string channelId = GetChannelId(activity);

                if (channelId == "directline" || channelId == "emulator")
                {
                    cardMessage = GetAdaptiveCardMessage(context, request, result);
                }
                else
                {
                    cardMessage = GetHeroCardMessage(context, request, result);
                }

                await context.PostAsync(cardMessage);
            }
            else
            {
                await PostAsync(context, activity, DoNotUnderstandPreMsg + " " + DoNotUnderstandMsg);
            }

            return("success");
        }
Exemple #3
0
        private static IMessageActivity GetHeroCardMessage(IDialogContext context, AskQuestionRequest request, AskQuestionResult result)
        {
            // Create message
            IMessageActivity message = context.MakeMessage();

            // Create hero card
            HeroCard card = new HeroCard();

            if (string.IsNullOrEmpty(result.Title) == false)
            {
                card.Title = result.Title;
            }

            if (string.IsNullOrEmpty(result.Answer) == false)
            {
                card.Subtitle = result.Answer;
            }

            if (string.IsNullOrEmpty(result.ImageUrl) == false)
            {
                card.Images = new List <CardImage> {
                    new CardImage(url: result.ImageUrl)
                };
            }

            if (string.IsNullOrEmpty(result.Url) == false)
            {
                card.Buttons = new List <CardAction> {
                    new CardAction()
                    {
                        Value = result.Url,
                        Type  = "openUrl",
                        Title = string.IsNullOrEmpty(result.UrlDisplayName) == false ? result.UrlDisplayName : "Open Url"
                    }
                };
            }

            // Attach card to message
            message.Attachments.Add(card.ToAttachment());

            return(message);
        }
Exemple #4
0
        private static IMessageActivity GetAdaptiveCardMessage(IDialogContext context, AskQuestionRequest request, AskQuestionResult result)
        {
            // Create message
            IMessageActivity message = context.MakeMessage();

            // Create adaptive card
            AdaptiveCard card = new AdaptiveCard();

            if (string.IsNullOrEmpty(result.Title) == false)
            {
                card.Body.Add(new AdaptiveTextBlock()
                {
                    Text = result.Title,
                    Size = AdaptiveTextSize.Large,
                    HorizontalAlignment = AdaptiveHorizontalAlignment.Left,
                    IsSubtle            = false,
                    Weight = AdaptiveTextWeight.Bolder,
                    Wrap   = true
                });
            }

            if (string.IsNullOrEmpty(result.Answer) == false)
            {
                card.Body.Add(new AdaptiveTextBlock()
                {
                    Text = result.Answer,
                    Size = AdaptiveTextSize.Medium,
                    HorizontalAlignment = AdaptiveHorizontalAlignment.Left,
                    IsSubtle            = false,
                    Wrap = true
                });
            }

            if (string.IsNullOrEmpty(result.ImageUrl) == false)
            {
                AdaptiveImage currentImage = new AdaptiveImage();
                currentImage.Url  = new Uri(result.ImageUrl);
                currentImage.Size = AdaptiveImageSize.Stretch;
                card.Body.Add(currentImage);
            }

            if (string.IsNullOrEmpty(result.Url) == false)
            {
                card.Actions.Add(new AdaptiveOpenUrlAction()
                {
                    Title = string.IsNullOrEmpty(result.UrlDisplayName) == false ? result.UrlDisplayName : "Open Url...",
                    Url   = new Uri(result.Url)
                });
            }

            // Attach card to message
            Attachment attachment = new Attachment()
            {
                Content     = card,
                ContentType = "application/vnd.microsoft.card.adaptive"
            };

            message.Attachments.Add(attachment);

            return(message);
        }