Slack chat all the things!


Get alerts with incoming webhooks

I’m mostly a one-man show when I’m working, so team chat applications like slack and hipchat have never really appealed to me – who would I talk to? I’m sure (and reviews agree) that these tools are great for teams, but I’ve found a reason to use slack chat that even solo developers might enjoy.

Incoming webhooks

Slack allows you to create your own integrations into their platform, and their api allows some pretty advanced stuff, but their incoming webhooks are so simple and awesome that I’ve been finding uses for them left and right.

Here are some ways that incoming webhooks have been helping me out and saving me time:

  • Alerts when updates are available for wordpress sites that I manage.
  • Server reboot alerts.
  • Application error alerts. Using the function lower in this post, I’ve started to add slack alerts to my applications so that I get instant notifications on fatal errors, and odd issues that I might want to look into.
  • Suspicious activity alerts, like too many failed login attempts and such.
  • New user alerts for a service I recently launched.

Here’s the simple PHP function that I wrote for sending messages to slack from my applications:


function send_slack_message($username, $message){
	$url = 'YOUR SLACK WEBHOOK URL GOES HERE';

	$payload = json_encode(array('username' => $username, 'text' => $message));
	$slack_post = array('payload' => $payload);

	$handle = curl_init($url);
	curl_setopt($handle, CURLOPT_POST, true);
	curl_setopt($handle, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($handle, CURLOPT_POSTFIELDS, $slack_post);
	curl_exec($handle);
}

This function is super simple to use:

	send_slack_message('some-username', 'Your message text goes here');

You can also send slack messages using curl right from the command line on OSX and Linux (assuming you have curl available to you).


curl --data 'payload={"username":"some-username","text":"Your message text goes here."}' https://hooks.slack.com/services/XXXXXXXXX

Just be sure to properly escape your message text.

You can find out more about incoming webhooks in the slack api docks.