Module: Slackert::Templates

Defined in:
lib/slackert/templates.rb

Overview

Pre-defined Slack message templates that you can use for a quick message layout instead of building one from scratch. Most of the templates are customizable and you can include or exclude fields like title, description or extra data that will appear in the message.

Example Usage

message = Slackert::Templates.job_start(
  title: 'Job Title',
  desc: 'This job does this and that',
  overview: {
    'Job Type': 'Refresh',
    'Action': 'Update',
    'Start Time': Time.now.strftime("%Y/%m/%d %H:%M:%S")
  }
)

alerts = Slackert::Alerter.new('mywebhook')
alerts.info(message)

Class Method Summary collapse

Class Method Details

.job_error(title:, error:, notify_user_ids: [], extra: {}, add_alert_emoji: true) ⇒ Hash

Message on job error that notifies specified users by tagging them in the alert.

Parameters:

  • title (String)

    title of the job/alert, plain text only

  • error (String)

    error message

  • notify_user_ids (defaults to: [])

    Array<String> Slack member IDs that will be tagged and notified

  • extra (Hash) (defaults to: {})

    additional section of key: value fields, both accept markdown and keys are bold by default

  • add_alert_emoji (Boolean) (defaults to: true)

    adds rotating light emoji in the title

Returns:

  • (Hash)

    Slack message



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/slackert/templates.rb', line 121

def self.job_error(
  title:,
  error:,
  notify_user_ids: [],
  extra: {},
  add_alert_emoji: true
)
  MessageBuilder.build do |alert|
    header = "Error while processing #{title}"
    header = add_alert_emoji ? ":rotating_light: #{header}" : header

    alert.add_header(header)
    alert.notify_users(notify_user_ids) unless notify_user_ids.empty?
    alert.add_divider
    alert.add_markdown_text('*Result*: Fail')
    alert.add_markdown_text("*Error Output*:\n```#{error}```")
    add_section_from_hash(alert, extra) unless extra.empty?
  end
end

.job_executed(title: '', desc: '', result: '', overview: {}, stats: {}) ⇒ Hash

Combines both job start and job finish into a single message. Best for quick jobs where a separate start and finish alerts are unnecessary.

Parameters:

  • title (String) (defaults to: '')

    title of the message, plain text only

  • desc (String) (defaults to: '')

    description of the message, accepts markdown

  • result (String) (defaults to: '')

    result of the job, accepts markdown

  • overview (Hash) (defaults to: {})

    a section of key: value fields, both keys and values accept markdown and keys are bold by default

  • stats (Hash) (defaults to: {})

    a separate section of key: value fields, both keys and values accept markdown and keys are bold by default

Returns:

  • (Hash)

    Slack message



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/slackert/templates.rb', line 95

def self.job_executed(
  title: '',
  desc: '',
  result: '',
  overview: {},
  stats: {}
)
  MessageBuilder.build do |alert|
    alert.add_header(title) unless title.empty?
    alert.add_plain_text(desc) unless desc.empty?
    alert.add_markdown_text("*Result*: #{result}") unless result.empty?
    add_section_from_hash(alert, overview) unless overview.empty?
    add_section_from_hash(alert, stats) unless stats.empty?
  end
end

.job_finish(title: '', desc: '', result: '', stats: {}) ⇒ Hash

Message layout to notify of a job finish.

Parameters:

  • title (String) (defaults to: '')

    title of the message, plain text only

  • desc (String) (defaults to: '')

    description of the message, accepts markdown

  • result (String) (defaults to: '')

    result of the job, accepts markdown

  • stats (Hash) (defaults to: {})

    extra execution key: value section fields to be included in the message, both keys and values accept markdown and keys are bold by default

Returns:

  • (Hash)

    Slack message



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/slackert/templates.rb', line 69

def self.job_finish(
  title: '',
  desc: '',
  result: '',
  stats: {}
)
  MessageBuilder.build do |alert|
    alert.add_header(title)
    alert.add_plain_text(desc)
    alert.add_markdown_text("*Result*: #{result}")
    add_section_from_hash(alert, stats) unless stats.empty?
  end
end

.job_start(title: '', desc: '', overview: {}) ⇒ Hash

Message layout to notify of a job start.

Parameters:

  • title (String) (defaults to: '')

    title of the message, plain text only

  • desc (String) (defaults to: '')

    description of the message, accepts markdown

  • overview (Hash) (defaults to: {})

    extra identifiable key: value section fields to be included in the message, both keys and values accept markdown and keys are bold by default

Returns:

  • (Hash)

    Slack message



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/slackert/templates.rb', line 48

def self.job_start(
  title: '',
  desc: '',
  overview: {}
)
  MessageBuilder.build do |alert|
    alert.add_header(title) unless title.empty?
    alert.add_plain_text(desc) unless desc.empty?
    add_section_from_hash(alert, overview) unless overview.empty?
  end
end

.notification(text:, title: '') ⇒ Hash

Message layout for a quick notification.

Parameters:

  • title (String) (defaults to: '')

    optional title

  • text (String)

    notification text, accepts markdown

Returns:

  • (Hash)

    Slack message



33
34
35
36
37
38
# File 'lib/slackert/templates.rb', line 33

def self.notification(text:, title: '')
  MessageBuilder.build do |alert|
    alert.add_header(title) unless title.empty?
    alert.add_markdown_text(text)
  end
end