Post to Slack using the incoming webhooks API

David Carr

API Tutorials PHP & MySQL

Slack is a great tool for teams I use it to communicate with the rest of my team, recently I’ve been wanting my web applications to send notifications to certain Slack channels. The Slack incoming webhook API is perfect for this task.

What is a incoming webook?

Incoming Webhooks are a simple way to post messages from external sources into Slack. They make use of normal HTTP requests with a JSON payload that includes the message text and some options. Message Attachments can also be used in Incoming Webhooks to display richly-formatted messages that stand out from regular chat messages.

To get started head over to https://my.slack.com/services/new/incoming-webhook/ to create a new integration, select the channel to be used. Take not of the provided Webhook URL you will need that to post updated to.

It looks something like this:

https://hooks.slack.com/services/456565/gffg5532/2ImDbgSZW89HBZApVOb

Messages are in json format to send a plain text message the format is:

payload={"text": "your text"}

Line breaks can be added by placing \n where you want a line break. Links are in the format of:

<|Click here>

To change the default display name:

"username": "new-bot-name"

also the icon can be changed by either:

"icon_url": "https://slack.com/img/icons/app-57.png"

or

icon_emoji": ":ghost:"

All of this should be added to Json and posted to the webhook url. To make things easier a function can be used. The function below sets the text to send, the roo and icon and username. 

The room, icon and username cna have default values meaning the only parameter you need is the message. When ran the params are added to json_encode to create a json object and then posts to your webhook url using curl.

Import the webhook url must be set in curl_init().

function post($message, $room = "#general", $icon = ":ghost", $username = "BOT") 
{
    $data = json_encode(array(
        "channel"       =>  $room,
        "text"          =>  $message,
        "icon_emoji"    =>  $icon,
        "username"      =>  $username
    ));

    $ch = curl_init("Your-Slack-webhook-url");
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS, array('payload' => $data));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $result = curl_exec($ch);
    curl_close($ch);

    return $result;    
}

Usage is very simple to post and use the defaults set in the function:

post('Sample text to send to Slack!');

To post to a user with a username of abc123 then remove the # and use a @:

post('Sample text to send to Slack!', '@abc123);

The same goes for setting the icon and username:

post('Sample text to send to Slack!', '@abc123', ':superbot', 'AceBot');

When ran the result is returned for success will contain ‘OK’ for failure an error message is returned.

This makes it very easy to post messages to your team in slack.

Read articles directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Copyright © 2006 - 2024 DC Blog - All rights reserved.