Skip to content

A simple and easy way to send notifications to Slack from your Ruby or Rails application.

License

Notifications You must be signed in to change notification settings

ButternutBox/slacked

 
 

Repository files navigation

Gem Version Circle CI

Slacked

This is a super simple Slack integration for Ruby and for Ruby and Rails applications. A use case for this would be to post a notification in Slack when a new User is created or a certain action has been taken in your application.

Are there other gems that provide similar functionality? Yes. Do some of them provide more flexibility? Yes. The point of this was to make installing and integrating a 30 second process.

This gem can be used with a rails application and enabled/disabled based on the environment config as you may see below

Getting Started

without rails

gem install slacked

With rails

Add this line to your application's Gemfile:

gem 'slacked'

And then execute:

$ bundle

Then run the installer:

$ bundle exec rails g slacked:install

This will create a .env file in the root of the rails application. Specify the Webhook Url and the message to be sent.

SLACK_WEBHOOK= "WEBHOOK_URL"
SLACK_DEFAULT_MESSAGE= "TEST"

Usage

Set the SLACK_WEBHOOK env variable with the value of the webhook which you want to send the messages. If you want to send a unique message in your application like 'Application is running' you can set the SLACK_DEFAULT_MESSAGE and call the message methods without sending an argument.

To send a sync message to slack use the method:

Slacked.post(message: "This is a test post")

or

Slacked.post

The last example will use the SLACK_DEFAULT_MESSAGE value

To send an async message to slack use the method:

Slacked.post_async(message: "This is a test post")

or

Slacked.post_async

The last example will use the SLACK_DEFAULT_MESSAGE value

To pass the config:

Slacked.post(
	message: "I have a message from the underworld!",
	config:  {icon_emoji: ':ghost:'}
)

or

Slacked.post_async(
	message: "Let's play fetch!",
	config:  {icon_emoji: ':dog:'}
)

Right now we only have the config for the icon, if you need another one let us know or submit a pull request.

To dynamically set the webhook

Slacked.post(
	message:     "Let's play fetch!",
	webhook_url: <WEBHOOK_URL>
)

or

Slacked.post_async(
	message:     "Let's play fetch!",
	webhook_url: <WEBHOOK_URL>
)

Example

class Post < ActiveRecord::Base
  after_create :slacked

  private

  def slacked
    Slacked.post(message: 'post created!')
  end
end

Using with rails

If you are using this gem inside a rails application you can enable or disable it based on your environment, to do it you only need to add the code below in your config file.

config.slacked_disabled = true

The default value is false

Testing

First install all dependencies:

bundle install

Then run the tests to confirm that all are passing:

rspec ./spec/

Contributors

License

The gem is available as open source under the terms of the MIT License.

About

A simple and easy way to send notifications to Slack from your Ruby or Rails application.

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Ruby 98.6%
  • Shell 1.4%