Skip to content

Instantly share code, notes, and snippets.

@fsubal
Last active December 13, 2024 15:12
Show Gist options
  • Save fsubal/3387f76bc4099634e22993d0823c629b to your computer and use it in GitHub Desktop.
Save fsubal/3387f76bc4099634e22993d0823c629b to your computer and use it in GitHub Desktop.
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