Class: Notification::Slack
- Inherits:
-
Object
- Object
- Notification::Slack
- Defined in:
- lib/capistrano/ops/rails/lib/notification/slack.rb
Instance Method Summary collapse
- #backup_notification(result, title, content, notification_level) ⇒ Object
-
#initialize ⇒ Slack
constructor
A new instance of Slack.
- #notify(message) ⇒ Object
Constructor Details
#initialize ⇒ Slack
Returns a new instance of Slack.
8 9 10 11 12 13 14 15 |
# File 'lib/capistrano/ops/rails/lib/notification/slack.rb', line 8 def initialize @slack_secret = ENV['SLACK_SECRET'] @slack_channel = ENV['SLACK_CHANNEL'] @conn = Faraday.new(url: 'https://slack.com/api/') do |faraday| faraday.headers['Content-Type'] = 'application/json' faraday.headers['Authorization'] = "Bearer #{@slack_secret}" end end |
Instance Method Details
#backup_notification(result, title, content, notification_level) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/capistrano/ops/rails/lib/notification/slack.rb', line 36 def backup_notification(result, title, content, notification_level) return if @slack_secret.nil? || @slack_channel.nil? return if notification_level == 'error' && result begin res = @conn.post('chat.postMessage') do |req| req.body = { channel: @slack_channel, blocks: [ { type: 'header', text: { type: 'plain_text', text: title || "#{Rails.env} Message", emoji: true } }, { type: 'section', text: { type: 'mrkdwn', text: content || 'No content' } } ] }.to_json end response = JSON.parse(res.body) raise Notification::Error, response['error'] if response['ok'] == false response rescue Notification::Error => e puts "Slack error: \n\t#{e.}" end end |
#notify(message) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/capistrano/ops/rails/lib/notification/slack.rb', line 17 def notify() return if @slack_secret.nil? || @slack_channel.nil? begin res = @conn.post('chat.postMessage') do |req| req.body = { "channel": @slack_channel, "text": }.to_json end response = JSON.parse(res.body) raise Notification::Error, response['error'] if response['ok'] == false response rescue Notification::Error => e puts "Slack error: \n\t#{e.}" end end |