Overview

Skill Level: Beginner

Beginners

This recipe will explain how to use Watson “Alchemy News” service to fetch news. In this recipe we will create a ReST endpoint which can be invoked by Browser, Mobile UI or any app. Response will be string value which will have News.

Ingredients

IBM Bluemix account

Internet of Things Platform service

Watson Alchemy News service

Step-by-step

  1. Login to Bluemix platform

    Go to https://console.ng.bluemix.net/ Bluemix page and click on “Login”.

    Enter your registered email id and password.

    Blumix-Login-1-2

    Blumix-Login-3-2

    Blumix-Login-4-2

     

    Bluemix Dashboard page will get loaded on successful login:

    Login-Sucessful-LandingPage-2

     

  2. Add IOT service

    We have two options to create IOT service, first option is using Node-Red starter application service and other is adding IOT service directly. In this recipe I am explaining how to add IOT service.

     

    Click on Menu left-top of page [Three lines] -> then on “Services” -> after that “Dashboard”. Dashboard page will get loaded.

     To add new service “Create Service” option is available on dashboard page. Click on blue button “Create Service”. Create service page will get loaded. On top of the page search/filter service option is available to search existing services. Search “IOT” service. Below two results will be shown for “Boilerplates” category:

    [1] Internet of Things Platform Starter

    [2] IoT for Electronics Starter

    Add-IOT-Service-1-2

     

    We will add “Internet of Things Platform Starter” service. Click on “Internet of Things Platform Starter” service. New page will get loaded to create IOT service. Enter the application name and keep all other configurations as it is [Keep default settings]. Click on “Create” button at the bottom-right of page. Bluemix platform verifies for unique application name. This application name will be used to call this service.

    Add-IOT-Service-2-2

     

    System will check application name, name should be unique. System will check application name, name should be unique. System will ask to re-enter different name if name is already used by someone.

    Add-IOT-Service-3-2

     

    System will add/create IOT application and load the application dashboard page with service details. At the top of page you will find application name with “status”. Application status will become “Green” color dot after service gets started successfully. If service is not started or if you find dot in “Grey” or “Red” color you can restart the service again by clicking on the option of restart at right side of IOT service application name.

    Add-IOT-Service-4-2

     

     

  3. Add Watson Alchemy News service

    Once your IOT application is up and running then we will add “Alchemy News” service.

    Go back to Service -> Dashboard page

    Click on “Create Services” blue button option to create/add services.

    Page will get loaded with option to search service at top of the page.

    Search for “Alchemy” and you will get one result “AlchemyAPI” for “Watson” category.

    Click on “AlchemyAPI” service to add this service.

    Add_AlchemyAPI_Service_1

     

    Enter Service name and click on “Create” blue button at bottom of page right side.

    Add_AlchemyAPI_Service_2

     

    Service gets created successfully.  and service detail page gets loaded as below:

    Add_AlchemyAPI_Service_Successful_3

     

    Click on “Service Credentials” submenu, after that “View Credentials” to know service “API Key” and “URL”. We will use these details while configuration of Alchemy News node.

     

    Add_AlchemyAPI_Service_Credentials_4

     

    You can copy these service credentials to notepad to keep it handy to configure node or preparing API URL.

  4. How This Application will get called

    We can call this REST API using any browser or using any runtime like Perl, PHP, Java or Python or using Mobile Application. Please find below details how to call this REST API using IR browser.

    How_To_Call_Application_1

     

    Also you can test/verify application in Node-Red by clicking “Inject” node.

    How_To_Call_Application_2

  5. Create Node-red application flow as REST end point / method / API

    We will draw/create flow for this application using “Node-Red” application.

     

    [a] Complete Node-red flow

    Complete_Node-Red-Flow

     

    [b] Configuration of each Node

     

    [i] Load Node-Red flow editor

    Click on “Menu” -> “Services” -> “Dashboard” to load Services dashboard. On dashboard page go to “All Apps” section, this section lists all cloud foundry application created. Your application name will be hyperlinked [under ROUTE] section. Please verify STATE of your application, state should be “green”. “Green” state means application/service is up and running. If you find STATE with “Grey” or “Red” dot then restart the service by click on three dots under “Action” column.

     

    Click on Application name which is hyperlink to load the Node-red-flow Editor. New tab/window will get opened.

    Services_Dashboard_1

     

    Next page will get loaded with Node-Red details with big rectangle in RED color at right side bottom of the page. Click on “Go to your Node-RED flow editor” button to load flow editor.

    Node_Red_Browser_default_page-3

    Node_Red_Browser_Editor_page

     

    [ii] Add “http” input node

    We will call this service as REST end point. Search “http” node from available nodes using “Filter Nodes” option available at top-left of the flow editor. Drag “http” node from “Input” node category. Double click on the node and complete configuration shown as below:

    GetNews_Recipe_Http_InputNode_1

    Method: Keep GET method as it is, as we will call this service using GET method.

    URL: Please enter REST API name with “/” at start. Example: “/getnews”.

    Name: Give valid name which can recognize the functionality of Node. Enter “Getnews-APICall” as name for this node.

     

    Click on “Done” button. Done button will be in Red color at the top of Node configuration window.

     

    [iii] Add “Inject” node

    To test this application we will add “Inject” node tat start of the flow. We will use this node to initiate or call this flow. Search “Inject” node from left side node list and drag it to workspace area. You can search available nodes using “Filter Nodes” option available at left side top of the flow editor. Drag “Inject” node which is displayed under “Input” node category. We will not change any configuration details, if you want this node can called after fixed interval like after every 10 mins or every 30 mins we can configure using “Repeat” value with “Interval”.

    GetNews_Recipe_Http_InjectNode_1

    GetNews_Recipe_Http_InjectNode_2

    We will not do any configuration changes, keep default settings. Click on “Done” button. Done button will be in Red color at the top of Node configuration window.

     

    [iv] Add Alchemy News Node

    We will add Alchemy News service node. Search “news” node from left side node list and drag it to workspace area. You can search available nodes using “Filter Nodes” option available at left side top of the flow editor. Drag “news” node which is displayed under “IBM_Watson” node category. Double click on the news node and do below configuration:

    GetNews_Recipe_AlchemyAPI_Node_1

    Name: Give valid name to function, for example “GetNews”.

    Service: Keep “External Service” as it is.

    Key: Please add key which you got when “Alchemy Service” added. You can find key from service credential section.

    Start: Select yesterday’s date.

    Name: Select Todays date.

    Max Results:  we will fetch 2 results; you can change this value as per your application need.

    Custom query: Select this check box as we will pass custom query with additional parameters.

    Query:  Add below URL, replace “<APIKEY>” value with actual api key.

    https://access.alchemyapi.com/calls/data/GetNews?apikey=<APIKEY>&return=enriched.url.title&start=now-1d&end=now&q.enriched.url.cleanedTitle=India&count=2&outputMode=json

     

    Click on “Done” button. Done button will be in Red color at the top of Node configuration window.

     

    [v] Function node to read News and prepare News title String 

    We will read output of Alchemy API service, parse the news results and prepare single string of headlines. We will use function node to do this functionality.

     

    Search “function” node from left side node list and drag it to workspace area. You can search available nodes using “Filter Nodes” option at top-left side of the flow editor. Drag “function” node which is displayed under “Function” node category. Double click on the function node and do below configuration:

    GetNews_Recipe_Function_Node_1

    Name: Give valid name to function, for example “GetNewsTitles”.

    Function: We will read and parse output received from Alchemy API service node. In loop we will extract news titles and concatenate in one string.

     

    var Allheadlines=””;

    var statusval = msg.payload.status; // get Status OK

    var StatusResult = statusval.localeCompare(“OK”); // Compare status

    if (StatusResult === 0){ // Check if status is OK

        var docsArray = msg.payload.result.docs;

        for (var i = 0; i < docsArray.length; ++i){ //Loop to get all headline concatenated into single string

            if(i > 0){

                Allheadlines = Allheadlines + “Headline ” + (i+1)+ ” – ” + docsArray[i].source.enriched.url.title + “. “;

            }else{

                Allheadlines = “Headline ” + (i+1)+ ” – ” + docsArray[i].source.enriched.url.title + “. “;

            }

        }

        msg.payload = Allheadlines;

    }else{

        msg.payload = “News results unavailable at this time.”;

    }

    return msg;

     

    Click on Red color “Done” button at the top of Node configuration window.

     

    [vi] HTTP output node to news string [http output]        

    Search “http” node from left side node list and drag it to workspace area. You can search available nodes using “Filter Nodes” option at top-left side of the flow editor. Drag “http” node which is displayed under “Output” node category. Double click on the function node and do below configuration:

    GetNews_Recipe_Http_OuputNode_1

    Name: Give valid name to http output node, for example “http-output”.

     

    Click on “Done” button. Done button will be in Red color at the top of Node configuration window.

     

    [vi] Add Debug node [debug]   

    Search “debug” node from left side node list and drag it to workspace area. You can search available nodes using “Filter Nodes” option at top-left side of the flow editor. Drag “debug” node which is displayed under “Output” node category. We will keep default configuration:

    GetNews_Recipe_Debug_Node_1

    We will keep default configuration, we are using this node to get output into the debug window.

     

    Click on “Done” button. Done button will be in Red color at the top of Node configuration window.

     

    [vii] Add Play Audio node [play audio] 

    [a] Add play audio node to node list

    Click on menu at top-right of flow editor page, three small lines. Select “Manage palette” option. New window will get opened with “Nodes” & “Install” tabs. Click on “Install” tab and search for “play-audio”. You will get “node-red-contrib-play-audio” in results. Just click on “Install” option to get this node added to node list. After successful installation Successful message will get displayed.

    GetNews_Recipe_PlayAudio_Node_1

    Install_Nodes_ClickOnInstall_4_1

    [b] Add play audio node to flow

    Search “play audio” node from left side node list and drag it to workspace area. You can search available nodes using “Filter Nodes” option at top-left side of the flow editor. Drag “play audio” node which is displayed under “Output” node category. We will keep default configuration:

     

    GetNews_Recipe_PlayAudio_Node_1

    Select “US English” option or you can keep default configuration. This node will convert text input into audio and play it. You can use device like Raspberry and push this output to be played on device as service.

     

    Click on “Done” button. Done button will be in Red color at the top of Node configuration window.

     

    [viii] Connect flow

    We have completed node configuration of all nodes. Now connect these nodes sequential. Mouse over to the connector of first node [Grey color connector at right side of http-in node]. Connector will become orange in color. Keep your mouse key pressed and drag it to left side connector of next node. Please complete wiring/connecting all nodes as shown below:

    Complete_Node-Red-Flow

    [c] Deploy the application

    “Deploy” option is available at top-right side of page. “Deploy” button will be in the red color. Click on deploy button to deploy application to server. On successful deployment “Successfully deployed” message will get displayed. Your application is now ready and live.

    GetNews_Recipe_Deploy_1

     

     

  6. Invoke or Call Application/REST API to get News

    After successful deployment application will be ready. We can test this application by calling this application/method from IE browser.

    How_To_Call_Application_1

    http://<Node-RED application name>.mybluemix.net/getnews

    Example:

    https://iotexamples11.mybluemix.net/getnews

     

    The Node-RED application name and the text value must be replaced before making a call.

    The news headlines string will be returned in the response. You can use this like API method call and configure it to your web page or you can play it on local device.

     

    Also you can test this Node-red flow using “Inject” node by click on Inject.

    GetNews_Recipe_InjectNode_Call_1

    You can call this REST endpoint from any other runtime to use this service.

     

     

  7. Summary

    In this recipe we demonstrated the use of AlchemyAPI service to get News and play it as audio. You can call this REST endpoint directly using browser, using any Runtime like Python or Mobile Application. I will add subsequent recipes which will focus on the other Watson services.

Join The Discussion