Module: ImmosquareSlack::Channel
- Extended by:
- SharedMethods
- Defined in:
- lib/immosquare-slack/channel.rb
Constant Summary collapse
- GENERAL_CHANNEL =
"general".freeze
Class Method Summary collapse
-
.list_channels ⇒ Object
## Fetches the list of channels (public + private, including archived).
-
.post_message(text, channel_name: nil, notify: nil, notify_text: nil, bot_name: nil, notify_general_if_invalid_channel: true) ⇒ Object
## Posts a message to a channel.
Class Method Details
.list_channels ⇒ Object
##
Fetches the list of channels (public + private, including archived). Memoized per process — call sites that need a fresh list should reset @list_channels explicitly.
##
14 15 16 17 18 19 20 21 22 |
# File 'lib/immosquare-slack/channel.rb', line 14 def list_channels @list_channels ||= begin extra_params = { :types => ["public_channel", "private_channel"].join(","), :exclude_archived => false } fetch_paginated_data("https://slack.com/api/conversations.list", "channels", extra_params) end end |
.post_message(text, channel_name: nil, notify: nil, notify_text: nil, bot_name: nil, notify_general_if_invalid_channel: true) ⇒ Object
##
Posts a message to a channel.
‘channel_name` and `bot_name` are optional keyword args: if nil, they fall back to the global config `ImmosquareSlack.configuration.default_channel` and `default_bot_name`. Lets apps that always notify the same channel avoid repeating the value at every call site.
If no channel can be resolved after fallback, raises an ArgumentError immediately (clearer than the cryptic error returned by the Slack API).
##
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/immosquare-slack/channel.rb', line 37 def (text, channel_name: nil, notify: nil, notify_text: nil, bot_name: nil, notify_general_if_invalid_channel: true) channel_name ||= ImmosquareSlack.configuration.default_channel bot_name ||= ImmosquareSlack.configuration.default_bot_name raise(ArgumentError, "channel_name is required (or set ImmosquareSlack.configuration.default_channel)") if channel_name.nil? channel_id = get_channel_id_by_name(channel_name) if channel_id.nil? text = "immosquare-slack missing channel *#{channel_name}*\nmessage:\n#{text}" return (text, :channel_name => GENERAL_CHANNEL, :notify => :channel, :notify_text => "", :bot_name => bot_name, :notify_general_if_invalid_channel => false) if channel_name != GENERAL_CHANNEL && notify_general_if_invalid_channel raise("channel '#{channel_name}' not found on slack") end url = "https://slack.com/api/chat.postMessage" notification_text = notify ? build_notification_text(channel_id, notify, *notify_text) : nil text = "#{notification_text}#{text}" body = { :channel => channel_id, :text => text } body[:username] = bot_name if bot_name make_slack_api_call(url, :method => :post, :body => body) end |