Quick Python backend for your Slack bot on Apache OpenWhisk

Upkar Lidder
Feb 27, 2019 · 6 min read
Image for post
Image for post
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

Image for post
Image for post

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

Image for post
Image for post

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

Image for post
Image for post

5. Add the following python code to the action

#
#
# main() will be run when you invoke this action
#
# @param Cloud Functions actions accept a single parameter, which must be a JSON object.
#
# @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' }
Image for post
Image for post

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’ }
Image for post
Image for post
Action invoked with no event passed

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

Image for post
Image for post
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.

Image for post
Image for post

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

Image for post
Image for post

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

Image for post
Image for post

9. Create an API end point for your action

Image for post
Image for post

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

Image for post
Image for post

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

Image for post
Image for post

12. Save the API

Image for post
Image for post

13. Grab the POST URL from API Explorer

Image for post
Image for post

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

Image for post
Image for post
Image for post
Image for post
Create a new application

2. Add a bot user

Image for post
Image for post
Add a new bot user
Image for post
Image for post
Add a new bot user

3. Subscribe to workspace and bot events

Image for post
Image for post

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

Image for post
Image for post
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.

Image for post
Image for post
Add request URL and verify in Slack

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

Image for post
Image for post
Image for post
Image for post

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

Image for post
Image for post

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.

Image for post
Image for post

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.

Image for post
Image for post

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 !


Design and Tech.Co

Ideas for the 21st Century Hustler. www.designandtech.co

Upkar Lidder

Written by

I design and build mobile and web solutions that delight and excite users. I have extensive consulting and development experience with small and large public an

Design and Tech.Co

Ideas for the 21st Century Hustler. www.designandtech.co

Upkar Lidder

Written by

I design and build mobile and web solutions that delight and excite users. I have extensive consulting and development experience with small and large public an

Design and Tech.Co

Ideas for the 21st Century Hustler. www.designandtech.co

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store