enerbot-slack 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: c64e1086ccdfa0448057636b7108f5590521e0e54788cc4cea10b1645d96f1f4
4
+ data.tar.gz: b03f92947d891aee5a5d99005677c47e0ea667281eb4fbeb5ebd498950f26bd3
5
+ SHA512:
6
+ metadata.gz: bf03f4ea93b1ea77466e3e5e2a306fb91477cb512d4d728d396fa55bf09b1dff458711eee2664ea6000e566b01c5b9d8b6c60438cd26b6762e8203be4df39252
7
+ data.tar.gz: 661324fa53960dd5609b38290a49e27d63e41908f619ba7cf3434ba95c824ef99c86bfc40fa77e29385bf2635dae43d042a841a228a880014ece14a7ff5ff783
@@ -0,0 +1,9 @@
1
+ class Credentials
2
+ def set_credentials
3
+ @bot_token = ENV['SLACK_TOKEN']
4
+ @bot_name = ENV['SLACK_NAME']
5
+ @bot_icon = ENV['SLACK_ICON']
6
+ @bot_channel = ENV['SLACK_DEFAULT_CHANNEL']
7
+ @bot_default_output = ENV['SLACK_DEFAULT_OUTPUT']
8
+ end
9
+ end
@@ -0,0 +1,30 @@
1
+ require 'slack-ruby-client'
2
+ require_relative 'modules/slack_message'
3
+ require_relative 'modules/slack_search'
4
+ require_relative 'modules/slack_image'
5
+ require_relative 'modules/slack_format'
6
+ require_relative 'credentials'
7
+
8
+ # Groups client initialization with instance parameters
9
+ class EnerbotSlack < Credentials
10
+ include SlackMessage
11
+ include SlackImage
12
+ include SlackFormat
13
+ include SlackSearch
14
+
15
+ def initialize(name: '', image: '', token: nil, as_user: false)
16
+ set_credentials
17
+
18
+ Slack.configure do |config|
19
+ config.token = token.nil? ? @bot_token : token
20
+ config.raise 'Missing token' unless config.token
21
+ end
22
+
23
+ @bot_name = name unless name.empty?
24
+ @bot_icon = image unless image.empty?
25
+ @bot_user = as_user
26
+
27
+ @time_client ||= Slack::RealTime::Client.new
28
+ @web_client ||= Slack::Web::Client.new
29
+ end
30
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Selection that should be improved
4
+ module SlackAdmin
5
+ def root_list_include?(user)
6
+ ENV['SLACK_BOT_ADMINS'].include? user
7
+ rescue NoMethodError => e
8
+ puts "You probably didn't set the SLACK_BOT_ADMINS variable. Error: #{e.message}"
9
+ false
10
+ end
11
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'slack-ruby-client'
4
+
5
+ # Client initialization that powers the rest of interactions
6
+ module SlackClient
7
+ def configure_client(type = 'web', token = ENV['SLACK_API_TOKEN'])
8
+ Slack.configure do |config|
9
+ config.token = token
10
+ config.raise 'Missing Bot token' unless config.token
11
+ end
12
+
13
+ case type
14
+ when 'realtime'
15
+ @configure_client = Slack::RealTime::Client.new
16
+ when 'web'
17
+ @configure_client = Slack::Web::Client.new
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Module for regular expressions that are common
4
+ module SlackFormat
5
+ def attachment_style(text, pretext: 'Test', color: '#e93d94', author: '')
6
+ attachment = []
7
+ attachment << {
8
+ "color": color,
9
+ "author_name": author,
10
+ "pretext": pretext,
11
+ "text": text
12
+ }
13
+ attachment
14
+ end
15
+
16
+ def coin_pattern(text)
17
+ check = text.match(/^<@(.*?)>.*(\+\+|--|balance)(.*)/ix)
18
+ !check.nil? ? check.captures : false
19
+ end
20
+
21
+ def hyper_text_pattern(text, type = 'user')
22
+ pattern = case type
23
+ when 'user'
24
+ /<@(.*)>/
25
+ when 'channel'
26
+ /<#(.*)\|(.*)>/
27
+ when 'channel_fix'
28
+ /\s#(.*?)\s/
29
+ end
30
+ text.match(pattern)
31
+ end
32
+
33
+ def channel_pattern(data)
34
+ match = data.match(/\s(<[#@])?((.*)\|)?(.*?)(>)? (.*?)$/i)
35
+ p match
36
+ unless match.nil?
37
+ channel = match.captures[2] || match.captures[3]
38
+ text = match.captures[5]
39
+ check_ts = channel.match(/(.*)-(\d*\.\d*)/)
40
+ channel = check_ts.captures[0] unless check_ts.nil?
41
+ thread = check_ts.captures[1] unless check_ts.nil?
42
+ end
43
+ [text, channel, thread]
44
+ end
45
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require './lib/slack_search'
4
+
5
+ # Obtains data of user from Slack and then uses them to mimic looks.
6
+ module SlackImage
7
+ def imitate_look(user)
8
+ c = get_user_info(user)
9
+ name = c.profile.display_name
10
+ image = c.profile.image_512
11
+ event_look_set(name, image)
12
+ end
13
+
14
+ def event_look_set(name, image)
15
+ ENV['SLACK_BOT_ICON'] = image
16
+ ENV['SLACK_BOT_NAME'] = name
17
+ end
18
+
19
+ def event_look_revert
20
+ ENV['SLACK_BOT_ICON'] = 'https://i.imgur.com/uo5AfyZ.png'
21
+ ENV['SLACK_BOT_NAME'] = 'Chayannetor'
22
+ end
23
+ end
@@ -0,0 +1,108 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'slack_client'
4
+ require_relative 'slack_search'
5
+
6
+ # Communication and interactions methods with Slack
7
+ module SlackMessage
8
+ include SlackClient
9
+ include SlackSearch
10
+
11
+ def destination_points(data, ts = nil)
12
+ @thread = if data.respond_to? :thread_ts
13
+ data.ts
14
+ else
15
+ ts unless ts.nil?
16
+ end
17
+ @channel = if data.respond_to? :channel
18
+ data.channel
19
+ else
20
+ data
21
+ end
22
+ end
23
+
24
+ def send_message(text, data, ts = nil)
25
+ client = configure_client
26
+ destination_points(data, ts)
27
+ client.chat_postMessage channel: @channel,
28
+ text: text,
29
+ icon_url: ENV['SLACK_BOT_ICON'],
30
+ username: ENV['SLACK_BOT_NAME'],
31
+ thread_ts: @thread,
32
+ as_user: false
33
+ rescue Slack::Web::Api::Errors::SlackError => e
34
+ print e.message
35
+ false
36
+ end
37
+
38
+ def send_direct_message(text, channel)
39
+ dm = get_user_id(channel)
40
+ dm == false ? false : send_message(text, dm)
41
+ end
42
+
43
+ def send_attachment(attachment, data, ts = nil)
44
+ client = configure_client
45
+ destination_points(data, ts)
46
+ client.chat_postMessage channel: @channel,
47
+ attachments: attachment,
48
+ icon_url: ENV['SLACK_BOT_ICON'],
49
+ username: ENV['SLACK_BOT_NAME'],
50
+ thread_ts: @thread,
51
+ as_user: false
52
+ rescue Slack::Web::Api::Errors::SlackError => e
53
+ print e.message
54
+ false
55
+ end
56
+
57
+ def send_ephemeral(text, user, data, ts = nil)
58
+ client = configure_client
59
+ destination_points(data, ts)
60
+ client.chat_postEphemeral channel: @channel,
61
+ text: text,
62
+ user: user,
63
+ icon_url: ENV['SLACK_BOT_ICON'],
64
+ username: ENV['SLACK_BOT_NAME'],
65
+ as_user: false
66
+ rescue Slack::Web::Api::Errors::SlackError => e
67
+ print e.message
68
+ false
69
+ end
70
+
71
+ def send_command(command, text, data, ts = nil)
72
+ client = configure_client('web', ENV['SLACK_REAL_TOKEN'])
73
+ destination_points(data, ts)
74
+ client.chat_command channel: @channel,
75
+ command: command,
76
+ text: text
77
+ end
78
+
79
+ def add_reaction(icon, channel, thread)
80
+ client = configure_client
81
+ client.reactions_add channel: channel,
82
+ name: icon,
83
+ timestamp: thread,
84
+ icon_url: ENV['SLACK_BOT_ICON'],
85
+ username: ENV['SLACK_BOT_NAME'],
86
+ as_user: false
87
+ rescue Slack::Web::Api::Errors::SlackError => e
88
+ print e.message
89
+ false
90
+ end
91
+
92
+ def send_file(path, data, ts = nil)
93
+ file = path
94
+ client = configure_client
95
+ destination_points(data, ts)
96
+ client.files_upload channels: @channel,
97
+ icon_url: ENV['SLACK_BOT_ICON'],
98
+ username: ENV['SLACK_BOT_NAME'],
99
+ thread_ts: @thread,
100
+ file: Faraday::UploadIO.new(file, 'text'),
101
+ title: File.basename(file),
102
+ filename: File.basename(file),
103
+ as_user: false
104
+ rescue Slack::Web::Api::Errors::SlackError => e
105
+ print e.message
106
+ false
107
+ end
108
+ end
@@ -0,0 +1,95 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'slack_client'
4
+
5
+ # Module for searching content on Slack
6
+ module SlackSearch
7
+ include SlackClient
8
+
9
+ def get_user_id(channel = @channel_id)
10
+ client = configure_client
11
+ dm = client.conversations_open users: channel
12
+ dm.channel.id
13
+ rescue Slack::Web::Api::Errors::SlackError => e
14
+ print e.message
15
+ false
16
+ end
17
+
18
+ def get_user_info(user)
19
+ client = configure_client
20
+ c = client.users_info user: user
21
+ c.user
22
+ rescue Slack::Web::Api::Errors::SlackError => e
23
+ print e.message
24
+ false
25
+ end
26
+
27
+ def get_user_list
28
+ client = configure_client
29
+ client.users_list
30
+ rescue Slack::Web::Api::Errors::SlackError => e
31
+ print e.message
32
+ false
33
+ end
34
+
35
+ def verify_type
36
+ properties = {
37
+ 'channel' => @properties.is_channel,
38
+ 'group' => @properties.is_group,
39
+ 'im' => @properties.is_im
40
+ }
41
+ check = ''
42
+ properties.each { |k, v| check = k if v == true }
43
+ check
44
+ end
45
+
46
+ def conversation_type(channel = @channel_id)
47
+ @channel_id = channel if @channel_id.nil?
48
+ result = conversation_info
49
+ if result == false
50
+ print 'Check if IM'
51
+ @channel_id = get_user_id
52
+ conversation_info == false ? false : verify_type
53
+ else
54
+ verify_type
55
+ end
56
+ rescue Slack::Web::Api::Errors::SlackError => e
57
+ print e.message
58
+ false
59
+ end
60
+
61
+ def conversation_info(channel = @channel_id)
62
+ client = configure_client
63
+ info = client.conversations_info channel: channel
64
+ @properties = info.channel
65
+ rescue Slack::Web::Api::Errors::SlackError => e
66
+ print e.message
67
+ false
68
+ end
69
+
70
+ def conversation_members(channel, limit = 60)
71
+ client = configure_client
72
+ info = client.conversations_members channel: channel, limit: limit
73
+ info.members
74
+ rescue Slack::Web::Api::Errors::SlackError => e
75
+ print e.message
76
+ false
77
+ end
78
+
79
+ def conversation_list
80
+ client = configure_client
81
+ client.conversations_list.channels
82
+ rescue Slack::Web::Api::Errors::SlackError => e
83
+ print e.message
84
+ false
85
+ end
86
+
87
+ def search_messages_on(channel, quantity = 5)
88
+ # type = conversation_type(channel)
89
+ client = configure_client
90
+ client.conversations_history channel: channel, count: quantity
91
+ rescue Slack::Web::Api::Errors::SlackError => e
92
+ print e.message
93
+ "Channel not found #{channel}"
94
+ end
95
+ end
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: enerbot-slack
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Luciano Adonis
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-02-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: colorize
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.8.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.8.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: slack-ruby-client
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '='
32
+ - !ruby/object:Gem::Version
33
+ version: 2.0.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '='
39
+ - !ruby/object:Gem::Version
40
+ version: 2.0.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.2'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.2'
55
+ description: The dream of enerbot
56
+ email: luciano.adonisv@gmail.com
57
+ executables: []
58
+ extensions: []
59
+ extra_rdoc_files: []
60
+ files:
61
+ - lib/credentials.rb
62
+ - lib/enerbot_slack.rb
63
+ - lib/modules/slack_admin.rb
64
+ - lib/modules/slack_client.rb
65
+ - lib/modules/slack_format.rb
66
+ - lib/modules/slack_image.rb
67
+ - lib/modules/slack_message.rb
68
+ - lib/modules/slack_search.rb
69
+ homepage: https://rubygems.org/gems/core_slack
70
+ licenses:
71
+ - MIT
72
+ metadata: {}
73
+ post_install_message:
74
+ rdoc_options: []
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ requirements: []
88
+ rubygems_version: 3.4.19
89
+ signing_key:
90
+ specification_version: 4
91
+ summary: Core functions for any kind of interaction with Slack
92
+ test_files: []