It sounds crazy, but I just started using Slack this month. I know, I know. I’m way behind the curve. But for the past year, I have been a “student.” Building apps on my own. Attending Starter School. And otherwise hacking away trying to learn as much as possible.

That’s all to say, I wasn’t really working on a team. And the need to continuously converse with teammates and clients wasn’t there. That all changed this week when I joined LaunchPad Lab in Chicago. And now I realize what all the hype is around Slack.

It also gave me my first chance to integrate Slack’s Outgoing WebHook abilities with a rails app.

THE GOAL OF THE APP

LaunchPad Lab currently has 8 employees (with hopes of growing quickly over the next year). We want to increase our presence on Twitter. We also want to make it easy for all team members to feel empowered to share their thoughts publicly, on behalf of the company.

Instead of having each team member login to the company account on Twitter, and since we use Slack so frequently, we want to be able to send Tweets directly from Slack. Pretty cool, huh?

The rest of this post will focus on the integration of Slack and the rails app. I will not focus on how I post the tweets.

SLACK SETUP

Slack makes creating an Outgoing WebHook extremely easy. If you are logged into your account, simply go to:

https://[your-team-domain].slack.com/services/new

From here, you are looking for the Outgoing WebHooks link. It’s pretty hard to miss.

Once you click on that, it will bring you to a nice interface to manage your new web hook.

You can select exactly what channel you want the web hook to integrate with (or all channels). You can choose specific trigger words (in the case of our app we use the trigger word tweet). And The URL is where Slack will send the POST request. More about the POST request below.

Slack does a great job of explaining what is included in the POST request. For the case of our app, the params[:trigger_word] will be tweet, and the params[:text] will be the content of the tweet we want to send.

The request also includes information that can help you with authentication, including token and team_domain.

It’s important to note that Outgoing WebHooks do not send any information about attachments in the POST request.

This is pretty much all you need to setup in Slack. Easy peasy!

RAILS SETUP

If you thought the Slack setup was easy, wait until you see how easy it is in Rails!

First we need to set up the routes. Whatever you call your resource should match the URL you included in the Slack setup.

config/routes.rb

Rails.application.routes.draw do
  resources :slack_webhooks
end

Because we have setup our routes with rails resources: we automatically get the ability to handle a POST request by sending it to a create action.

app/controllers/slack_webhooks_controller.rb

class SlackWebhooksController < ApplicationController
  def create
    # do something cool with the params
    # we used params[:team_domain], params[:token] for authentication, 
    # and params[:text] and params[:trigger_word] to create the tweet
    #
    # send response back to Slack
    render json: { text: "Some message you want to include" }
  end
end

The create action is where you will include all your fun business logic (for us it’s sending a tweet).

You can also create a message to send back to Slack by rendering a JSON response. Slack expects a text: property, which it will post to the Slack channel it was sent from.

That’s it. You and your rails app are web hooked!

Paul Gonzalez

Product Developer

Paul has always been a builder of things. K’nex were his tool of choice as a kid, but as an adult he has turned his focus to software. Prior to joining LaunchPad Lab, he spent six years in investment banking where he learned a valuable lesson he still uses today- run towards problems. Paul has also yet to find a beer he won’t try, a competitive reality TV show he won’t watch, or a fairway he won’t miss.

Ready to Build Something Great?

Partner with us to develop technology to grow your business.