Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

Build a basic bot on Slack in 5 minutes

Build a basic bot on Slack in 5 minutes
Photo by Alex Knight / Unsplash

Slack is a popular chat application that's often used by remote teams. Slack bots are third-party applications that you can install to add features and functionality to your Slack workspace.

In this tutorial, we are making a simple bot to send messages on a Slack channel.

First, go to https://api.slack.com/apps/.

Click on "Create an App". For Slack, all bots are apps, so that's what we need to do.

We will choose to create a Slack app from scratch.

Now, we just have to give it a name and select the Slack workplace for this app.

  • Important note: it's going to be easier if you're currently logged into the account that is the owner of the workspace you selected.

Once we have created the app, we will select "Bots".

Now that our bot is created, we need to define what the bot is allowed to do.

Going to the "OAuth & Permissions" tab, we will scroll down to "Bot Token Scopes".

We will add two scopes (things that we allow the bot to do):

  1. chat:write – Send messages as A Simple Bot
  2. chat:write.public – Send messages to channels A Simple Bot isn't a member of

Now that we have defined what the bot can do, we just need to get one last thing to use the bot.

We will need to retrieve the OAuth Tokens for the bot. Think of it as a password or a key.

Still, under the "OAuth & Permissions" tab, we will click on "Install to Workspace".

  • Important note: If you're currently logged into the account that is the owner of the workspace you selected, then you can just press Install To Workspace. Otherwise, you will have to Request to Install it, and the actual workspace owner has to approve it.

Now, we can see the "Bot User OAuth Token", which is the key/password we want to keep. Copy and paste it somewhere safe.

Now we have everything we need:

  • created a Slack app called A Simple Bot
  • granted scope/permissions:  Send messages to channels
  • password/key: Bot User OAuth Token

Let's try sending a message to a Slack channel now using curl.

After replacing xoxb-your-token with your Bot User OAuth Token,  you can copy and paste this command into your Terminal. This will send an API request to Slack through curl in our Terminal.

curl -X POST \
     -H 'Authorization: Bearer xoxb-your-token' \
     -H 'Content-type: application/json;charset=utf-8' \
    --data '{"channel":"#general","text":"Hello, Slack!"}' \
https://slack.com/api/chat.postMessage
  • replace xoxb-your-token with your Bot User OAuth Token
  • the channel is set to general
  • the message we are going to send is "Hello, Slack!"

Once you run this command, you should see the message on your Slack!

Awesome! We have done the most important steps to making a simple Slack bot.

We are basically able to send a message to a channel whenever we want.

We can also send an API request to Slack using all kinds of methods besides curl:

Here's an example of sending a message to a channel using the Ruby API SDK:

slack_client = Slack::Web::Client.new(token: "xoxb-your-token")

slack_client.chat_postMessage(channel: '#general', text: 'Hello World', as_user: true)
  • We log into Slack using our Bot User OAuth Token
  • We then send a message to the #general channel with the message "Hello World".

That's all. Thank you for reading!