Rails Slack Integration with Slack Notifier gem
In this article we will learn how to integrate slack with Rails applications using slack-notifier. Slack is a messaging app for teams. Slack comes with integration services with tools such as Github, Airbrake, Jenkins, Bitbucket and there are many. You can visit Slack services page for full details.
Slack Notifier in Action
There is a gem namely Slack Notifier which helps to send notifications to Slack webhooks. In this article, we will demonstrate how to create a slack channel and how to send notifications to slack channels using webhooks in Ruby.
1. Sign up on Slack
Create your slack account at Sign Up page of Slack.
2. Incoming Webhooks
In this step, we will configure incoming webhook URL in Slack. The webhook URL can be used to send notifications from Ruby application using the slack-notifier gem.
1) Create Incoming Webhooks for channels
Once you have a slack account, you need to configure Incoming Webhooks service to be able to send push notifications to a particular channel.
You can find an option to enable incoming webhooks at Services Page. This page lists many other tools which you can integrate slack with.
2) Select Channel
In this step select a channel to create incoming webhooks for as shown in pictures below
Here I have selected general channel to create incoming webhooks.
After selecting channel click on Add Incoming Webhooks Integration
3) Webhook URL
After step 3, you will be given unique slack URL to send notifications. The URL would look something like - Webhook URL
Note: This is real Webhook URL to post to my general channel, use it wisely if you are going to
3. Testing with slack-notifier gem
1) Install slack-notifier gem by command
2) Test sample send notification
After performing above code steps, we can see the “Hello World from AppName” notification in slack channel as given in the image below
3) Send by User
Slack notifier provides a way to send notification as a user, you need to set username attribute for notifier object.
Code:
This will send a notification to channel specified by user akshay
4) Send to Channel
Slack notifier provides a way to send a notification to a different channel by setting channel attribute for the notifier object.
Code:
This will send a notification #random channel your slack.
5) Adding links to message
You can add links to the message you sent to a slack channel as given below -
6) Setting Icon
I would suggest creating an instance of slack notifier as a Singleton pattern. As, we don’t need to initiate the object again and again.
If you want to send a notification to some other channel then you can select a different channel while creating incoming webhook in step number 2.
6) Formatting your message
Bold Text in Slack Notification
Strikethrough Text in Slack Notification
List in Slack Notification
Blockquotes in Slack Notification
Code Blocks in Slack Notification Raw
4. Exception Notifier in Rails Slack Integration
To be able to send notifications to slack channel from Rails applications - you would need to add gem slack-notifier to Gemfile of your Rails application
Add gems (exception_notification, slack-notifier) to Gemfile.
If you’re already using the exception_notification gem, then you just need to add slack-notifier gem.
Bundle Install
Run bundle install command in Rails application directory so that slack-notifier is used for your Rails application scope
Create channel #exception
Create a new channel into your slack account and give it a name #exceptions
Configure
Configure slack webhook url in your application.rb or environment specific file (development.rb/staging.rb/production.rb)
Basically, the exception notifier gem configuration is extended to setup slack notifications as well by providing webhook URL as well.
You’re done!
Whenever exceptions are raised from your application you will get email notifications because of exception notifier and you will get exceptions in your preferred slack channel because of slack-notifier gem
Resources
Here are few of resources which may for API integrations.
- Slack API - https://api.slack.com
- Slack - Incoming WebHooks
Let us know through comments if you face any difficulty in integration of slack or in any of the process mentioned above. Thanks for Reading!
Subscribe to Ruby in Rails
Get the latest posts delivered right to your inbox