Use Ruby To Post Content To Slack

Feb 07, 2016 Ruby, Slack

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]

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!

Here's the gist for the method above.


Did you learn something or find this article interesting?

If so, why not