Last active
December 13, 2024 15:12
-
-
Save fsubal/3387f76bc4099634e22993d0823c629b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SlackNotification | |
class UnknownSlackChannel < StandardError; end | |
WEBHOOK_URLS = Rails.configuration.x.slack_webhook_urls | |
def initialize(webhook_url:) | |
@notifier = Slack::Notifier.new(webhook_url) | |
end | |
def self.for_channel(channel_name) | |
key = channel_name.to_s.remove_prefix('#').to_sym | |
webhook_url = WEBHOOK_URLS.fetch(key) | |
new(webhook_url:) | |
rescue KeyError => e | |
raise UnknownSlackChannel, e | |
end | |
def send_text!(message, **options) | |
unless message.is_a? String | |
raise ArgumentError, message | |
end | |
@notifier.ping(message, link_names: true, **options) | |
end | |
def send_blocks!(*blocks) | |
@notifier.post(blocks: blocks.map(&:to_h)) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment