Skip to main content Link Search Menu Expand Document (external link)

Slack Notification

This article will show you how to include a Slack notification feature into our program.

Slack notification are useful in notifying any unwanted exceptions and important alerts.

Table of contents

  1. Slack Notifier
  2. How to install slack-notifier
  3. Usage

Slack Notifier

The slack-notifier gem is a simple wrapper for sending notifications to Slack webhooks.

How to install slack-notifier

Reminder:

Make sure that your containers are up and running.

In your gemfile, add gem 'slack-notifier'.

gem 'slack-notifier'

Then run bundle install.

 root@0122:/usr/src/app# bundle install
Fetching gem metadata from https://rubygems.org/..........
Resolving dependencies...
...
Installing slack-notifier 2.4.0
Bundle complete! 27 Gemfile dependencies, 101 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.

Usage

You may establish default payloads during setup by calling defaults in an initialization block.

Let’s create an OrderSlackNotify job.

 root@0122:/usr/src/app# rails g job OrderSlackNotify
      create  app/jobs/order_slack_notify_job.rb
# app/jobs/order_slack_notify_job.rb

class OrderSlackNotifyJob < ApplicationJob
  queue_as :default

  def perform(*args)
    notifier = Slack::Notifier.new 'WEBHOOK_URL' do
      defaults channel: '#default',
               username: 'notifier'
    end

    notifier.ping 'Hello default'
  end
end

Follow these procedures to obtain the WEBHOOK_URL.

  1. Go to https://slack.com/apps/A0F7XDUAZ-incoming-webhooks.
  2. After logging in to your team, select “Add Configuration” from the left-side panel.
  3. Choose the Slack channel where you wish to be notified, then click “Add Incoming WebHooks integration”. For this example, we will use channel #order-notification.

Set the webhook url and channel to our OrderSlackNotifyJob.

# app/jobs/order_slack_notify_job.rb

class OrderSlackNotifyJob < ApplicationJob
  queue_as :default

  def perform(*args)
-   notifier = Slack::Notifier.new 'WEBHOOK_URL' do
-     defaults channel: '#default',
+   notifier = Slack::Notifier.new 'https://hooks.slack.com/services/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' do
+     defaults channel: '#order-notification',
               username: 'notifier'
    end

    notifier.ping 'Hello default'
  end
end

Then try it out by calling the job.

irb(main):001:0> OrderSlackNotifyJob.perform_now
Performing OrderSlackNotifyJob (Job ID: 126003ec-fad4-403d-b43b-4d74cb5b9911) from Sidekiq(default) enqueued at 
Performed OrderSlackNotifyJob (Job ID: 126003ec-fad4-403d-b43b-4d74cb5b9911) from Sidekiq(default) in 565.51ms
=> [#<Net::HTTPOK 200 OK readbody=true>]

An options hash can also be used to set defaults.

# app/jobs/order_slack_notify_job.rb

class OrderSlackNotifyJob < ApplicationJob
  queue_as :default

  def perform(*args)
-   notifier = Slack::Notifier.new 'https://hooks.slack.com/services/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' do
-     defaults channel: '#order-notification',
-              username: 'notifier'
-   end
+   notifier = Slack::Notifier.new 'https://hooks.slack.com/services/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',
+                                  channel: '#order-notification',
+                                  username: 'notifier'

    notifier.ping 'Hello default'
  end
end

Now that we know the basics, let’s try implementing it to our top-up.

# app/controllers/top_ups_controller.rb

class TopUpsController < ApplicationController
   # ...
  def create
    @order = Order.new
    @order.amount = params[:order][:amount]
    @order.user = current_user
    @order.save
+   OrderSlackNotifyJob.perform_later(@order.id)
  end
end

Modify the notification message.

# app/jobs/order_slack_notify_job.rb

class OrderSlackNotifyJob < ApplicationJob
   queue_as :default

-  def perform(*args)
-   notifier = Slack::Notifier.new 'https://hooks.slack.com/services/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',
-                                  channel: '#order-notification',
-                                  username: 'notifier'
-
-   notifier.ping 'Hello default'
+  def perform(order_id)
+   order = Order.find order_id
+   notifier = Slack::Notifier.new 'https://hooks.slack.com/services/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'
+   notifier.ping "user: #{order.user&.email}, place order amount #{order.amount}"  
   end
end

Now every time a user top-up, it will notify us who made an order and its amount.

Now we successfully implemented slack-notifier in our application. More information may be found at slack-notifier.


Back to top

Copyright © 2020-2022 Secure Smarter Service, Inc. This site is powered by KodaCamp.