A really simple Slack integration in Rails
Tags: ruby, rails, slack, integration
Published: 2018-07-13 14:54:40

---
title: A really simple Slack integration in Rails
published: true
tags: ruby,rails,slack,integration 
canonical_url: https://neil.bar/a-really-simple-slack-integration-in-rails-eeae365d5f38
cover_image: https://cdn-images-1.medium.com/max/1024/1*NY2qPVbEMLWkX9Mur_qWJg.jpeg
---

I’m a big fan of [Slack](https://slack.com). One of its strengths is the large list of [integrations](https://slack.com/integrations) that it provides ‘out of the box’. I’ll cover another strength later (when I get my soap box out!).

Clearly, there isn’t likely to be an integration for ‘my bespoke rails webapp’ but Slack have you covered by supplying a framework to allow incoming notifications.

![](https://cdn-images-1.medium.com/max/692/1*3wBSCaJlHSk1kuq3losm_A.png)

I’m going to take advantage of this to send event details from ‘my bespoke rails webapp’ to Slack.

#### Real life use case

The application which I develop, has a bunch of internal mailers hanging off it. These send mails to specified people when certain actions have occurred. In this example we’ll take the case of **‘someone clicking the wibble button’**.

### Soap Box

![](https://cdn-images-1.medium.com/max/1024/1*WkrhMp77kTaZey5jpP3J7A.jpeg)<figcaption><a href="https://creativecommons.org/licenses/by/2.0/">Creative Commons</a> by <a href="https://www.flickr.com/photos/bmcirillo/">Brandon C</a>. No changes were made.</figcaption>

**_Email sucks_**. I get too much of it (I probably send too much as well). By allowing these mailers to exist (and by adding new ones), I’m perpetuating this pain.

### Setup

![](https://cdn-images-1.medium.com/max/1024/1*4xhTahwmWxmg9ny4Dty86Q.png)

In the ‘DIY Integrations’ section of the Slack admin interface you’ll see the option to add ‘Incoming Webhooks’. Select this and choose the default channel you’d like your notifications to appear in (this can be overriden on a case by case basis).

What we need from this is a unique ‘Webhook URL’. Once we have this, we can start putting together a payload.

![](https://cdn-images-1.medium.com/max/1024/1*j68Z2uqT4mmHlYKWdL9ECg.png)

### Integration

#### Specification

I’m not going to copy-paste the fine documentation produced by Slack. Instead, here are some links!

- [https://api.slack.com/incoming-webhooks](https://api.slack.com/incoming-webhooks)
- [https://api.slack.com/docs/attachments](https://api.slack.com/docs/attachments)

#### At last, some code!

Add your webhook URL and test channel to your development _application.yml_.

_The webhook URL will likely stay the same in your production environment, but you may want to update the channel name._

{gist https://gist.github.com/neilbartley/f1ac92cf40f79a789e64 %}

I’ve created a service for our our SlackBot, which could simply be called from the controller.

{gist https://gist.github.com/neilbartley/87bbcb97c67617d16344 %}

To demonstrate, lets call from rails console:

{gist https://gist.github.com/neilbartley/21d1d6e1d4ffb09700c9 %}

Line two will produce the following notification in the #events\_test channel:

![](https://cdn-images-1.medium.com/max/966/1*eUHKRxHgLaVjXiLMOZhC8A.png)

Lines four and six illustrate overriding the default channel to another channel or user.

* * *