Integrating Slack Outgoing WebHooks with a Rails App

Building an app to send tweets from Slack

Paul Gonzalez
LaunchPad Lab
3 min readSep 2, 2015

--

It sounds crazy, but I just started using Slack this week. 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 9 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.

This is the main screen in Slack where you will manage your outgoing webhook.

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.

Slack’s explanation of the POST request makes it very simple to understand. Note: Slash commands send slightly different POST requests.

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

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_webooks_controller.rb

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!

If you like what you read, please recommend and share this post!

Also, feel free to follow me on Twitter @paulgonz6. The repo for this project can also be viewed at https://github.com/paulgonz6/slack_twitter_app.

--

--

Paul Gonzalez
LaunchPad Lab

Head of Product at LaunchPad Lab. I mostly write about product and business stuff.