Skip to main content

Microsoft Bot Framework Basics: Building Intelligent Bots – Adding Bing News API (Part 3)

Scope

The following articles demonstrates the use of the Bing News API from Microsoft Cognitive Services in a bot using the Microsoft Bot Framework. This article will build on the previous articles about Bot Framework where the basics of building a bot and the integration of Language Understanding API was demonstrated.
The objective is to allow a user to send a message to the bot, the bot will use language understanding capability to understand the message, fetch news based on the user input using Bing API and return the results to the user.

Introduction

The News API provides a similar (but not exact) experience to Bing.com/News. The News API lets you send a search query to Bing and get back a list of relevant news articles.
Bing News API also includes authoritative images of the news article, related news and categories, provider information, article URLs, and dates when images were added.

Adding the Bing News API

At this stage, we have setup our project and we have successfully implemented Language Understanding in our bot. It’s now time to build the search functionality using the Bing News API.
What will happen is that once LUIS identify a sentence as a news, it will then return us the News Entity, that is, the item about which the user is trying to search for news. We’ll then take this entity and pass it to the Bing News API to get the results.


To connect to the Bing News API, follow the steps here  to register and get the keys. The API takes a text and input and returns a JSON of all the news item as shown below.
{
  "_type": "News",
  "totalEstimatedMatches": 1980000,
  "value": [
    {
      "name": "Tennessee <b>Football</b>: Former Coach Phillip Fulmer Takes Shot at Lane Kiffin",
      "image": {
        "thumbnail": {
          "width": 700,
          "height": 461
        }
      },…

Once we have subscribed to the Bing News API, follow the steps below to add the code to the bot to retrieve the news.

Create function GetBingNews to fetch the news. 

The function getBingNews takes the news to search as parameter, makes a connection to the Bing News API, pass the parameter to it and get the JSON response of the news back.
Then, it is deserialized and converted to a List of news using the JsonConvert library.

private async Task<BingNews> getBingNews(string query)
        {
            BingNews bingNews;
            String bingUri = "https://api.cognitive.microsoft.com/bing/v5.0/news/search/?count=50&q=  " + query;
            string rawResponse;
 
            HttpClient httpClient = new HttpClient()
            {
                DefaultRequestHeaders = {
                    {"Ocp-Apim-Subscription-Key""SUBSCRIPTION KEY"},
                    {"Accept""application/json"}
            }
            };
 
            try
            {
                rawResponse = await httpClient.GetStringAsync(bingUri);
                bingNews = JsonConvert.DeserializeObject<BingNews>(rawResponse);
            }
            catch (Exception e)
            {
                return null;
            }
            return bingNews;
        }


Retrieve the news item to search


In the SearchNews intent, add the following code to retrieve the entity. It is this retrieved entity that represents the news that we need to search

EntityRecommendation newsEntity;
if (result.TryFindEntity("NewsItem"out newsEntity)) {…}

Call the getBingNews method using the intent

BingNews bingNews = await getBingNews(newsEntity.Entity);

Check if there are no news returned and inform the user accordingly


var message = await activity;
var reply = context.MakeMessage();
 
if (bingNews == null || bingNews.totalEstimatedMatches == 0)
{
     reply.Text = "Sorry, couldn't find any news about '" + newsEntity.Entity;
}


Display the news to the user



reply.AttachmentLayout = AttachmentLayoutTypes.Carousel;
reply.Attachments = new List<Microsoft.Bot.Connector.Attachment>();
for (int i = 0; i < 10 && i < (bingNews?.totalEstimatedMatches ?? 0); i
{
    var article = bingNews.value[i];
    HeroCard attachment = new HeroCard()
    {
        Title = article.name.Length > 60 ? article.name.Substring(0, 57) + "..." : article.name,
        Text = article.provider[0].name + ", " + article.datePublished.ToString("d") + " - " + article.description,
        Images = new List<CardImage>() { new CardImage(article.image?.thumbnail?.contentUrl + "&w=400&h=400") },
        Buttons = new List<CardAction>() { new CardAction(
                                                        ActionTypes.OpenUrl,
                                                        title: "View on Web",
                                                        value: article.url)}
    };
    reply.Attachments.Add(attachment.ToAttachment());
}
await context.PostAsync(reply);
context.Wait(this.MessageReceived);


Test the bot in the emulator




References


  1. Searching the web for news

Comments

Popular posts from this blog

Creating and Querying Microsoft Azure DocumentDB

DocumentDB is the latest storage option added to Microsoft Azure.
It is a no-sql storage service that stores JSON documents natively and provides indexing capabilities along with other interesting features.

This article is available available on theMicrosoft Technet Wiki. This article was highlighted in theTop Contributor awardson the 12th of October 2014. This article was highlighted in the TNWiki Article Spotlight. This article was highlighted in the The Microsoft TechNet Guru Awards! (October 2014).


DocumentDB is the latest storage option added to Microsoft Azure.
It is a no-sql storage service that stores JSON documents natively and provides indexing capabilities along with other interesting features.
This wiki shall introduce you to this new service.

Setting up a Microsoft Azure DocumentDBGo to the new Microsoft Azure Portal. https://portal.azure.com/ 


 Click on New > DocumentDB


Enter A Database ID and hit Create!



Query Unstructured Data From SQL Server Using PolyBase

Scope The following article demonstrates how unstructured data and relational data can be queried, joined and processed in a single query using PolyBase, a new feature in SQL Server 2016. Pre-RequisitesIntroduction to Big Data Analytics Using Microsoft Azure Big Data Analytics Using Hive on Microsoft Azure Analyze Twitter Data With Hive in Azure HDInsight Running Hadoop on Linux using Azure HDInsight  Introduction Traditionally, Big Data is processed using Apache Hadoop which is totally fine. But what if the result of this needs to be linked to the traditional Relation Database? For example, assume that from the analysis of tons of application logs, marketing needs to contact some customs that faced problems in an application following a failure in the application.
This problem is solved with PolyBase. PolyBase allows you to use Transact-SQL (T-SQL) statements to access data stored in Hadoop or Azure Blob Storage and query it in an ad-hoc fashion. It also lets you query semi-structure…

Creating and Deploying Microsoft Azure WebJobs

Azure WebJobs enables you to run programs or scripts in your website as background processes. It runs and scales as part of Azure Web Sites.
This article focuses on the basics of WebJobs before demonstrating an example where it can be used.

This article is also available on the Mirosoft TechNet Wiki.
This article was highlighted in the The Microsoft TechNet Guru Awards! (October 2014).


Introduction
What is Microsoft Azure WebJobs?
Azure WebJobs enables you to run programs or scripts in your website as background processes. It runs and scales as part of Azure Web Sites.

What Scheduling Options is supported by Microsoft Azure WebJobs? Azure WebJobs can run Continuously, On Demand or on a Schedule.
In what language/scripts are WebJobs written?
Azure WebJobs can be created using the following scripts:  .cmd, .bat, .exe (using windows cmd).ps1 (using powershell).sh (using bash).php (using php).py (using python).js (using node)In this article, the use of c# command line app shall be demonstrated.
Cr…