Slack2Watson (1): Connect API Connect to Serverless OpenWhisk

‘Slack2Watson (1): Connect API Connect to Serverless OpenWhisk’ is part 1 of an application called Slack2Watson, which integrates a ChatBot into Slack and allows you to retrieve information about events via Slash Commands in Slack. In part 1, I will set up the ‘API Connect’ service and link the APIs in ‘API Connect’ to the OpenWhisk functions.

Integration is managed using API management in ‘API Connect,’ serverless or event-based APIs in OpenWhisk, and IBM Watson to create the Conversation bot. Some data is retrieved via Google Sheets API.

  1. Create API Connect API v1.0.0
  2. Publish the API
  3. Create an OpenWhisk action
  4. Connect API Connect to OpenWhisk

Requirements

This tutorial uses the OpenWhisk service and the API Connect service on http://bluemix.net, the IBM Cloud. Create an account on IBM Bluemix to run this tutorial.

1. Create API Connect API v1.0.0

  • Go to the Bluemix Catalog,
  • In the left menu select the ‘APIs’ category, and click the ‘API Connect’ service,
  • Click the ‘CREATE’ button to create the ‘API Connect’ service,
  • Click the ‘Sandbox’ icon to go to the Sandbox view with a list of products, which currently says ‘There are no products available in the selected catalog.’,
  • Go to the ‘Navigate to’ icon next to the ‘Home’ icon, and click ‘Drafts’, which will take you by default to the ‘Drafts’ view in the ‘Designer’ perspective,
  • In the ‘Drafts’ view, you see a ‘Products’ tab and an ‘APIs’ tab, click the ‘APIs’ tab,
  • Click the ‘ADD’ button and add a ‘New API’ titled ‘<username>-slack2watson’
    • Title: ‘<username>-slack2watson’
    • Name: ‘<username>-slack2watson’
    • Base Path: /slack2watson
    • Version: 1.0.0

  • Click the ‘Create API’ button,
  • In the ‘Design’ tab, click the ‘Paths’,
  • Add a new Path ‘/hello’, and Save the Design,
  • Go to the ‘Assemble’ tab,
  • By default, you will see there is an ‘Invoke’ node,
  • Delete the ‘Invoke’ node, and add a ‘Switch’ node, by dragging the ‘Switch’ node from the left menu of nodes, to the empty line between the 2 nodes,
  • Make sure the Switch node is selected, click to select the ‘Case 0’ line below the Switch node. It will popup all unassigned Paths. Select the ‘GET /hello’ Path we created earlier,
  • Next, from the left menu, drag a ‘Set Variable’ node to the ‘GET /hello’ case,
  • Edit the settings,
    • Title: ‘set-message-body’,
    • Action: Set,
    • Set: message.body,
    • Type: string,
    • Value: Remko

2. Publish the API

  • To make an API available to the public, we must generate a Product from the Draft. Click the top-right dropdown list and select ‘Generate a default product,’
  • Click the ‘Create product’ button,
  • This will generate a public API,
  • Click the ‘Explore’ button, which takes you to the Explorer view of the Sandbox,
  • Click the ‘GET /hello’ path in the left menu, and scroll down to the ‘Call Operation’ button,
  • Click the ‘Call Operation’ button to test the ‘GET /hello’ request,
  • Close the Explorer perspective, by clicking the ‘close’ cross in the top-right, click in the top-left the ‘All APIs’ breadcrumb, and click the ‘Products’ tab. You now see a product listed in the Products page,
  • From this Dashboard, you can ‘depricate’, ‘retire’ or ‘delete’ API products, or change the ‘Visibility’ of an API,
  • Click the Explore > Sandbox,
  • In the top of the Explorer view, you see the base path of the API product,
    https://us.apiconnect.ibmcloud.com/orgs/<namespace>/catalogs/sb
  • To make a REST call to the GET /slackapp/hello resource, call, use the full URL,

3. Create an OpenWhisk action

To install the OpenWhisk command-line tool, see https://console.ng.bluemix.net/openwhisk/learn/cli.

  • Go back to Bluemix, expand the top-left menu and go to OpenWhisk,
  • In the OpenWhisk site, click the ‘Develop in your Browser’ button or the ‘Develop’ tab,
  • Click the ‘Create an Action’ button,
  • Name the new action ‘getNames’, and click the ‘Create Action’ button,
  • Edit the action as follows,
    function main(params) {
    return { names: ['Remko', 'Joe', 'Luke'] };
    }
  • Click the ‘Make It Live’ button,
  • Click the ‘View REST Endpoint’ link at the bottom-right,
  • Scroll down and click the ‘View Full Command’ button, and copy the full curl command,
  • The HTTP header for Authorization contains a base64 encrypted string of ‘username:password’,
  • You can find the authorization credentials at this page https://console.ng.bluemix.net/openwhisk/learn/cli, which also includes installation instructions for the OpenWhisk command-line (wsk cli) utility,
  • The authorization credentials are in the format ‘username:password’,
  • The username and password or authorization credentials can also be retrieved via the ‘wsk cli,’
  • Send a POST request via the command-line curl tool,
    curl -X POST -H "Authorization: Basic AGH123KHDIUaZko=" "https://openwhisk.ng.bluemix.net/api/v1/namespaces/remkohdev%40us.ibm.com_test/actions/getNames?blocking=true"
  • or replace the namespace string by an underscore,
    curl -X POST -H "Authorization: Basic AGH123KHDIUaZko=" "https://openwhisk.ng.bluemix.net/api/v1/namespaces/_/actions/getNames?blocking=true"
  • Parse the ‘result.response’ for the response of the OpenWhisk action,
    "response": {
    "result": {
    "names": ["Remko", "Joe", "Luke"]
    },

4. Connect API Connect to OpenWhisk

  • Open the ‘API Connect’ service, go to the ‘Drafts’ view,
  • Open the ‘APIs’ tab, and click the latest version of the API,
  • This opens the ‘Designer’ perspective,
  • Click the ‘Design’ tab, and browse to ‘Properties,’
  • We will create two properties that we will use later in the configuration of the ‘Invoke’ node,
  • First create a new Property ‘wsk.username’ by clicking the + icon, and configure as follows
    • Property Name: wsk.username,
    • Under ‘Add Value’, for the ‘Default’ Catalog, for ‘Value’ enter the OpenWhisk username,
  • Second, create a new Property ‘wsk.password’ by clicking the + icon, and configure as follows
    • Property Name: wsk.password,
    • Under ‘Add Value’, for the ‘Default’ Catalog, for ‘Value’ enter the OpenWhisk password,
  • Click the ‘Assemble’ tab,
  • Delete the ‘Set Variable’ node on the ‘GET /hello’ case,
  • Drag a new ‘Invoke’ node onto the ‘GET /hello’ case,
  • Configure the ‘Invoke’ node,
    • Title: invoke-wsk-getNames,
    • Description: invoke OpenWhisk getNames action,
    • URL: https://openwhisk.ng.bluemix.net/api/v1/namespaces/_/actions/getNames?blocking=true,
    • Username: $(wsk.username),
    • Password: $(wsk.password),
    • HTTP Method: POST,
    • Response object variable: wsk.response,
  • Drag a new ScriptGateway node onto the ‘GET /hello’ case after the ‘Invoke’ node,
  • Configure the gatewayscript to run the following script,
    var wskresponse = apim.getvariable('wsk.response');
    apim.setvariable('message.body', {'wskresponse': wskresponse} );
  • The script first reads the response from the OpenWhisk Invoke, then sets the response body,
  • Save the changes to the API Draft, and ‘Save as a new version’, enter ‘1.1.0’, and ‘Generate a default product,’
  • You now have 2 published products,
  • Retire version 1.0.0.

Next

  1. Slack2Watson (2): Integrate Slack Slash Commands into API Connect,

Upcoming:

  1. Make the API Connect API private via an API Key, adding proper API Management,
  2. Parse the Google Sheets API and complete the OpenWhisk API,
  3. Add Watson Conversation,
  4. Automate build and deployment.

Leave a Reply

Your email address will not be published. Required fields are marked *