Integrating Slack Commands with API Connect and OpenWhisk

This is part 1 to build an event management application called EventQuarry, integrated into Slack, using API management, event-based APIs in OpenWhisk, Google Sheets API, IBM Watson to create a Conversation bot.

  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, find the API Connect service,
  • Create the API Connect service and open the API Connect service, this will take you by default to the ‘Drafts’ view in the ‘Designer’ perspective,
  • In the ‘Drafts’ view there are a ‘Designer’ and an ‘Explore’ perspective,
  • In addition to the ‘Drafts’ view, there is a ‘Sandbox’ view in the ‘Catalog’, which is where you see the ‘Products’. Currently, there are no Products yet, we only created a Draft,
  • Open the API Connect Drafts
  • Add a New API titled ‘<username>-slackapp’
    • Title: ‘<username>-slackapp’
    • Name: ‘<username>-slackapp’
    • Base Path: /slackapp
    • 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,
  • 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 and open the ‘Catalogs > Sandbox > Explore’ perspective, from which you can click the ‘Call Operation’ to test the API 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,
  • Next to the Home icon, click the ‘Expand’ icon, and click ‘Dashboard,’
  • Click the ‘Sandbox’ service, you now see all your Products and their status,
  • From this Dashboard, you can ‘depricate’, ‘retire’ or ‘delete’ API products, or change the ‘Visibility’ of an API,
  • Click the Explorer > Sandbox,
  • In the top of the Explorer view, you see the base path of the API product,
    https://us.apiconnect.ibmcloud.com/orgs/<username><path>-<space>/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’,
  • The username and password or authorization credentials can be retrieved via the ‘wsk cli,’ install the OpenWhisk command-line utility or find the authorization credentials at this page https://console.ng.bluemix.net/openwhisk/learn/cli,
  • The credentials are in the format ‘username:password’,
  • 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, open the APIs tab, and click the latest version of the API,
  • This opens the ‘Designer’ perspective in the ‘Drafts’ view,
  • 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,’

Next

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 the Slack command integration,
  4. Add Watson Conversation.

Leave a Comment

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

%d bloggers like this: