Quick Python backend for your Slack bot on Apache OpenWhisk
Tags: openwhisk, apacheopenwhisk, python3, slack
Published: 2019-02-28 18:23:07

---
title: Quick Python backend for your Slack bot on Apache OpenWhisk
published: true
tags: openwhisk,apache-openwhisk,python3,slack
canonical_url: 
---

![](https://cdn-images-1.medium.com/max/1024/1*5IaZdfxGvkqfUhNz4YXwxg.png)<figcaption>OpenWhisk enables event driven architecture from the ground up !</figcaption>

> 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](https://cloud.ibm.com/login).
3.[Optional — WSK CLI](https://openwhisk.apache.org/documentation.html#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](https://cloud.ibm.com/login). 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

![](https://cdn-images-1.medium.com/max/785/1*6d4UOrBPwNkeAGTCm2iAlQ.png)

3.Create a new action by clicking on **_Start Creating_**.

![](https://cdn-images-1.medium.com/max/1007/1*1d5ulxV9EtYh5Fda5NOKYQ.png)

4.Give the action a name and pick **_Python 3_** as your Runtime.

![](https://cdn-images-1.medium.com/max/968/1*WaQr84YLAEMzHAYooYBDmw.png)

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' }
```

![](https://cdn-images-1.medium.com/max/963/1*kF397V8W2KuYUBIR9EE0cw.png)

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’ }
```
![](https://cdn-images-1.medium.com/max/1024/1*Uzxi8VCZAE-OzvSgy1Cr_Q.png)    <figcaption>Action invoked with no event passed</figcaption>

6.Add the following parameter by clicking on **_Change Input_** as follows:
![](https://cdn-images-1.medium.com/max/1024/1*N3sC7xpppOemNtcyENufRg.png)<figcaption>Set event as input for the action</figcaption>

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](https://api.slack.com/events/url_verification).

![](https://cdn-images-1.medium.com/max/1024/1*2AAtuoWC-wnrqFubTiUX6A.png)

7.Click on **_Endpoints_** and enable the action as a web action

![](https://cdn-images-1.medium.com/max/1024/1*j_uDOkWq0yY6CMJ-2jJ0vg.png)

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

![](https://cdn-images-1.medium.com/max/1024/1*nd6KcWNbNSlG10e6fHqYXQ.png)

9.Create an API end point for your action

![](https://cdn-images-1.medium.com/max/1024/1*cXold5SVtP8HvRzGtoNfYQ.png)

10.Give a **_name_** , a **_base api path_** and **_create a new operation_**.

![](https://cdn-images-1.medium.com/max/1024/1*L9AHCPvoZVWViLDT8f5BxA.png)

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

![](https://cdn-images-1.medium.com/max/1024/1*ndPuMPZVzf5-akoj31nhWw.png)

12.Save the API

![](https://cdn-images-1.medium.com/max/1024/1*1NB8o66siDHN9JQot4_54g.png)

13.Grab the POST URL from API Explorer

![](https://cdn-images-1.medium.com/max/1024/1*Eakxf5_dXddqwAZun8xVDw.png)

`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

![](https://cdn-images-1.medium.com/max/723/1*WkcmGQa3zMUOoYTSYQ2i_Q.png)

![](https://cdn-images-1.medium.com/max/784/1*I3u3gfHmFc1zxBuJB6nNfQ.png)<figcaption>Create a new application</figcaption>

2. Add a bot user

![](https://cdn-images-1.medium.com/max/988/1*EJOfE8Totg-H5jR7UX7B0g.png)<figcaption>Add a new bot user</figcaption>

![](https://cdn-images-1.medium.com/max/1015/1*y0h_FZdlkVD5FArK_8wvzA.png)<figcaption>Add a new bot user</figcaption>

3. Subscribe to workspace and bot events

![](https://cdn-images-1.medium.com/max/779/1*uoISxzVjjKs2CHXeHY1fHw.png)

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

![](https://cdn-images-1.medium.com/max/1009/1*pRhj4rsJnoC0hdofv19ecQ.png)<figcaption>The bot subscribes to different message events</figcaption>

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.

![](https://cdn-images-1.medium.com/max/961/1*1XgNdKXy1c80CtNYuZauOQ.png)<figcaption>Add request URL and verify in Slack</figcaption>

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

![](https://cdn-images-1.medium.com/max/920/1*V5DUZSUlqDTfQzMQPEInjA.png)

![](https://cdn-images-1.medium.com/max/581/1*sEy3NvrcgkkvO8BYZmehPA.png)

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

![](https://cdn-images-1.medium.com/max/652/1*_-DUBREVFfO__Rca-IytJw.png)

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.

![](https://cdn-images-1.medium.com/max/989/1*xQo2xZR4kBtdaSldPAsFwQ.png)

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.

![](https://cdn-images-1.medium.com/max/956/1*SzoDYziioRLTbtu8DjjvNw.png)

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,](https://medium.com/u/bf01a11701fe) [Carlos Santana,](https://medium.com/u/1f2fe78a0cf3) [Tomomi Imura,](https://medium.com/u/a0b0677819e4)