08 November 2020
by
Jan Vorčák's profile photo, Webscope.io
Ján Vorčák

Slack Tutorial Part 5 - Incoming Webhooks

Slack

Setting up OAuth

In our previous post, we have created a slack command, which needs to be initiated by a user writing /<name-of-the-command>. In the next articles, we will learn how your app can post a message to Slack directly as a reaction to some event. In our example, we will send a notification about new project report once user reports it.
For doing this, we need a Slack Webhook URL where we will send blocks to be displayed. You can either take them from Slack Dev UI, but that doesn't really help if you want to distribute your app to users. We will need to generate them programmatically and that is exactly where OAuth flow will help us.
In this tutorial we will skip the part where you can manually generate a webhook and send data to it. It's pretty easy and it doesn't really help with a production app. If you still want to go through this, follow this tutorial.
Banner

Do you need a reliable partner in tech for your next project?

What is OAuth

Describing OAuth is out of scope of this article. [This Digial Ocean's tutorial] is probably the one that explains it in the best way. In their article, they have the following image:
Source: https://www.digitalocean.com/community/tutorials/an-introduction-to-oauth-2

Configuring Your App

In our case, Application is our Slack application and Slack API is the Service API.
  1. Go to your Application Settings, and define your Redirect URL. This is where Authorization Server (Slack) will send you an Access Token.
  1. In your Application Settings > Manage Distribution you'll find a different ways how your users can install your app.
We'll choose Embeddable Slack Button and put it on our homepage. Have a look at this commit to see how to do this if you're not experienced with Django.
The important part is to add a new entry to your urls.py, create a view that renders HTML and add a Slack button. Optionally, you can define your base.html so that you can re-use the basic styles in other templates e.g. redirect address url that Slack will follow after installing our application.
Right now, we see the following screen at our index.
After your users click the button, they'll be redirected to Slack (Authorization Server) (Step 3.)
And Slack will redirect them to your application (Step 4.) with an Access Token.
😮 Our redirect URL doesn't exist, we need it to store an access token. We will use it to talk to Slack later on.

Setting up Redirect URL

When Slack redirects to our URL. We will have the following data in request.GET.
We will need these data to interact with Slack later on. Let's build models that will persist this information in a database.
Share post

Let’s stay connected

Do you want the latest and greatest from our blog straight to your inbox? Chuck us your email address and get informed.
You can unsubscribe any time. For more details, review our privacy policy