Tutorials, PHP & MySQL, API

Post to Slack using the incoming webhooks API

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:

<https://daveismyname.blog|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.

David Carr

David Carr

For the past 12 years, I’ve been developing applications for the web using mostly PHP. I do this for a living and love what I do as every day there is something new and exciting to learn.

In my spare time, the web development community is a big part of my life. Whether managing online programming groups and blogs or attending a conference, I find keeping involved helps me stay up to date. This is also my chance to give back to the community that helped me get started, a place I am proud to be apart of.

Besides programming I love spending time with friends and family and can often be found together going out catching the latest movie, staying in playing games on the sofa or planning a trip to someplace I’ve never been before.