2 Star 1 Fork 1

Gitee 组 / exception_notification

forked from Jim / exception_notification 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
slack.md 3.51 KB
一键复制 编辑 原始数据 按行查看 历史

Slack notifier

This notifier sends notifications to a slack channel using the slack-notifier gem.

Usage

Just add the slack-notifier gem to your Gemfile:

gem 'slack-notifier'

To configure it, you need to set at least the 'webhook_url' option, like this:

Rails.application.config.middleware.use ExceptionNotification::Rack,
                                        email: {
                                          email_prefix: '[PREFIX] ',
                                          sender_address: %{"notifier" <notifier@example.com>},
                                          exception_recipients: %w{exceptions@example.com}
                                        },
                                        slack: {
                                          webhook_url: '[Your webhook url]',
                                          channel: '#exceptions',
                                          additional_parameters: {
                                            icon_url: 'http://image.jpg',
                                            mrkdwn: true
                                          }
                                        }

The slack notification will include any data saved under env['exception_notifier.exception_data'].

An example of how to send the server name to Slack in Rails (put this code in application_controller.rb):

before_action :set_notification

def set_notification
  request.env['exception_notifier.exception_data'] = { 'server' => request.env['SERVER_NAME'] }
  # can be any key-value pairs
end

If you find this too verbose, you can determine to exclude certain information by doing the following:

Rails.application.config.middleware.use ExceptionNotification::Rack,
                                        slack: {
                                          webhook_url: '[Your webhook url]',
                                          channel: '#exceptions',
                                          additional_parameters: {
                                            icon_url: 'http://image.jpg',
                                            mrkdwn: true
                                          },
                                          ignore_data_if: lambda {|key, value|
                                            "#{key}" == 'key_to_ignore' || value.is_a?(ClassToBeIgnored)
                                          }
                                        }

Any evaluation to true will cause the key / value pair not be be sent along to Slack.

Options

webhook_url

String, required

The Incoming WebHook URL on slack.

channel

String, optional

Message will appear in this channel. Defaults to the channel you set as such on slack.

username

String, optional

Username of the bot. Defaults to the name you set as such on slack

custom_hook

String, optional

Custom hook name. See slack-notifier for more information. Default: 'incoming-webhook'

additional_parameters

Hash of strings, optional

Contains additional payload for a message (e.g avatar, attachments, etc). See slack-notifier for more information.. Default: '{}'

additional_fields

Array of Hashes, optional

Contains additional fields that will be added to the attachement. See Slack documentation.

Ruby
1
https://gitee.com/mayun-team/exception_notification.git
git@gitee.com:mayun-team/exception_notification.git
mayun-team
exception_notification
exception_notification
master

搜索帮助