Send Messages to Slack via Webhook

Jason Wu
5 min readApr 15, 2022

--

Never miss an important update. A step-by-step guide to creating a real-time alert system in Slack.

Photo by Stephen Phillips - Hostreviews.co.uk on Unsplash

Slack is a business communication software, more commonly used within teams in workplaces and organizations as chat rooms and private messaging. A not-so-common-known feature of Slack is its comprehensive API where you can build your own automation app on the platform to make your life easier and broadcast needed information.

Building your own automation system can assist you with your daily tasks by creating alerts, notifications, and subscribing to needed channels without missing a beat. One popular method to achieve this goal is using Webhook, a mediator that allows you to connect Slack to your own system/program.

Today, we will dive into How to send messages to Slack via Webhook. (One-way communication)

Here’s a sample end result with alerts on Bitcoin price updates every 2 seconds:

High-level Data Flow:

Technology being used:

Limitation: The logic only sends the message to Slack one way. Slack cannot send messages back to the logic.

Steps:

  1. Create a Slack app & Slack channel
  2. Enable & Create an incoming Webhook
  3. Send messages to Webhook

Workplace setting:

If you are looking to apply this method to your Slack workplace, I highly recommend checking in and obtaining approval from your IT Admin first. Since Webhook is a one-way communication to Slack, it would not require read access to Slack messages.

Step1: Create a Slack app & Slack channel:

First, we want to create the Slack app that handles all Webhook requests and a Slack channel to receive all the messages that are being sent to Webhook.

How to create a Slack app:

Once you log into your slack account, you can use this link (https://api.slack.com/ ) to enter the Slack API main page. You can locate Create an app button on the page, and select From scratch. Enter your App Name. For our example, let’s call it super_messenger. Select your workspace where you want to deploy the app. Complete this step by clicking Create App button.

How to create a Slack channel:

Creating a channel is relatively easy. In the Slack application interface, you can click on the plus (+) button next to the channel and give it a name. In this example, we can call it playground.

It doesn’t matter whether you mark it as a private or public channel, but note permission scope will be different if you mark it private since this can no longer be made public in the future.

Step 2: Enable & Create an incoming webhook

After creating your channel, the App Setting page will appear. On this page, select the Incoming Webhooks in the Features section, and enable Activate Incoming Webhooks.

Once this feature is enabled, the Webhook URLs for your Workspace will be created. Select the Add New Webhook to Workspace button at the bottom of the page.

Now, we need to tell Webhook which Slack channel we want to send these messages to. In our example, we will send the message to the playground channel we created earlier in Step 1. Select the playground channel and select Allow.

Once you click Allow, it will redirect you to the previous page where Webhook URL has now been created. Please keep this URL safe as anyone in your network can send a message to this URL.

Step 3: Send messages to Webhook

Above the website URL is a sample CURL request. We can now copy + paste the CURL request to your Terminal on your computer. This will enables messages to this Webhook link and Webhook will forward the message to the Slack channel. You will notice the data structure sent to Slack Webhook is in JSON format.

As we don’t want to manually send messages in the terminal by using CURL each time. Let’s write some Python code to automate this step.

For this example, we will use the channel to monitor Bitcoin price updates. Here, I am going to use Python to get Bitcoin data from Binance every 2 seconds and send it to the Webhook, where it will be forwarded to the playgound channel in Slack.

Full code:

Outcome:

Other ways of sending messages to Slack.

Slack Webhook is not the only method to send messages to Slack channels. There are also other Slack APIs that you can use to achieve the same (https://api.slack.com/messaging/sending#threading). However, Slack Webhook has one of the lowest security concerns, as its one-directional communication messaging tool. We will later explore other Slack APIs to send & read messages from Slack.

Upcoming:

Another use case is creating a Slack chatbot to enable two-way communication on Slack. I will post how to achieve the below example in a separate article.

Thank you for reading.

--

--

Jason Wu

Avid Traveler, Investor, Curious Learner. Hobby blogger on process automation and data analytics.. for now.