How to create a Slack robot in 3 easy steps
Tags: showdev, beginners, webdev
Published: 2020-05-02 16:04:28

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](https://api.slack.com/apps). 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](https://api.slack.com/apps).
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](https://api.slack.com/scopes).


#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](https://dev-to-uploads.s3.amazonaws.com/i/gz7ugarsuq0g5dvao822.png)

Find your app and click on the "Add" button

![Add app to slack channel](https://dev-to-uploads.s3.amazonaws.com/i/6z5qiqqegu4s8ym7ubx5.png)

#Step 3 - Post messages in a Slack channel

Copy your app access token:

![Slack access token](https://dev-to-uploads.s3.amazonaws.com/i/x585l4zbo0z5hn88l34m.png)

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

![Slack channel ID](https://dev-to-uploads.s3.amazonaws.com/i/cn2zak9me1m5mugxox6k.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/1jhrewfz37vlzzjov4ro.png)

And that will be all guys, if you want to make more complex messaging interactions I recommend you going to the [Slack documentation](https://api.slack.com/messaging) and read more about it.

See you next time.