Module: ImmosquareSlack::Channel

Extended by:
SharedMethods
Defined in:
lib/immosquare-slack/channel.rb

Constant Summary collapse

GENERAL_CHANNEL =
"general".freeze

Class Method Summary collapse

Class Method Details

.list_channels(force: false) ⇒ Object

##

Fetches the list of channels (public + private, including archived), memoized per process for the lifetime of that process (a Puma worker or Sidekiq runs for hours/days).

Piège : le cache ne reflète que l’état du workspace au premier appel. Un channel créé ou renommé ensuite n’y est pas. ‘force: true` vide le cache et refetch — à utiliser sur un miss de lookup pour distinguer un channel réellement absent d’un cache périmé avant tout fallback.

##


20
21
22
23
24
25
26
27
28
29
# File 'lib/immosquare-slack/channel.rb', line 20

def list_channels(force: false)
  @list_channels = nil if force
  @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).

##

Raises:

  • (ArgumentError)


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/immosquare-slack/channel.rb', line 44

def post_message(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?

  ##============================================================##
  ## A miss can mean the channel really doesn't exist, or that
  ## the memoized list predates its creation. Retry once on a
  ## freshly fetched list before declaring it missing.
  ##============================================================##
  channel_id = get_channel_id_by_name(channel_name)
  channel_id = get_channel_id_by_name(channel_name, :force => true) if channel_id.nil?

  if channel_id.nil?
    text = "immosquare-slack missing channel *#{channel_name}*\nmessage:\n#{text}"
    return post_message(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