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
= 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()
Class Method Summary collapse
-
.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.
-
.job_executed(title: '', desc: '', result: '', overview: {}, stats: {}) ⇒ Hash
Combines both job start and job finish into a single message.
-
.job_finish(title: '', desc: '', result: '', stats: {}) ⇒ Hash
Message layout to notify of a job finish.
-
.job_start(title: '', desc: '', overview: {}) ⇒ Hash
Message layout to notify of a job start.
-
.notification(text:, title: '') ⇒ Hash
Message layout for a quick notification.
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.
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.
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.
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.
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.
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 |