Consuming Slack Events with Event Subscription API and Slack App in Python

064_Pankaj Maran
Analytics Vidhya
Published in
5 min readJul 26, 2021

Slack is a common platform that every organization is using for communication among employees.

Wait, is slack only used for communication among employees only?

The answer, in my opinion is NO, because slack provides various APIs, Incoming webHooks, Bots creation, etc. which makes it multi-purpose. We can bind any other service to Slack. Not only this, Slack can be used as a buffer as well for communication among services.

If you want to build an event-driven service that runs your business logic whenever the change happened in Slack channels. A Change could be anything such as a new message received, any reaction toward messages such emojis, or anything. This is just one part of my intern project at SAP Labs India, where my microservice needs to deal with Slack. So, all sounds pretty interesting, right? So how cloud you bind your service with Slack? Is there any way? Without wasting network calls in polling, can we get updates of Slack Channels?

The answer is Yes, all this can be done in Slack with the help of Slack Events API and Slack App/bot.

*Note Slack bot is also a slack App that is designed to interact with users. So don’t get confused about where I have written slack App/bot. For understanding purposes, you can assume both as the same.

Let me walk you through the process of consuming Slack Events API and Slack App/bot creation. We will do this in 2 steps which are as follow:

1. Creation of Slack App/bot and subscribing to Events.

2. Consuming these events from our python service.

Step 1: Creation of Slack App/bot and subscribing to Events:

We want our service to get notified whenever any change happened in our Slack workspace. Right? We must tell all this to Slack as well. But how can we do this? Slack App/bot is the way of doing this. So let’s create one yoooo…..

  1. https://api.slack.com/ at the right top you will see Your apps option

2. Click on Your apps, the you can able to see all the apps you have created.

3. Click on the create app option.

4. Click on from Scratch and give name to your app and chose the workspace. Click on create app.

5. Now your app is created, Like we have hired a person but we need to assign a job to him. So let assign it. You must me be getting the same screen as image shown below.

At top of the leftmost column you will see your app name, in my case it is For-article-demo.

6. Chose the event subscription from the leftmost column under features.

7. Activate event subscription for your slack app

Now here in the Request URL, we need give URL of our service which should be on the web. Slack will verify this end point by sending a POST request to this URL. Our service should return challenge parameter as response or slack verification token (you can find this in basic information). Once this is verified Slack App will send all the future events on this end point only.

*Note if your service is not on web then you can use ngrok tool and forward traffic from ngrok to your local port.

After verification is should looks like this :

So till now we have hired a person and tell them to update us at our office by giving our address.

Now let’s assign some work to him, means subscribe to some events for our Slack App.

click on save changes after choosing bot events.

Code for verifying end point.

8. Click on OAuth & Permission and install app to your workspace.

Once you our app is installed in your workspace then one Bot User OAuth token is generated which can used for API verification.

9. Now let’s add our app in slack channels to get messages as events.

1. Type @app_name in chat box then hit enter

2. Here For-article-demo is a app name

step 2 : Now let’s Consuming these events from our python service:

Let’s send message in slack.

You will get event named message in your python service.

payload will look like the below:

*Note : I have replace the other details with xxxxxxxxx

Now you will receive all slack events which your app has subscribed. Finally, all done 🤩.

😵 A little long and confusing process right, but this feature is pretty interesting to work on.

Hope you enjoyed it!!!

References :

https://api.slack.com/apis/connections/events-api, https://www.youtube.com/watch?v=6gHvqXrfjuo

--

--