Class: SlackNotification

Inherits:
Notification show all
Defined in:
lib/app/models/slack_notification.rb

Overview

SlackNotification is a concrete class of Notification, sending notifications to the slack system. Initially this will be only used internally, however we could configure this to work with an account's slack configuration.

This object allows you to create an EmailNotification object, set it's property and tell it send_notification. It'll handle everything from there including

  1. Running in the background thread
  2. Error recovery
  3. Retry on failure

Usage:

SlackNotification.say 'Error getting data from resource'

or To change the channel it's posted to...

SlackNotification.say 'Error getting data from resource', to: 'another_channel'

or To who the message is from, the default is the environment name

SlackNotification.say 'Error getting data from resource', to: 'another_channel', from: 'someone else'

You are done! Go about your business of creating awesome software!

Constant Summary

Constants inherited from Notification

Notification::ALL_STATES, Notification::DELIVERY_EMAIL, Notification::DELIVERY_SLACK, Notification::DELIVERY_SMS, Notification::STATE_INVALID, Notification::STATE_NEW, Notification::STATE_PROCESSED, Notification::STATE_PROCESSING, Notification::STATE_RESUBMITTED, Notification::STATE_RETRYING, Notification::STATE_SUBMITTED, Notification::STATE_VIEWED

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Notification

#account_message_template, #default_message_template, #deletable?, #deliver_message, #finish_processing, #from_template, #message_from_haml_file, #message_from_haml_text, #message_from_liquid_text, #message_from_template, #render_liquid_text, #retry_delivery, #send_notification, #sendable?, #start_processing, #stringify_all, #successful?, #template_from_file, #viewed

Methods included from StandardModel

#clear_cache, included, #remove_blank_secure_fields, #secure_fields, #update, #update!

Methods included from App47Logger

#clean_params, #delete_parameter_keys, #log_controller_error, #log_debug, log_debug, log_error, #log_error, log_exception, #log_message, log_message, #log_warn, log_warn, #mask_parameter_keys, #update_flash_messages

Class Method Details

.say(message, to: nil, from: nil, template: nil) ⇒ Object

Convenience method to say something when we see something



76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/app/models/slack_notification.rb', line 76

def self.say(message, to: nil, from: nil, template: nil)
  return unless SystemConfiguration.slack_configured?

  notification = SlackNotification.new(to: to, from: from)
  notification.message = if template.present?
                           notification.message_from_template template, message
                         elsif message.is_a?(Array)
                           message.join("\n")
                         else
                           message
                         end
  notification.send_notification
end

Instance Method Details

#deliver_message! ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/app/models/slack_notification.rb', line 34

def deliver_message!
  return unless SystemConfiguration.slack_configured?

  start_processing
  payload = { text: message }
  payload[:channel] = if to.present?
                        to
                      elsif SystemConfiguration.slack_support_channel.present?
                        SystemConfiguration.slack_support_channel
                      else
                        'support'
                      end
  # Use the environment as the default, otherwise set it as the from
  payload[:username] = from.presence || Rails.env
  # Setup the delivery method for this message only.
  RestClient.post(SystemConfiguration.slack_api_url, payload.to_json)
  finish_processing
rescue StandardError => error
  App47Logger.log_debug '!!! Error sending SLACK notification !!!'
  App47Logger.log_debug error.message
  App47Logger.log_debug error.backtrace
  App47Logger.log_debug '!!! Error sending SLACK notification !!!'
  finish_processing error.message
end

#delivery_channel ⇒ Object

Default delivery channel is email, override for sms, SMTP or other channels



69
70
71
# File 'lib/app/models/slack_notification.rb', line 69

def delivery_channel
  DELIVERY_SLACK
end

#max_retries ⇒ Object

Limit the number of times the slack notification is retried



62
63
64
# File 'lib/app/models/slack_notification.rb', line 62

def max_retries
  1
end