immosquare-slack 0.1.5 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b4eec84f4f9ca3ea1bb5fdbe1f5534fcc1b78ac8d0ba359079615fbbca500ae0
4
- data.tar.gz: 20e5313fa30c0ec42c2cbed9d40b0a96d674986d324096a4e8b92f8aed7cb0f3
3
+ metadata.gz: 622166a109e7d56741e24a45e19fb139940e467aaa93713fdbf9ee57800f8d00
4
+ data.tar.gz: a7603ceee1e8467dd39b48441b201a5fea6938a3a5a3308bf7855066cdce8059
5
5
  SHA512:
6
- metadata.gz: 703bd12a7ec32728c1dee2e67361925931d15826c0e560546b8006e8f3cd54c1fbd776f3350c3c2c59acadccd2bf880f3279c78ebad0a77df860813b1a1ae5d9
7
- data.tar.gz: 380f30822739cc1147d845be7795ea6596d6e53deec54c6dc7f88c64a5ff000296b7fd4c6a83c8c1eeaa650fe62cf166a7d961ceb63217913e8153f56f8f7858
6
+ metadata.gz: cd8138d1bca5e66c75777ccdee7752c8530fa38ae5aa0e8c737679a25994927274fe6eb87c5840ca936c77d5a562c5d74836f86b665e233a6a66b50fd901e495
7
+ data.tar.gz: 3db28b76da6504ef0b8ee7f99309342b860071430ec32843fc886063cf59ffaa117fe4fedfffc252c010a62607a40580dd4f4688784fecb120973155cc790120
@@ -1,21 +1,53 @@
1
1
  module ImmosquareSlack
2
2
  module Channel
3
3
  extend SharedMethods
4
+
5
+ GENERAL_CHANNEL = "general".freeze
6
+
4
7
  class << self
5
8
 
6
9
  ##============================================================##
7
- ## Pour récupérer la liste des channels
10
+ ## Fetches the list of channels (public + private, including
11
+ ## archived). Memoized per process — call sites that need a
12
+ ## fresh list should reset @list_channels explicitly.
8
13
  ##============================================================##
9
14
  def list_channels
10
- fetch_paginated_data("https://slack.com/api/conversations.list", "channels")
15
+ @list_channels ||= begin
16
+ extra_params = {
17
+ :types => ["public_channel", "private_channel"].join(","),
18
+ :exclude_archived => false
19
+ }
20
+ fetch_paginated_data("https://slack.com/api/conversations.list", "channels", extra_params)
21
+ end
11
22
  end
12
23
 
13
24
  ##============================================================##
14
- ## Pour poster un message dans un channel
25
+ ## Posts a message to a channel.
26
+ ##
27
+ ## `channel_name` and `bot_name` are optional keyword args:
28
+ ## if nil, they fall back to the global config
29
+ ## `ImmosquareSlack.configuration.default_channel` and
30
+ ## `default_bot_name`. Lets apps that always notify the same
31
+ ## channel avoid repeating the value at every call site.
32
+ ##
33
+ ## If no channel can be resolved after fallback, raises an
34
+ ## ArgumentError immediately (clearer than the cryptic error
35
+ ## returned by the Slack API).
15
36
  ##============================================================##
16
- def post_message(channel_name, text, notify: nil, notify_text: nil, bot_name: nil)
37
+ def post_message(text, channel_name: nil, notify: nil, notify_text: nil, bot_name: nil, notify_general_if_invalid_channel: true)
38
+ channel_name ||= ImmosquareSlack.configuration.default_channel
39
+ bot_name ||= ImmosquareSlack.configuration.default_bot_name
40
+
41
+ raise(ArgumentError, "channel_name is required (or set ImmosquareSlack.configuration.default_channel)") if channel_name.nil?
42
+
17
43
  channel_id = get_channel_id_by_name(channel_name)
18
- raise("Channel #{channel_name} not found on slack") if channel_id.nil?
44
+
45
+ if channel_id.nil?
46
+ text = "immosquare-slack missing channel *#{channel_name}*\nmessage:\n#{text}"
47
+ 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
48
+
49
+ raise("channel '#{channel_name}' not found on slack")
50
+ end
19
51
 
20
52
  url = "https://slack.com/api/chat.postMessage"
21
53
  notification_text = notify ? build_notification_text(channel_id, notify, *notify_text) : nil
@@ -31,32 +63,41 @@ module ImmosquareSlack
31
63
  make_slack_api_call(url, :method => :post, :body => body)
32
64
  end
33
65
 
34
-
35
-
36
66
  private
37
67
 
38
68
  ##============================================================##
39
- ## Pour récupérer l'id d'un channel en fonction de son nom
69
+ ## Resolves a channel id from its name. "general" is matched
70
+ ## via the `is_general` flag because workspaces can rename
71
+ ## their general channel.
40
72
  ##============================================================##
41
73
  def get_channel_id_by_name(channel_name)
42
74
  channels = list_channels
43
- channel = channels.find {|c| c["name"] == channel_name }
44
- channel ? channel["id"] : nil
75
+ channel = channels.find do |c|
76
+ if channel_name == GENERAL_CHANNEL
77
+ c["is_general"]
78
+ else
79
+ c["name"] == channel_name && c["is_archived"] == false
80
+ end
81
+ end
82
+ channel.nil? ? nil : channel["id"]
45
83
  end
46
84
 
47
85
  ##============================================================##
48
- ## Pour récupérer la liste des membres d'un channel
86
+ ## Fetches the list of members of a channel.
49
87
  ##============================================================##
50
88
  def get_channel_members(channel_id)
51
89
  fetch_paginated_data("https://slack.com/api/conversations.members", "members", {:channel => channel_id})
52
90
  end
53
91
 
54
92
  ##============================================================##
55
- ## Méthode récupérant les membres d'un channel et les notifier
56
- ## sur le message
57
- ## on ne peut pas utilser in? si on veut que la gem soit
58
- ## compatible avec ruby (sans rails)
59
- ## pareil pour present?
93
+ ## Builds the notification prefix prepended to the message
94
+ ## body. `notify` may be:
95
+ ## - an Array of emails: mentions matching workspace members
96
+ ## - :all : mentions every member of the channel
97
+ ## - :channel/:here/:everyone : Slack-wide broadcast tokens
98
+ ##
99
+ ## Note: avoids `in?` and `present?` so the gem stays usable
100
+ ## from plain Ruby (no Rails / ActiveSupport required).
60
101
  ##============================================================##
61
102
  def build_notification_text(channel_id, notify, text = "Hello")
62
103
  final =
@@ -75,8 +116,9 @@ module ImmosquareSlack
75
116
  "<!everyone>"
76
117
  end
77
118
 
119
+ return nil if final.to_s.empty?
78
120
 
79
- "#{text} #{final}\n" if !final.to_s.empty?
121
+ text.empty? ? "#{final}\n" : "#{text} #{final}\n"
80
122
  end
81
123
 
82
124
  end
@@ -1,10 +1,12 @@
1
1
  module ImmosquareSlack
2
2
  class Configuration
3
3
 
4
- attr_accessor :slack_api_token_bot, :openai_model
4
+ attr_accessor :slack_api_token_bot, :default_channel, :default_bot_name
5
5
 
6
6
  def initialize
7
7
  @slack_api_token_bot = nil
8
+ @default_channel = nil
9
+ @default_bot_name = nil
8
10
  end
9
11
 
10
12
  end
@@ -6,28 +6,27 @@ module ImmosquareSlack
6
6
  private
7
7
 
8
8
  ##============================================================##
9
- ## On récupère tous les résultats avec une loop sur le cursor
9
+ ## Fetches all results by looping on the Slack API cursor.
10
10
  ##============================================================##
11
11
  def fetch_paginated_data(url, data_key, extra_query = {})
12
12
  items = []
13
13
  cursor = nil
14
14
 
15
15
  loop do
16
- query = cursor ? {:cursor => cursor}.merge(extra_query) : extra_query
16
+ query = cursor ? {:cursor => cursor}.merge(extra_query) : extra_query
17
17
  response = make_slack_api_call(url, :query => query)
18
-
18
+ cursor = response.dig("response_metadata", "next_cursor")
19
19
 
20
20
  items.concat(response[data_key])
21
- cursor = response.dig("response_metadata", "next_cursor")
22
21
  break if cursor.nil? || cursor.empty?
23
22
  end
24
23
  items
25
24
  end
26
25
 
27
26
  ##============================================================##
28
- ## On fait le call à l'API Slack. On raise le message entier
29
- ## en string si la réponse n'est pas "ok" ou si la réponse
30
- ## n'est pas un JSON valide.
27
+ ## Calls the Slack API. Raises the full response body as a
28
+ ## string if the response is not "ok" or if the body is not
29
+ ## valid JSON.
31
30
  ##============================================================##
32
31
  def make_slack_api_call(url, method: :get, query: {}, body: nil)
33
32
  options = {
@@ -38,17 +37,17 @@ module ImmosquareSlack
38
37
  }
39
38
 
40
39
  ##============================================================##
41
- ## On crée les options en fonction du cas de figure
40
+ ## Builds the request options based on the call type.
42
41
  ##============================================================##
43
- options[:query] = query if query.any?
42
+ options[:query] = query if query.any?
44
43
  options[:body] = body.to_json if body
45
44
 
46
45
  ##============================================================##
47
- ## On send la requête et on parse la réponse
46
+ ## Sends the request and parses the response.
48
47
  ##============================================================##
49
- response = HTTParty.send(method, url, options)
48
+ response = HTTParty.public_send(method, url, options)
50
49
  parsed_response = JSON.parse(response.body)
51
- raise(parsed_response.to_json) unless parsed_response["ok"]
50
+ raise(parsed_response.to_json) if !parsed_response["ok"]
52
51
 
53
52
  parsed_response
54
53
  rescue JSON::ParserError
@@ -1,3 +1,3 @@
1
1
  module ImmosquareSlack
2
- VERSION = "0.1.5".freeze
2
+ VERSION = "0.2.0".freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: immosquare-slack
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - immosquare
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2024-12-11 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: httparty
@@ -45,7 +44,6 @@ homepage: https://github.com/immosquare/immosquare-slack
45
44
  licenses:
46
45
  - MIT
47
46
  metadata: {}
48
- post_install_message:
49
47
  rdoc_options: []
50
48
  require_paths:
51
49
  - lib
@@ -53,15 +51,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
53
51
  requirements:
54
52
  - - ">="
55
53
  - !ruby/object:Gem::Version
56
- version: 2.7.2
54
+ version: 3.2.6
57
55
  required_rubygems_version: !ruby/object:Gem::Requirement
58
56
  requirements:
59
57
  - - ">="
60
58
  - !ruby/object:Gem::Version
61
59
  version: '0'
62
60
  requirements: []
63
- rubygems_version: 3.5.22
64
- signing_key:
61
+ rubygems_version: 4.0.12
65
62
  specification_version: 4
66
63
  summary: A Ruby gem for integrating with Slack API to perform various actions like
67
64
  posting messages.