Wednesday, 8 April 2020

Create, Install and Manage a slack app - OAUTH2 Flow

Hey,
So, here we will go into details about creating, installing and distributing a slack app.

This is where you start.

I created an App to send random cat emojis. I used AWS to host a service used for this app. So yeah, building a slack app is free but will cost you something eventually if you want to distribute it too.

First, create one app. 
Create a bot user. You can assign permission to it. So, unless you give the bot permission for doing something, it can't do it. You always have to give permission and scope and reinstall if your permission is changed.

Normally by default, each slack app is installed just in its own developing workspace.
To install it to another workspace, you need to build an oauth2 flow. 
Oauth2 is basically a service that will exchange temporary token and after installing, return a bot user oauth token for that particular workspace. Now, you can save that token for that particular workspace and use it to post a message as bot token.

Read more about oauth2 here - https://api.slack.com/legacy/oauth

So, nonetheless, I created a free AWS account. It is free for one year. Yay!!
I used the serverless framework to deploy the service and see logs. Google up about this to find how to set it up. https://serverless.com/framework/docs/providers/aws/guide/credentials/
sls deploy --------> to deploy the service
sls -f <function name> -t    -------> to see logs

So, now coming more to oauth2 flow,

This is what will happen.
  1.  There is a button where the user can click. It will ask the user to sign into to workspace in which they want to install. 'Add to Slack' button. Instead of a button, there can be a link as well to click on. Following is the link of my helloKitty app - https://slack.com/oauth/v2/authorize?client_id=1043883525779.1063323392868&scope=commands,incoming-webhook
  2. If already signed into multiple orgs, you can choose which one to install to.
  3. After reviewing the scope, the user hits the allow button. Then it is redirected to the URL provided when creating an app. -  Redirect URL. 
  4. On this URL, you will receive the temporary token.
  5. You are supposed to return it.
  6. In response, you will receive a bot token. These tokens don't change unless this revoked.
  7. You can use this bot token for posting a message.

Important points to keep in mind - 
  • Since this redirect URL is one for all, we indeed need to have one unchanging endpoint. This creates a need to have extra service. 
  • You will also need to store the token against workspace name/id to post a message there.
  • Client ID and Client Secret will be known to you once you create the app.

Following is the snippet of code written for oauth2.




To debug - if your logs say, not able to find a particular module. Add it to your package.json and do npm install.

With this, we are done with oauth2 flow.

Anyone should be able to install your app now.

I created one slash command too just to explore the whole stuff. 
It also takes one constant URL. Each time that command is called, it gets triggered by the URL provided while creating one. So, for each slash command, we need one constant URL as well.
If you install this app, do /helloKitty
It will pop up one random cat image for you.

I have obviously followed a lot of bits and pieces from the internet. and some by asking slack experts.
I had the following detailed chat with another user but implementing it definitely cleared all the doubts. Adding it here, just in case if someone has similar doubts. Although, I have tried to clarify as much as possible in this blog.



No comments:

Post a Comment