How to Create Slack Apps with Python

Sasidharan G
Python in Plain English
6 min readApr 5, 2021

--

Slack is a proprietary business communication platform developed by American software company Slack Technologies. Slack offers many IRC-style features, including persistent chat rooms organized by topic, private groups, and direct messaging. To know more about it, visit here.

With the help of slack, we can build our own applications and we can use it for our daily usage. I have built many such applications which are helpful for my team.

After you, signup/login to slack, click here and click on Create New App. Provide a name to your app and select the development workspace that you want to create.

So, the architecture of our app would be:

The problem with this architecture is your app must reply to the HTTP POST request with an HTTP 200 OK response within 3 seconds of receiving the payload and if your app fails to do so, slack will send three retry events, and also slack evaluates the success rate of at least 5% of events per 60 minutes to prevent automatic disabling. To know more about it, visit here.

So, to avoid that we are gonna redesign our architecture slightly differently as below:

Here, lambda 1 sends the response to slack immediately and triggers lambda 2 to handle the payload and generate the response to slack. Also, there are many different ways to avoid first architecture setbacks like introducing SQS and handling the response but having synchronous/asynchronous lambda is the easiest way to get it done.

Now, we are gonna with the second architecture and it’s all about selecting the scopes and permissions that we want for our app. To start with, go to OAuth & Permissions in your app and click on Add an OAuth Scope, and select app_mentions:read, files:write and chat:write.

There are hundreds of scopes which has a specific purpose and to know more about it, click here and go through it.

Now, go to Event Subscriptions under Features and select the toggle on to receive events and slack would ask for a Request URL, and would they would send us a parameter and would ask to return the value of it. Let’s see how to achieve it.

Select app_mention in the Bot user events as shown above.
Now, let’s start configuring at the AWS.

Go to API Gateway and follow the below steps:

To complete this, we need to create our lambda function and come back here.

First lambda → Receive event, trigger another lambda and return 200 immediately to slack.

Second lambda (Asynchronous)→ Receive payload from first and process it.

Name first lambda and select python latest run time environment and select basic execution role.

Kindly visit my GitHub to get the code for our first Lambda function. Modify slightly, in the last return code replace it by just return event.

Replace:

return { ‘statusCode’: 200, ‘body’: json.dumps(‘Success, Received payload’) }

With:

return event

This is because in the initial verification for our slack, they expect us to return a parameter to complete request verification. Once we have successfully verified, we need to revert it back.

Now, create a second Lambda function and select runtime as python 3.8 and leave it empty for now and copy its ARN and paste it in our first lambda.

Let’s go back to our API Gateway and finish our setup.

Once you deploy the API, we will get this URL and copy this URL and go to the Slack API Events page. As soon as we pass the URL, Slack verified it successfully and click on Save Changes.

Now, we will start receiving events. As said earlier, once verification is complete we need to revert the changes that we did not for our first lambda.

Replace:

return event

With:

return { ‘statusCode’: 200, ‘body’: json.dumps(‘Success, Received payload’) }

Let’s go back to our second lambda function and complete it. In order to do so, visit this page and get to know how to create a lambda python deployment package because we would be using few packages that wouldn’t be readily available in AWS lambda.

Once you have created the lambda deployment package, copy the function code for the second lambda from my github. Copy the token in your slack app and paste it in your slack lambda in the token variable. Ideally, you should not put sensitive variables like tokens/passwords in lambda code in your production environment. Instead, you can have it in AWS Secret Manager or Lambda environment variables with encryption or in any other safer place. To just keep it simple, I have configured it this way.

Now, upload the zip file in your second lambda in Code/Upload from: Zip file. That’s it, we have done all the configurations and we just have to add our application in slack.

In any one of your Slack channels, type @your-app-name and it will ask you to add to the channel and click on it.

As of now, I have configured only one command called help and it would respond to it stating “I am your slack helper ” and for any other text other than help, it would ask you to Configure more. You can see the same in the above image.

Now, this is just a small example of what we can achieve in slack.

Few examples of slack application for the dev/ops/monitoring teams:

  1. Getting AWS metrics from slack and generating cloudwatch image.
  2. Increase RDS Storage from slack.
  3. Integrate the slack app with your deployment/release tool to get frequent updates.
  4. Handle code approval\push from slack and many more…

Also, you can have user restrictions in your slack application, and all you have to handle it in your code. We don't want everyone to have access to the RDS Storage app right!

So, imagine you get a call from the monitoring team that RDS storage is getting full and in the middle of a night, and instead of logging in, you can use your mobile to open the slack application and increase the storage if needed. How cool is that right!

Few snapshots of the above-said applications:

To get aws metrics from slack
RDS CPU of a Shard
To post important tickets in Slack to the respective team to be taken care of.
Increasing disk for an rds shard from slack

Conclusion

I understand this is a bit long article, and please take one step at a time and kindly comment below if you have any clarifications and I am happy to share this with you and if you have time, please do share your feedback and it would certainly boost me to do more like this. Keep inspiring others! Ciao.

More content at plainenglish.io

--

--