Use Ruby To Post Content To Slack

Once you learn how to use Slack's incoming webhooks, the possibilities are endless. See how to post to Slack using Ruby.

Slack has a really nice feature called Incoming Webhooks. It enables you to post messages with dynamic content, using a dynamic username and avatar.

The possibilities on what you can use this for are endless. For example, you could post score updates on sporting events, changes within another service, like git commits, or project tasks.

Before we get into this, I want to mention one thing. Always make sure your solution doesn't exist before you try to create one. For example, I wanted to post commits pushed to Bitbucket in my Slack channel. Well, there is already an integration written for that. Another time I wanted to have a running feed of Basecamp activity, and while there is no Slack integration, there is a nice script to which I made a few changes and now run as a cron job.

If your solution doesn't already exist, let's get into it!

01: Create Incoming Webhook

The first thing to do is to setup an incoming webhook service integration. You can do this from your services page. The URL is [your-team].slack.com/services/new.

Here choose Incoming Webhooks, then pick a channel and add the integration.

Do note that the channel you pick doesn't have to have any bearing on the channel to which you actually post, as you can change that with the payload you send to Slack.

02: Send The Request

From here, it's really quite simple, especially with Ruby. But let's keep it clean and wrap it up in a method.

require 'json'

def notify_slack(webhook_url, channel, username, text, image)
payload = {
:channel => channel,
:username => username,
:text => text,
:icon_url => image
cmd = "curl -X POST --data-urlencode 'payload=#{payload}' #{webhook_url}"

We set up the payload, then send a curl request. This means you need to have curl installed on your machine.

When you want to send a request, just call the method, assuming it's already defined.

'I Am Not A Robot',
'I am not a robot, even though I look like one.',

Sure, you can clean it up from here, but it really is that quick. Now go do something fun with it!


Let's Connect

Keep Reading

Parse a Web Page and Post to Slack Using Ruby

When another website is doing the heavy-lifting updating some content you want to keep up on, but there's no good integration with Slack, you can parse it yourself and post directly to a Slack channel.

Feb 24, 2016

Use Slack For Rails Error Notification

Post a message to one of your Slack channels when your Rails app encounters a 500 error.

Dec 22, 2015

A Quicker Way to Compare Multiple Equals Operators in Ruby

When you attempt to write several predictable comparisons in one statement, it gets ugly fast. Here are some methods for cleaning it up.

Apr 20, 2015