DEV Community

Upkar Lidder for IBM Developer

Posted on • Originally published at Medium on

Quick Python backend for your Slack bot on Apache OpenWhisk

OpenWhisk enables event driven architecture from the ground up !

A colleague recently asked on our internal slack workspace how to build Python backend for a slack bot. This is a great use case for serverless , where your code is doing a very focused task and the usage may be infrequent and unpredictable. We will use IBM Cloud Functions, which is a hosted version of Apache OpenWhisk, an open source distributed event driven serverless platform.

Prerequisites

1.Slack workspace where you bot will be deployed. Mine is called upkar-watson-team.
2.IBM Cloud account to build the action and expose behind an API Gateway. You can sign up here.
3.Optional — WSK CLI to upload the function to OpenWhisk. We will also show how to do this on IBM Cloud UI.

Steps

There steps are pretty straight forward and involve creating an application and a bot on Slack and deploying a function on OpenWhisk that you can use as a callback.

The idea is that Slack will sent a POST request to our Python action with event information for all events that our bot subscribes to.

Write an action on Apache OpenWhisk (IBM Cloud Functions)

1.Sign into IBM Cloud. You will be taken to your dashboard after you log in.

2.Click on Functions on the left menu to launch the OpenWhisk home page

3.Create a new action by clicking on Start Creating.

4.Give the action a name and pick Python 3 as your Runtime.

5.Add the following python code to the action

#
#
# main() will be run when you invoke this action
#
# [@param](http://twitter.com/param) Cloud Functions actions accept a single                     parameter, which must be a JSON object.
#
# [@return](http://twitter.com/return) The output of this action, which must be     a JSON object.
#
#
import sys

def main(event):
  # check if the event contains a challenge
  if "challenge" in event:
      print('Event with verification challenge detected.')
      challenge = event['challenge']
      return {'challenge': challenge}

  return { 'message': 'No challenge found. Nothing to do' }

You can invoke this function from the UI itself. Since there is no event in the event dictionary, the action returns

‘message’: ‘No challenge found. Nothing to do’ }

Action invoked with no event passed

6.Add the following parameter by clicking on Change Input as follows:

Set event as input for the action

When you invoke the function now, you see the challenge is returned back as the output. This is what Slack is expecting. You can read more details here.

7.Click on Endpoints and enable the action as a web action

8.Go back to the Actions home page to add an API infront of your action.

9.Create an API end point for your action

10.Give a name , a base api path and create a new operation.

11.Create an operation that listens for POST request and calls our python action

12.Save the API

13.Grab the POST URL from API Explorer

Keep this URL handy. We will provide it to slack in the next section as a verification URL.

Create a bot on slack

  1. Go to api.slack.com and create a new application

Create a new application

  1. Add a bot user

Add a new bot user

Add a new bot user

  1. Subscribe to workspace and bot events

  1. Enable events and add the API gateway end point POST URL.

The bot subscribes to different message events

Add the IBM API URL you copied in the section above as the Request URL in this screen. Tab out of the field and ensure that the URL can be verified by Slack.

Add request URL and verify in Slack

  1. We are done with configuring our application and the bot on Slack. Next step is to install our application to our workspace.

  1. Go to the slack workspace and you should see the new bot available.

Yay 🎆🎈!! We built a Python backend in OpenWhisk very quickly that will allow us to interact with our bot.

Testing our bot in Slack

I changed the OpenWhisk action code to print out the message coming in from Slack.

Now, we can use wsk activation poll command to poll the OpenWhisk logs and as you can see, there same message is posted in both the Slack client and the logs. This requires you to install IBM Cloud CLI and WSK CLI. Alternatively, you can use IBM built in Logs to see the value printed out by the action.

Now that we have successfully deployed a Python backend for our bot, we can process the events coming in from Slack. A common use case is to use opensource python libraries to analyze what is being said by the user. I am working on a post to use NLTK, a popular NLP library in written in Python, to detect sentiments in what the user types in.

Stay tuned !

Max Katz, Carlos Santana, Tomomi Imura,

Top comments (0)