immosquare-slack 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 30900d7e5d44acbfd25f87836eea3f8e95458e06977ad7dc48db9f2d327f4dcf
4
+ data.tar.gz: 0fd10ae542118b73b92fb943cb509feddc4fb42812e82b1d8707234dc446eb1c
5
+ SHA512:
6
+ metadata.gz: f6d89e11d26a10f08515915260ef442a5ab6164c75761695e78d87258024f601c72ebe903ecc11870802bf0c60205fa5f88b0f5aae9fa174356afe6e6f9707be
7
+ data.tar.gz: e21b4091ab55defe1fa6c2924a98e52c287b4befbb7eada3607c50b9c7d82474d1ff4fbb8e414b727baceadf1a0c07b68a170eb15b8c0d57ed1eb8fecc24f6f8
@@ -0,0 +1,63 @@
1
+ module ImmosquareSlack
2
+ module Channel
3
+ extend SharedMethods
4
+ class << self
5
+
6
+
7
+ ##============================================================##
8
+ ## Pour récupérer la liste des channels
9
+ ##============================================================##
10
+ def list_channels
11
+ fetch_paginated_data("https://slack.com/api/conversations.list", "channels")
12
+ end
13
+
14
+ ##============================================================##
15
+ ## Pour poster un message dans un channel
16
+ ##============================================================##
17
+ def post_message(channel_name, text, notify: false, bot_name: nil)
18
+ channel_id = get_channel_id_by_name(channel_name)
19
+ raise("Channel not found") if channel_id.nil?
20
+
21
+
22
+ url = "https://slack.com/api/chat.postMessage"
23
+ body = {
24
+ :channel => channel_id,
25
+ :text => "#{build_notification_text(channel_id) if notify}#{text}",
26
+ :username => bot_name.presence
27
+ }
28
+ make_slack_api_call(url, :method => :post, :body => body)
29
+ end
30
+
31
+
32
+
33
+ private
34
+
35
+ ##============================================================##
36
+ ## Pour récupérer l'id d'un channel en fonction de son nom
37
+ ##============================================================##
38
+ def get_channel_id_by_name(channel_name)
39
+ channels = list_channels
40
+ channel = channels.find {|c| c["name"] == channel_name }
41
+ channel ? channel["id"] : nil
42
+ end
43
+
44
+ ##============================================================##
45
+ ## Pour récupérer la liste des membres d'un channel
46
+ ##============================================================##
47
+ def get_channel_members(channel_id)
48
+ fetch_paginated_data("https://slack.com/api/conversations.members", "members", {:channel => channel_id})
49
+ end
50
+
51
+ ##============================================================##
52
+ ## Méthode récupérant les membres d'un channel et les notifier
53
+ ## sur le message
54
+ ##============================================================##
55
+ def build_notification_text(channel_id, text = "Hello")
56
+ members = get_channel_members(channel_id)
57
+ members = member_mentions = members.map {|member_id| "<@#{member_id}>" }
58
+ "#{text} #{member_mentions.join(", ")}\n"
59
+ end
60
+
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,11 @@
1
+ module ImmosquareSlack
2
+ class Configuration
3
+
4
+ attr_accessor :slack_api_token_bot, :openai_model
5
+
6
+ def initialize
7
+ @slack_api_token_bot = nil
8
+ end
9
+
10
+ end
11
+ end
@@ -0,0 +1,55 @@
1
+ module ImmosquareSlack
2
+ module SharedMethods
3
+ private
4
+
5
+ ##============================================================##
6
+ ## On récupère tous les résultats avec une loop sur le cursor
7
+ ##============================================================##
8
+ def fetch_paginated_data(url, data_key, extra_query = {})
9
+ items = []
10
+ cursor = nil
11
+
12
+ loop do
13
+ query = cursor ? {:cursor => cursor}.merge(extra_query) : extra_query
14
+ response = make_slack_api_call(url, :query => query)
15
+
16
+
17
+ items.concat(response[data_key])
18
+ cursor = response.dig("response_metadata", "next_cursor")
19
+ break if cursor.nil? || cursor.empty?
20
+ end
21
+ items
22
+ end
23
+
24
+ ##============================================================##
25
+ ## On fait le call à l'API Slack. On raise le message entier
26
+ ## en string si la réponse n'est pas "ok" ou si la réponse
27
+ ## n'est pas un JSON valide.
28
+ ##============================================================##
29
+ def make_slack_api_call(url, method: :get, query: {}, body: nil)
30
+ options = {
31
+ :headers => {
32
+ "Authorization" => "Bearer #{ImmosquareSlack.configuration.slack_api_token_bot}",
33
+ "Content-Type" => "application/json"
34
+ }
35
+ }
36
+
37
+ ##============================================================##
38
+ ## On crée les options en fonction du cas de figure
39
+ ##============================================================##
40
+ options[:query] = query if query.any?
41
+ options[:body] = body.to_json if body
42
+
43
+ ##============================================================##
44
+ ## On send la requête et on parse la réponse
45
+ ##============================================================##
46
+ response = HTTParty.send(method, url, options)
47
+ parsed_response = JSON.parse(response.body)
48
+ raise(parsed_response.to_json) unless parsed_response["ok"]
49
+
50
+ parsed_response
51
+ rescue JSON::ParserError
52
+ raise("Invalid JSON response")
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,13 @@
1
+ module ImmosquareSlack
2
+ module User
3
+ extend SharedMethods
4
+ class << self
5
+
6
+
7
+ def list_users
8
+ fetch_paginated_data("https://slack.com/api/users.list", "members")
9
+ end
10
+
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,3 @@
1
+ module ImmosquareSlack
2
+ VERSION = "0.1.0".freeze
3
+ end
@@ -0,0 +1,29 @@
1
+ require_relative "immosquare-slack/configuration"
2
+ require_relative "immosquare-slack/shared_methods"
3
+ require_relative "immosquare-slack/channel"
4
+ require_relative "immosquare-slack/user"
5
+
6
+
7
+ ##===========================================================================##
8
+ ##
9
+ ##===========================================================================##
10
+ module ImmosquareSlack
11
+ class << self
12
+
13
+ ##===========================================================================##
14
+ ## Gem configuration
15
+ ##===========================================================================##
16
+ attr_writer :configuration
17
+
18
+ def configuration
19
+ @configuration ||= Configuration.new
20
+ end
21
+
22
+ def config
23
+ yield(configuration)
24
+ end
25
+
26
+
27
+
28
+ end
29
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: immosquare-slack
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - IMMO SQUARE
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-12-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: httparty
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: ImmosquareSlack is a Ruby gem that provides an easy and efficient way
28
+ to integrate Ruby applications with Slack. It leverages the Slack API to enable
29
+ functionalities such as posting messages, managing channels, and other Slack-related
30
+ operations. Designed for simplicity and ease of use, this gem is ideal for developers
31
+ looking to enhance their Ruby applications with Slack's communication capabilities.
32
+ email:
33
+ - jules@immosquare.com
34
+ executables: []
35
+ extensions: []
36
+ extra_rdoc_files: []
37
+ files:
38
+ - lib/immosquare-slack.rb
39
+ - lib/immosquare-slack/channel.rb
40
+ - lib/immosquare-slack/configuration.rb
41
+ - lib/immosquare-slack/shared_methods.rb
42
+ - lib/immosquare-slack/user.rb
43
+ - lib/immosquare-slack/version.rb
44
+ homepage: https://github.com/IMMOSQUARE/immosquare-slack
45
+ licenses:
46
+ - MIT
47
+ metadata: {}
48
+ post_install_message:
49
+ rdoc_options: []
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: 2.7.2
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ requirements: []
63
+ rubygems_version: 3.4.13
64
+ signing_key:
65
+ specification_version: 4
66
+ summary: A Ruby gem for integrating with Slack API to perform various actions like
67
+ posting messages.
68
+ test_files: []