Пример #1
0
        public static List <ThumbnailCard> GetHeroCardsForWebPages(WebPageList webPages)
        {
            var cards =
                (from webPage in webPages.WebPages.Value
                 select new ThumbnailCard
            {
                Title = webPage.Name,
                Subtitle = webPage.About != null ? "About: " + webPage.About.FirstOrDefault()?.Name : string.Empty,
                Text = webPage.Snippet != null ? webPage.Snippet : string.Empty,
                Images = new List <CardImage>
                {
                    new CardImage
                    {
                        Alt = webPage.Snippet,
                        Tap = Common.BuildViewCardAction(webPage.Url, "WebPage"),
                        Url = webPage.ThumbnailUrl
                    }
                },
                Buttons = new List <CardAction>
                {
                    Common.BuildViewCardAction(webPage.Url, "WebPage")
                }
            })
                .ToList();

            return(cards);
        }
Пример #2
0
        public static WebPageList BingWebPageSearch(string searchQuery)
        {
            // Construct the URI of the search request
            var uriQuery = searchUriBase + "?q=" + Uri.EscapeDataString(searchQuery) + queryString;

            // Perform the Web request and get the response
            WebRequest request = HttpWebRequest.Create(uriQuery);

            request.Headers["Ocp-Apim-Subscription-Key"] = Common.accessKey;
            HttpWebResponse response = (HttpWebResponse)request.GetResponseAsync().Result;
            string          json     = new StreamReader(response.GetResponseStream()).ReadToEnd();

            WebPageList webPages = JsonConvert.DeserializeObject <WebPageList>(json);

            return(webPages);
        }
Пример #3
0
        private static IMessageActivity HandleSearch(ITurnContext turnContext, IMessageActivity activity)
        {
            // Look at the user input, and figure out what kind of attachment to send.
            IMessageActivity     reply = null;
            List <ThumbnailCard> cards = null;

            if (activity.Text.StartsWith("1"))
            {
                WebPageList results = WebPagesHelper.BingWebPageSearch(Common.searchText);
                reply = (turnContext.Activity as Activity)
                        .CreateReply($"## Reading news about {Common.searchText}");

                cards = WebPagesHelper.GetHeroCardsForWebPages(results);
            }
            else if (activity.Text.StartsWith("2"))
            {
                Images pictures = ImageHelper.BingImagesSearch(Common.searchText);
                reply = (turnContext.Activity as Activity)
                        .CreateReply($"## Showing images about {Common.searchText}");

                cards = ImageHelper.GetHeroCardsForImages(pictures);
            }
            else if (activity.Text.StartsWith("3"))
            {
                Videos recordings = VideoHelper.BingVideosSearch(Common.searchText);
                reply = (turnContext.Activity as Activity)
                        .CreateReply($"## Showing videos about {Common.searchText}");

                cards = VideoHelper.GetHeroCardsForVideos(recordings);
            }
            else if (activity.Text.StartsWith("4"))
            {
                News articles = NewsHelper.BingNewsSearch(Common.searchText);
                reply = (turnContext.Activity as Activity)
                        .CreateReply($"## Reading news about {Common.searchText}");

                cards = NewsHelper.GetHeroCardsForArticles(articles);
            }
            if (cards != null)
            {
                cards.ForEach(card =>
                              reply.Attachments.Add(card.ToAttachment()));

                reply.AttachmentLayout = AttachmentLayoutTypes.Carousel;
            }
            return(reply);
        }