DEV Community

Cover image for How to create a Slack robot in 3 easy steps
Edgar Gonzalez
Edgar Gonzalez

Posted on

How to create a Slack robot in 3 easy steps

Hey guys a few days ago I needed to create a Slack app to fetch details from one API and post into a Slack channel if there were any changes. I'm going to explain an easy step by step on how to create your own Slack app and post custom messages on a channel.

Step 1 - Get your APP token

The first thing you need is to do is creating a Slack app. You can do that going here. Once you have your app ready, you need to give it the correct permissions to post messages in a channel (for the purpose of this guide, I'm going to assume you only need to post messages to a particular channel, and you also know the channel ID as this was also my case).

Requesting these permissions is as easy as:

  1. Load up the settings for your app from the app management page.
  2. In the navigation menu, choose the OAuth & Permissions feature.
  3. Scroll down to the Scopes section, and pick chat:write from the drop-down menu.
  4. Scroll back to the top of this page and look for the button that says 5. Install App to Workspace (or Reinstall App if you've done this before). Click it.

You'll now see a permission request screen to install your app to its original workspace.

If you had already installed your app to its original workspace before, you might still see the permissions screen if the scopes you just added weren't previously granted to your app.

Authorize your app and you should see a success message. On the OAuth & Permissions settings page you're brought back to, you should now see an OAuth access token available.

Grab this token and store it for later, as we'll use that token to make some Web API calls.

If you want to read more about all the scopes you can add to your app and the details, you can click here.

Step 2 - Add your app in the channel

One important step also, is to add your recently created app to your channel, go to your channel details and click on "Add apps"

Add app to slack channel

Find your app and click on the "Add" button

Add app to slack channel

Step 3 - Post messages in a Slack channel

Copy your app access token:

Slack access token

Find the channel ID you will be posting the messages into, you can find it here:

Slack channel ID

After you have completed previous steps, is only a matter of making a post request to this API:

POST https://slack.com/api/chat.postMessage
Content-type: application/json
Authorization: Bearer YOUR_TOKEN_HERE
{
  "channel": "YOUR_CHANNEL_ID",
  "text": "Hello, world"
}

In my case it was something like this:

POST https://slack.com/api/chat.postMessage
Content-type: application/json
Authorization: Bearer <my-app-token>
{
  "channel": "test-channel",
  "text": "Hello, world"
}

You will get something similar to the image below on your channel:

Slack robot message

And that will be all guys, if you want to make more complex messaging interactions I recommend you going to the Slack documentation and read more about it.

See you next time.

Top comments (4)

Collapse
 
caroso1222 profile image
Carlos Roso

Very easy to follow, thanks for putting this up!

Collapse
 
edgargonzalez525 profile image
Edgar Gonzalez

thanks man.

Collapse
 
saulonunesdev profile image
Saulo Nunes

cool, thanks

Collapse
 
edgargonzalez525 profile image
Edgar Gonzalez

thanks for reading.