How to integrate Slack with your App

Reinhard Simeon
Code == Life
Published in
6 min readNov 30, 2016

We all know Slack is the leading team communication app in the world which is widely used not only by start-ups but also university students for their group projects and clubs. Slack is more than just a communication tool to send messages between human but also from our application.

We will setup a simple Sinatra app, a ruby based DSL for creating web applications, which receives webhooks and sends the data to our Slack channels. Please install Ruby if you do not have it on your computer.

Getting Started

Before we jump into making the app, we have to configure the Incoming Webhook app in our slack domain. From your Slack channel, find a link “Add an app or custom integration” or login to yourdomain.slack.com/apps, then search for “Incoming Webhooks”. You can setup the webhook to a specific channel and continue by adding the app.

The next part is to configure the app. The most important part here is to copy the webhook URL which is the endpoint to send data to your Slack group. We can customize the name, icon and label if needed. There are setup instructions that provides the general steps in case you want to do it in other programming languages.

That is all we need to configure from Slack. Let’s install some dependencies in your work directory.

gem install sinatra
gem install rest-client

Simple Use Case

We will create a Sinatra app that will act as the webhook to send username and email to our Slack channel.

Create a Ruby file called slack.rb and open the file with any IDE. This file will contain a class who’s job is to call the webhook URL we get from configuration and send data to our slack channel. Let’s take a look at the code.

require 'rest-client'
require 'json'
class Slackdef self.to_channel(channel, message)
RestClient.post(
'your webhook URL',
{
payload: {
channel: "#{channel}",
text: "#{message}",
username: "reinhardcustomnamebot",
icon_emoji: ":ghost:"
}.to_json
}
)
end
end

The Slack class has a class method called to_channel that accepts two string parameters: channel which is the targeted Slack channel and message is the message we want to send to our channel.

We make POST request which sends a JSON string as the payload parameter. We can customize the bot name and icon in the payload as well.

Now let’s create a new Ruby file in the same directory called app.rb where we run the app from and it will call the Slack class we created.

require 'sinatra'
require 'rubygems'
require 'bundler/setup'
require './slack'
post '/hooks/notify-channel-test' do
body = JSON.parse(request.body.read)
username = body['username']
email = body['email']
message = "Hello, my username is #{username} and email is #{email}"
channel = "channel_test"
Slack.to_channel(channel, message) message
end

This simple Sinatra app has an endpoint that accepts body in JSON format which contains the username and email. As the previous step, we now want to call the Slack class and call to_channel class method to send the message. The message variable is the actual message you will send to your Slack channel while the channel variable is the targeted channel. At the end of the file, message variable is used as the acknowledgment if the POST request is successful.

Other method of sending message

Beside the above Ruby code, we can also send message using a curl command like the following:

curl -X POST --data-urlencode 'payload={"channel": "#channel_test", "username": "reinhardcustomnamebot", "text": "Hello, my username is Reinhard and email is reinhard@reinhard.com", "icon_emoji": ":ghost:"}' https://hooks.slack.com/services/[your own url link]

You will see this in your slack channel:

Awesome! That is the first message we sent to our Slack channel! Beside sending a simple text message, the Incoming Webhook app is also able to send attachment with various formats. These attachments bring more content such as image, video, links, and highlight relevant pieces of data.

Let’s try Slack attachments

We have to change slack.rb in order to accommodate attachments:

require 'rest-client'
require 'json'
class Slackdef self.to_channel(channel, message, attachments=nil)
RestClient.post(
'your webhook URL',
{
payload: {
channel: "#{channel}",
text: "#{message}",
username: "reinhardcustomnamebot",
icon_emoji: ":ghost:",
attachments: attachments
}.to_json
}
)
end
end

The Slack class now has an optional argument that accepts an array containing attachment setup and data we want. The previous endpoint we created will still be working without an attachment.

Now we go back to our app.rb, add the following code:

post '/hooks/notify-channel-test-attachment' do
body = JSON.parse(request.body.read)
username = body['username']
email = body['email']
value = "Hello, my username is #{username} and email is {email}"
attachments = [
{
fallback: "New User",
color: "good",
fields: [{
title: "New User Signup",
value: value,
short: false
}]
}
]
channel = "channel_test"
message = "This is test message"
Slack.to_channel(channel, message, attachments) message
end

The attachments variable is an array of hashes containing the message:

  • Fallback is a required plain-text summary of the attachment.
  • Color is an optional value for the border along the left side of the message attachment. The values are can be good, warning, danger, any color, or any hex color code (eg. #4286f4)
  • Fields are an array of hashes that will be displayed in a table inside the message attachment.
  • Title will be shown as bold text.
  • Value is the message you want to send.
  • Short is an optional flag indicating whether the value is short enough to be displayed side by side with other values (in case you have more than one value).
Message with one attachment

How does it bring value to MuseFind

Our operation team has a complex day-to-day activities everyday ranging from finding and inviting influencers to campaigns to helping brands running successful campaigns. For example, the team spent hours in the past just to find which campaigns need more influencers and this manual work can be solved using the Incoming Webhook app. We have a cronjob that runs everyday to call a simple API that is acting as our webhook to tell which campaigns are in need of influencers. Now with the list generated in our dedicated Slack channel removes the manual work which enables them to concentrate on other task. We save thirty to sixty minutes by automating this! The message structure is clean and easy to read because the Slack attachments enable us to highlight each team member’s campaigns in one line with one green border (or other color as we like).

Slack attachments to list campaigns grouped by name

Final Words

That is how we send messages to Slack channel. You can use this lightweight app as a webhook to send messages to your Slack channel. The Incoming Webhook app can do more than what we did just now. Some cool features include button, pictures, links and more attachments. If you are interested more in exploring the app, please read the documentation.

About The Author

Reinhard Simeon is web developer at MuseFind. You can connect with him on Facebook, Twitter, or LinkedIn.

--

--