X
Popular Searches

How To Automate Your Slack Messages

If you’re constantly sending repeated messages to Slack, and want to automate the process, you can do so from your primary Slack account, without adding a new bot user. This will allow scripts and programs to send messages as you.

How Does This Work?

When you create a new Slack App, their API allows for two types of capabilities. The first type adds a bot user, like Slackbot, that you can interact with separately from your account. This is often used to build Slack integrations for other services, like Trello or Jira, that you can add to your workspace.

The other kind of Slack app is one that users can authenticate with to give out access to their account. One of these permissions is chat.write, which gives the Slack app permission to send messages as you. If you give your own app permission, you’re essentially given an OAuth token that you can use to access Slack’s API and send POST requests for new messages from your own account.

It’s easy to set up, but since you’re still required to use the Slack app system to get the key, you may still need to get permission from your workspace administrators if adding new apps is disabled for you. However, you’re not adding a bot user or anything, just requesting an OAuth key with the single scope of sending messages as yourself, so it should be fine given an explanation of the situation.

If you do want to set up a bot user, you can read our guide to doing that instead.

Setting Up A Slack App

To get started, head over to Slack’s Developer Portal and create a new application. You can eventually activate public distribution and submit it to the Slack App Directory to have the app be freely installable, but for now, simply choose the workspace you plan on automating messages in as your development workspace.

Before you request the app be added to the workspace, you’ll need to define its scopes. Under “OAuth and Permissions” in the sidebar, scroll down to the “Scopes” panel, and add a new scope under “User Token Scopes.” Unlike general permissions, these are specific to your personal account.

Add chat.write to the list. If you require anything else, add it here as well, but chat.write will give it the simple permission to send messages as you, once you’ve authorized the app to do so.

Once the scope is added, you can request the app be installed to your workplace from the “Install App” tab.

If you need permission from your workspace administors, Slack will send them a message informing them of the request. If not, you’ll simply have to authorize it yourself.

After it’s approved, you

Sending Messages

The API endpoint for sending messages is:

http://slack.com/api/chat.postMessage

This takes a few options, but you’ll just need:

  • token, which passes the auth token you generated above,
  • channel, which tells the bot where to post,
  • and text, where you pass what you’re writing.

If you’re mentioning users, you’ll also want to set parse to full for it to display properly.

To get the channel ID, right click on the channel in Slack, and press “Copy Link.” This will copy the full web URL—the channel ID is the last part of that.

All in all, the final URL will look something like:

http://slack.com/api/chat.postMessage?token=xxxxxxxxxx&channel=xxxxxxxxx&parse=full&text=Hello

You can send a POST request here using any HTTP request methods in your scripting language of choice. For example, this can be done easily in JS using axios:

const axios = require('axios');

const text = "Hello, Slack!";

axios.post('http://slack.com/api/chat.postMessage?token=xxxxx&channel=xxxxx&parse=full&text=' + text)
                .then((res) => console.log(res);)

If you want to send more complex messages, you can look into Slack’s Block Kit Builder, which allows for richly formatted messages using JSON.

Anthony Heddings Anthony Heddings
Anthony Heddings is the resident cloud engineer for LifeSavvy Media, a technical writer, programmer, and an expert at Amazon's AWS platform. He's written hundreds of articles for How-To Geek and CloudSavvy IT that have been read millions of times. Read Full Bio »

The above article may contain affiliate links, which help support CloudSavvy IT.