A summary of data about the Ruby ecosystem.

https://github.com/slack-notifier/slack-notifier

A simple wrapper for posting to slack channels
https://github.com/slack-notifier/slack-notifier

Keywords

ruby slack

Keywords from Contributors

crash-reporting error-handling mvc activerecord activejob rubygems github-api octokit-rb rspec error-monitoring

Last synced: about 24 hours ago
JSON representation

Repository metadata

A simple wrapper for posting to slack channels

readme.md

A simple wrapper to send notifications to Slack webhooks.

Build Status
Code Climate
Gem Version
SemVer

Example

require 'slack-notifier'

notifier = Slack::Notifier.new "WEBHOOK_URL"
notifier.ping "Hello World"
# => if your webhook is setup, will message "Hello World"
# => to the default channel you set in slack

Installation

Install the latest stable release:

$ gem install slack-notifier

Or with Bundler, add it to your Gemfile:

gem "slack-notifier"

Setting Defaults

On initialization you can set default payloads by calling defaults in an initialization block:

notifier = Slack::Notifier.new "WEBHOOK_URL" do
  defaults channel: "#default",
           username: "notifier"
end

notifier.ping "Hello default"
# => will message "Hello default"
# => to the "#default" channel as 'notifier'

To get the WEBHOOK_URL you need:

  1. go to https://slack.com/apps/A0F7XDUAZ-incoming-webhooks
  2. choose your team, press configure
  3. in configurations press add configuration
  4. choose channel, press "Add Incoming WebHooks integration"

You can also set defaults through an options hash:

notifier = Slack::Notifier.new "WEBHOOK_URL", channel: "#default",
                                              username: "notifier"

These defaults are over-ridable for any individual ping.

notifier.ping "Hello random", channel: "#random"
# => will ping the "#random" channel

Links

Slack requires links to be formatted a certain way, so the default middlware stack of slack-notifier will look through your message and attempt to convert any html or markdown links to slack's format before posting.

Here's what it's doing under the covers:

message = "Hello world, [check](http://example.com) it <a href='http://example.com'>out</a>"
Slack::Notifier::Util::LinkFormatter.format(message)
# => "Hello world, <http://example.com|check> it <http://example.com|out>"

Formatting

Slack supports various different formatting options. For example, if you want to alert an entire channel you include <!channel> in your message

message = "<!channel> hey check this out"
notifier.ping message

#ends up posting "@channel hey check this out" in your Slack channel

You can see Slack's message documentation here

Escaping

Since sequences starting with < have special meaning in Slack, you should use Slack::Notifier::Util::Escape.html if your messages may contain &, < or >.

link_text = Slack::Notifier::Util::Escape.html("User <user@example.com>")
message = "Write to [#{link_text}](mailto:user@example.com)"
notifier.ping message

Blocks

This plugin supports the Slack blocks format and block kit builder. This is useful for displaying buttons, dropdowns, and images.

blocks = [
  {
    "type": "image",
    "title": {
      "type": "plain_text",
      "text": "image1",
      "emoji": true
    },
    "image_url": "https://api.slack.com/img/blocks/bkb_template_images/onboardingComplex.jpg",
    "alt_text": "image1"
  },
  {
    "type": "section",
    "text": {
      "type": "mrkdwn",
      "text": "Hey there šŸ‘‹ I'm TaskBot. I'm here to help you create and manage tasks in Slack.\nThere are two ways to quickly create tasks:"
    }
  }
]

notifier.post(blocks: blocks)

Additional parameters

Any key passed to the post method is posted to the webhook endpoint. Check out the Slack webhook documentation for the available parameters.

Setting an icon:

notifier.post text: "feeling spooky", icon_emoji: ":ghost:"
# or
notifier.post text: "feeling chimpy", icon_url: "http://static.mailchimp.com/web/favicon.png"

Adding attachments:

a_ok_note = {
  fallback: "Everything looks peachy",
  text: "Everything looks peachy",
  color: "good"
}
notifier.post text: "with an attachment", attachments: [a_ok_note]

HTTP options

With the default HTTP client, you can send along options to customize its behavior as :http_options params when you post or initialize the notifier.

notifier = Slack::Notifier.new 'WEBHOOK_URL', http_options: { open_timeout: 5 }
notifier.post text: "hello", http_options: { open_timeout: 10 }

Note: you should only send along options that Net::HTTP has as setters, otherwise the option will be ignored and show a warning.

Proxies

:http_options can be used if you need to connect to Slack via an HTTP proxy.
For example, to connect through a local squid proxy the following options would be used.

notifier = Slack::Notifier.new 'WEBHOOK_URL', http_options: {
                                                              proxy_address:  'localhost',
                                                              proxy_port:     3128,
                                                              proxy_from_env: false
                                                            }

Custom HTTP Client

There is a packaged default client wrapping Net::HTTP, but your HTTP needs might be a little different. In that case, you can pass in your own wrapper to handle sending the notifications. It just needs to respond to ::post with the arguments of the endpoint URI, and the payload pretty much the same as Net:HTTP.post_form.

A simple example:

module Client
  def self.post uri, params={}
    Net::HTTP.post_form uri, params
  end
end

notifier = Slack::Notifier.new 'WEBHOOK_URL' do
  http_client Client
end

It's also encouraged for any custom HTTP implementations to accept the :http_options key in params.

Setting client per post

You can also set the http_client per-post if you need to special case certain pings.

notifier.post text: "hello", http_client: CustomClient

Setting a No-Op client

In development (or testing), you may want to watch the behavior of the notifier without posting to slack. This can be handled with a no-op client.

class NoOpHTTPClient
  def self.post uri, params={}
    # bonus, you could log or observe posted params here
  end
end

notifier = Slack::Notifier.new 'WEBHOOK_URL' do
  http_client NoOpHTTPClient
end

Middleware

By default slack-notifier ships with middleware to format links in the message & text field of attachments. You can configure the middleware a notifier will use on initialization:

notifier = Slack::Notifier.new "WEBHOOK_URL" do
  middleware format_message: { formats: [:html] }
end
# this example will *only* use the format_message middleware and only format :html links

notifier.post text: "Hello <a href='http://example.com'>world</a>! [visit this](http://example.com)"
# => will post "Hello <http://example.com|world>! [visit this](http://example.com)"

The middleware can be set with a their name, or by name and options. They will be triggered in order.

notifier = Slack::Notifier.new "WEBHOOK_URL" do
  middleware :format_message, :format_attachments
end
# will run format_message then format_attachments with default options

notifier = Slack::Notifier.new "WEBHOOK_URL" do
  middleware format_message: { formats: [:html] },
             format_attachments: { formats: [:markdown] }
end
# will run format_message w/ formats [:html] then format_attachments with formats [:markdown]

Available middleware:

format_message

This middleware takes the :text key of the payload and runs it through the Linkformatter. You can configure which link formats to look for with a :formats option. You can set [:html] (only html links), [:markdown] (only markdown links) or [:html, :markdown] (the default, will format both).

format_attachments

This middleware takes the :text key of any attachment and runs it through the Linkformatter. You can configure which link formats to look for with a :formats option. You can set [:html] (only html links), [:markdown] (only markdown links) or [:html, :markdown] (the default, will format both).

at

This simplifies the process of notifying users and rooms to messages. By adding an :at key to the payload w/ an array of symbols the appropriately formatted commands will be prepended to the message. It will accept a single name, or an array.

For example:

notifier.post text: "hello", at: :casper
# => "<@casper> hello"

notifier.post text: "hello", at: [:here, :waldo]
# => "<!here> <@waldo> hello"

channels

If the channel argument of a payload is an array this splits the payload to be posted to each channel.

For example:

notifier.post text: "hello", channel: ["default", "all_the_things"]
# => will post "hello" to the default and all_the_things channel

To send a message directly to a user, their username no longer works. Instead you'll need to get the user's ID and set that as the channel.

At the time of writing, one way to get a user's ID is to:

  • go to their profile
  • click ... ("More actions")
  • click Copy Member ID

Writing your own Middleware

Middleware is fairly straightforward, it is any class that inherits from Slack::Notifier::PayloadMiddleware::Base and responds to #call. It will always be given the payload as a hash and should return the modified payload as a hash.

For example, lets say we want to replace words in every message, we could write a middleware like this:

class SwapWords < Slack::Notifier::PayloadMiddleware::Base
  middleware_name :swap_words # this is the key we use when setting
                              # the middleware stack for a notifier

  options pairs: ["hipchat" => "slack"] # the options takes a hash that will
                                        # serve as the default if not given any
                                        # when initialized

  def call payload={}
    return payload unless payload[:text] # noope if there is no message to work on

    # not efficient, but it's an example :)
    options[:pairs].each do |from, to|
      payload[:text] = payload[:text].gsub from, to
    end

    payload # always return the payload from your middleware
  end
end


notifier = Slack::Notifier.new "WEBHOOK_URL" do
  middleware :swap_words # setting our stack w/ just defaults
end
notifier.ping "hipchat is awesome!"
# => pings slack with "slack is awesome!"

notifier = Slack::Notifier.new "WEBHOOK_URL" do
  # here we set new options for the middleware
  middleware swap_words: { pairs: ["hipchat" => "slack",
                                   "awesome" => "really awesome"]}
end

notifier.ping "hipchat is awesome!"
# => pings slack with "slack is really awesome!"

If your middleware returns an array, that will split the message into multiple pings. An example for pinging multiple channels:

class MultiChannel < Slack::Notifier::PayloadMiddleware::Base
  middleware_name :channels

  def call payload={}
    return payload unless payload[:channel].respond_to?(:to_ary)

    payload[:channel].to_ary.map do |channel|
      pld = payload.dup
      pld[:channel] = channel
      pld
    end
  end
end

Versioning

Since version 1.0 has been released, the aim is to follow Semantic Versioning as much as possible. However, it is encouraged to check the changelog when updating to see what changes have been made.

To summarize the reasoning for versioning:

Given a version number MAJOR.MINOR.PATCH, increment:

- MAJOR version when incompatible API changes are made
- MINOR version for adding functionality in a backwards-compatible manner or bug fixes that *may* change behavior
- PATCH version for make backwards-compatible bug fixes

Testing

$ rspec

There is also an integration test setup to just double check pinging across the supported rubies. To run:

  1. Copy the .env-example file to .env and replace with your details.
  2. Make sure bin/test is executable
  3. then run and watch for the pings in your slack room
$ bin/test

Contributing

If there is any thing you'd like to contribute or fix, please:

  • Fork the repo
  • Add tests for any new functionality
  • Make your changes
  • Verify all new & existing tests pass
  • Make a pull request

License

The slack-notifier gem is distributed under the MIT License.


Owner metadata


GitHub Events

Total
Last Year

Committers metadata

Last synced: 3 days ago

Total Commits: 190
Total Committers: 29
Avg Commits per committer: 6.552
Development Distribution Score (DDS): 0.216

Commits in past year: 0
Committers in past year: 0
Avg Commits per committer in past year: 0.0
Development Distribution Score (DDS) in past year: 0.0

Name Email Commits
Steven Sloan s****n@g****m 149
Richard Lee d****y@g****m 5
Aurélien Noce a****e@i****r 4
Ben Lavender b****a@g****m 3
Rodion i6@b****u 2
Yuki Hattori y****i@s****p 2
kazuooooo m****7@g****m 2
Elektron1c97 E****7@g****m 2
Patrick Davey p****y@r****z 1
Alexander Fisher a****x@l****k 1
Bruce McIntyre b****e@g****m 1
Federico Ravasio r****o@g****m 1
Grey Baker g****l@g****m 1
Iulian Onofrei i****i@y****m 1
Julien Vanier j****r@g****m 1
Keith Pitty k****h@k****m 1
Leonard Bogdonoff l****t@g****m 1
Mark Quezada m****k@m****m 1
Masataka Kuwabara k****a@p****e 1
OSA Shunsuke h****g@g****m 1
Olivier Lacan hi@o****m 1
Olle Jonsson o****n@g****m 1
Patrick Veverka v****p 1
Phil Pirozhkov p****j 1
Raphael Kallensee r****e@g****e 1
Snow Helsing s****w 1
Thorben Starsy t****n@e****e 1
Waynn Lue W****s@g****m 1
Xavi Vila x****m@g****m 1

Committer domains:


Issue and Pull Request metadata

Last synced: 19 days ago

Total issues: 55
Total pull requests: 48
Average time to close issues: 3 months
Average time to close pull requests: about 2 months
Total issue authors: 53
Total pull request authors: 42
Average comments per issue: 3.67
Average comments per pull request: 1.73
Merged pull request: 24
Bot issues: 0
Bot pull requests: 0

Past year issues: 0
Past year pull requests: 2
Past year average time to close issues: N/A
Past year average time to close pull requests: about 6 hours
Past year issue authors: 0
Past year pull request authors: 1
Past year average comments per issue: 0
Past year average comments per pull request: 0.0
Past year merged pull request: 0
Past year bot issues: 0
Past year bot pull requests: 0

More stats: https://issues.ecosyste.ms/repositories/lookup?url=https://github.com/slack-notifier/slack-notifier

Top Issue Authors

  • stevenosloan (2)
  • llamicron (2)
  • rnhurt (1)
  • knagode (1)
  • Dirk23 (1)
  • zedtux (1)
  • nburt (1)
  • alonhartal (1)
  • kmuthupa (1)
  • fabsrc (1)
  • cimm (1)
  • NourEldinShobier (1)
  • Falkor (1)
  • anchit-desai (1)
  • morshedalam (1)

Top Pull Request Authors

  • HoneyEyesbot (4)
  • stevenosloan (4)
  • veverkap (2)
  • siegy22 (2)
  • pirj (1)
  • rjrobinson (1)
  • pedrofurtado (1)
  • greysteil (1)
  • skcc321 (1)
  • benhalpern (1)
  • revolter (1)
  • f-teruhisa (1)
  • lubekpl (1)
  • alexjfisher (1)
  • kouyaf77 (1)

Top Issue Labels

Top Pull Request Labels


Package metadata

ubuntu-24.04: ruby-slack-notifier

  • Homepage: https://github.com/slack-notifier/slack-notifier
  • Licenses:
  • Latest release: 1.5.1-3 (published 25 days ago)
  • Last Synced: 2026-02-06T16:04:21.883Z (25 days ago)
  • Versions: 1
  • Dependent Packages: 0
  • Dependent Repositories: 0
  • Rankings:
ubuntu-23.04: ruby-slack-notifier

  • Homepage: https://github.com/slack-notifier/slack-notifier
  • Licenses:
  • Latest release: 1.5.1-3 (published 20 days ago)
  • Last Synced: 2026-02-11T06:50:11.547Z (20 days ago)
  • Versions: 1
  • Dependent Packages: 0
  • Dependent Repositories: 0
  • Rankings:
    • Dependent repos count: 0.0%
    • Dependent packages count: 0.0%
    • Average: 100%
ubuntu-23.10: ruby-slack-notifier

  • Homepage: https://github.com/slack-notifier/slack-notifier
  • Licenses:
  • Latest release: 1.5.1-3 (published 18 days ago)
  • Last Synced: 2026-02-13T18:33:05.943Z (18 days ago)
  • Versions: 1
  • Dependent Packages: 0
  • Dependent Repositories: 0
  • Rankings:
    • Dependent repos count: 0.0%
    • Dependent packages count: 0.0%
    • Average: 100%
ubuntu-24.10: ruby-slack-notifier

  • Homepage: https://github.com/slack-notifier/slack-notifier
  • Licenses:
  • Latest release: 1.5.1-3 (published 22 days ago)
  • Last Synced: 2026-02-09T17:18:38.355Z (22 days ago)
  • Versions: 1
  • Dependent Packages: 0
  • Dependent Repositories: 0
  • Rankings:
debian-12: ruby-slack-notifier

  • Homepage: https://github.com/slack-notifier/slack-notifier
  • Documentation: https://packages.debian.org/bookworm/ruby-slack-notifier
  • Licenses:
  • Latest release: 1.5.1-3 (published 19 days ago)
  • Last Synced: 2026-02-12T23:42:00.133Z (19 days ago)
  • Versions: 1
  • Dependent Packages: 0
  • Dependent Repositories: 0
  • Rankings:
debian-13: ruby-slack-notifier

  • Homepage: https://github.com/slack-notifier/slack-notifier
  • Documentation: https://packages.debian.org/trixie/ruby-slack-notifier
  • Licenses:
  • Latest release: 2.4.0-1 (published 19 days ago)
  • Last Synced: 2026-02-13T13:19:58.853Z (18 days ago)
  • Versions: 1
  • Dependent Packages: 0
  • Dependent Repositories: 0
  • Rankings:
    • Dependent repos count: 0.0%
    • Dependent packages count: 0.0%
    • Average: 100%

Dependencies

Gemfile rubygems
  • benchmark-ips >= 0 development
  • pry-byebug >= 0 development
  • pry-debugger >= 0 development
  • rake ~> 12.0 development
  • rspec ~> 3.5.0 development
slack-notifier.gemspec rubygems

Score: -Infinity