slack-smart-bot 1.0.2 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (32) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +5 -6
  3. data/lib/slack-smart-bot.rb +83 -47
  4. data/lib/slack-smart-bot_rules.rb +1 -1
  5. data/lib/slack/smart-bot/comm.rb +21 -21
  6. data/lib/slack/smart-bot/commands/general/bot_help.rb +1 -1
  7. data/lib/slack/smart-bot/commands/general/bot_status.rb +4 -4
  8. data/lib/slack/smart-bot/commands/general/stop_using_rules.rb +2 -2
  9. data/lib/slack/smart-bot/commands/general/use_rules.rb +2 -2
  10. data/lib/slack/smart-bot/commands/on_bot/add_shortcut.rb +1 -1
  11. data/lib/slack/smart-bot/commands/on_bot/admin/add_routine.rb +5 -5
  12. data/lib/slack/smart-bot/commands/on_bot/admin/extend_rules.rb +4 -4
  13. data/lib/slack/smart-bot/commands/on_bot/admin/pause_bot.rb +3 -3
  14. data/lib/slack/smart-bot/commands/on_bot/admin/pause_routine.rb +2 -2
  15. data/lib/slack/smart-bot/commands/on_bot/admin/remove_routine.rb +6 -2
  16. data/lib/slack/smart-bot/commands/on_bot/admin/run_routine.rb +2 -2
  17. data/lib/slack/smart-bot/commands/on_bot/admin/see_routines.rb +5 -5
  18. data/lib/slack/smart-bot/commands/on_bot/admin/start_bot.rb +3 -3
  19. data/lib/slack/smart-bot/commands/on_bot/admin/start_routine.rb +2 -2
  20. data/lib/slack/smart-bot/commands/on_bot/admin/stop_using_rules_on.rb +3 -3
  21. data/lib/slack/smart-bot/commands/on_bot/delete_shortcut.rb +2 -2
  22. data/lib/slack/smart-bot/commands/on_bot/ruby_code.rb +1 -1
  23. data/lib/slack/smart-bot/commands/on_extended/bot_rules.rb +2 -2
  24. data/lib/slack/smart-bot/commands/on_master/admin/kill_bot_on_channel.rb +1 -1
  25. data/lib/slack/smart-bot/commands/on_master/admin_master/exit_bot.rb +9 -3
  26. data/lib/slack/smart-bot/commands/on_master/admin_master/notify_message.rb +3 -3
  27. data/lib/slack/smart-bot/commands/on_master/create_bot.rb +11 -11
  28. data/lib/slack/smart-bot/listen.rb +1 -1
  29. data/lib/slack/smart-bot/process_first.rb +16 -10
  30. data/lib/slack/smart-bot/treat_message.rb +13 -13
  31. data/lib/slack/smart-bot/utils.rb +20 -16
  32. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e640de80df6bea5fc531eff1e1e08d4032018b15be712262851779f04028f642
4
- data.tar.gz: d3e361a01f6fedd4447a7874c3b8960b24ba18b716a4e1c1c3c83ee90a77d67b
3
+ metadata.gz: bc5284b6978388549e27e9f7e438064e0eb36dbe6528363e966cb024d5cad4e7
4
+ data.tar.gz: a3dea56debef65e4e9cd3b3e2733571721fa8e370ad97026dfe35f9836f4f285
5
5
  SHA512:
6
- metadata.gz: ae2e4b9e6dd3180a5b73ae91d4e6b07de9696ffc0a7502342810938414ea9680b3fd7135a9df9046fb33917405fe6f902b7cb65269f6cd47bba7c3304dd16aa4
7
- data.tar.gz: d8bb4daf48b2788d4824ae0103edf1e6382896269e561a97d9c9076769e028bd4a6ebaa8a70d4ab13c38825149e68f492c8747de45ef96a51226b1c7509b443b
6
+ metadata.gz: c2b70ce96e94bf8b27e03694becf8abb7c7bec14a130c6d887bddc8db65f3ec638197603798130056cdce4e30ca3b211317134a363035680fa547b83fcd30a1d
7
+ data.tar.gz: 376585b302cd52381fed5539c4fc00d75f3d93c7c530d042bdf73ee6acf3e8c1a07623a57c1823de38b3b7a9ee4be5063401caecebe17efa2f0e84b4543500fd
data/README.md CHANGED
@@ -39,14 +39,13 @@ After you install it you will need just a couple of things to configure it.
39
39
  Create a file like this on the folder you want:
40
40
 
41
41
  ```ruby
42
- # the channel that will act like the master channel, main channel
43
- MASTER_CHANNEL="my_master_channel"
44
- #names of the master users
45
- MASTER_USERS=["mario"]
46
42
 
47
43
  require 'slack-smart-bot'
48
44
 
49
45
  settings = {
46
+ # the channel that will act like the master channel, main channel
47
+ master_channel: 'my_master_channel',
48
+ masters: ["mario"], #names of the master users
50
49
  nick: 'smart-bot', # the smart bot name
51
50
  token: 'xxxxxxxxxxxxxxxxxx' # the API Slack token
52
51
  }
@@ -56,9 +55,9 @@ SlackSmartBot.new(settings).listen
56
55
 
57
56
  ```
58
57
 
59
- The MASTER_CHANNEL will be the channel where you will be able to create other bots and will have special treatment.
58
+ The master_channel will be the channel where you will be able to create other bots and will have special treatment.
60
59
 
61
- The MASTER_USERS will have full access to everything. You need to use the slack user name defined on https://YOUR_WORK_SPACE.slack.com/account/settings#username.
60
+ The masters will have full access to everything. You need to use the slack user name defined on https://YOUR_WORK_SPACE.slack.com/account/settings#username.
62
61
 
63
62
  For the token remember you need to generate a token on the Slack web for the bot user.
64
63
 
@@ -16,29 +16,7 @@ require_relative "slack/smart-bot/process_first"
16
16
  require_relative "slack/smart-bot/process"
17
17
  require_relative "slack/smart-bot/utils"
18
18
 
19
- TESTING_SLACK_SMART_BOT ||= false
20
- unless TESTING_SLACK_SMART_BOT
21
- if ARGV.size == 0
22
- CHANNEL = MASTER_CHANNEL
23
- ON_MASTER_BOT = true
24
- ADMIN_USERS = MASTER_USERS
25
- RULES_FILE = "#{$0.gsub(".rb", "_rules.rb")}" unless defined?(RULES_FILE)
26
- unless File.exist?(RULES_FILE)
27
- default_rules = (__FILE__).gsub(/\.rb$/, "_rules.rb")
28
- FileUtils.copy_file(default_rules, RULES_FILE)
29
- end
30
- STATUS_INIT = :on
31
- SHORTCUTS_FILE = "slack-smart-bot_shortcuts_#{CHANNEL}.rb".gsub(" ", "_")
32
- else
33
- ON_MASTER_BOT = false
34
- CHANNEL = ARGV[0]
35
- ADMIN_USERS = ARGV[1].split(",")
36
- RULES_FILE = ARGV[2]
37
- STATUS_INIT = ARGV[3].to_sym
38
- SHORTCUTS_FILE = "slack-smart-bot_shortcuts_#{CHANNEL}.rb".gsub(" ", "_")
39
- end
40
- end
41
-
19
+ ADMIN_USERS = MASTER_USERS if defined?(MASTER_USERS) # for bg compatibility
42
20
  class SlackSmartBot
43
21
  attr_accessor :config, :client
44
22
  attr_reader :master_bot_id, :channel_id
@@ -51,22 +29,80 @@ class SlackSmartBot
51
29
  VERSION = version
52
30
 
53
31
  def initialize(config)
54
- Dir.mkdir("./logs") unless Dir.exist?("./logs")
55
- Dir.mkdir("./shortcuts") unless Dir.exist?("./shortcuts")
56
- Dir.mkdir("./routines") unless Dir.exist?("./routines")
57
- logfile = File.basename(RULES_FILE.gsub("_rules_", "_logs_"), ".rb") + ".log"
58
- @logger = Logger.new("./logs/#{logfile}")
59
- config_log = config.dup
60
- config_log.delete(:token)
32
+ if config.key?(:path) and config[:path] != ''
33
+ config.path.chop! if config.path[-1]=="/"
34
+ else
35
+ config[:path] = '.'
36
+ end
61
37
  config[:silent] = false unless config.key?(:silent)
62
38
  config[:testing] = false unless config.key?(:testing)
63
39
  config[:simulate] = false unless config.key?(:simulate)
40
+ if config.path.to_s!='' and config.file.to_s==''
41
+ config.file = File.basename($0)
42
+ end
43
+ if config.key?(:file) and config.file!=''
44
+ config.file_path = "#{config.path}/#{config.file}"
45
+ else
46
+ config.file_path = $0
47
+ config.file = File.basename(config.file_path)
48
+ config.path = File.dirname(config.file_path)
49
+ end
50
+ Dir.mkdir("#{config.path}/logs") unless Dir.exist?("#{config.path}/logs")
51
+ Dir.mkdir("#{config.path}/shortcuts") unless Dir.exist?("#{config.path}/shortcuts")
52
+ Dir.mkdir("#{config.path}/routines") unless Dir.exist?("#{config.path}/routines")
53
+
54
+ config.masters = MASTER_USERS if config.masters.to_s=='' and defined?(MASTER_USERS)
55
+ config.master_channel = MASTER_CHANNEL if config.master_channel.to_s=='' and defined?(MASTER_CHANNEL)
56
+
57
+ if ARGV.size == 0 or (config.file.to_s!='' and config.file.to_s!=File.basename($0))
58
+ config.rules_file = "#{config.file.gsub(".rb", "_rules.rb")}" unless config.rules_file.to_s!=''
59
+ unless File.exist?(config.path + '/' + config.rules_file)
60
+ default_rules = (__FILE__).gsub(/\.rb$/, "_rules.rb")
61
+ FileUtils.copy_file(default_rules, config.path + '/' + config.rules_file)
62
+ end
63
+ config.admins = config.masters unless config.admins.to_s!=''
64
+ config.channel = config.master_channel unless config.channel.to_s!=''
65
+ config.status_init = :on unless config.status_init.to_s!=''
66
+ else
67
+ config.rules_file = ARGV[2]
68
+ config.admins = ARGV[1].split(",")
69
+ config.channel = ARGV[0]
70
+ config.status_init = ARGV[3].to_sym
71
+ end
72
+ config.rules_file[0]='' if config.rules_file[0]=='.'
73
+ config.rules_file='/'+config.rules_file if config.rules_file[0]!='/'
74
+
75
+ config.shortcuts_file = "slack-smart-bot_shortcuts_#{config.channel}.rb".gsub(" ", "_")
76
+ if config.channel == config.master_channel
77
+ config.on_master_bot = true
78
+ config.start_bots = true unless config.key?(:start_bots)
79
+ else
80
+ config.on_master_bot = false
81
+ end
82
+
83
+ if !config.key?(:token) or config.token.to_s == ''
84
+ abort "You need to supply a valid token key on the settings. key: :token"
85
+ elsif !config.key?(:masters) or !config.masters.is_a?(Array) or config.masters.size == 0
86
+ abort "You need to supply a masters array on the settings containing the user names of the master admins. key: :masters"
87
+ elsif !config.key?(:master_channel) or config.master_channel.to_s == ''
88
+ abort "You need to supply a master_channel on the settings. key: :master_channel"
89
+ elsif !config.key?(:channel) or config.channel.to_s == ''
90
+ abort "You need to supply a bot channel name on the settings. key: :channel"
91
+ end
92
+
93
+
94
+
95
+ logfile = File.basename(config.rules_file.gsub("_rules_", "_logs_"), ".rb") + ".log"
96
+ @logger = Logger.new("#{config.path}/logs/#{logfile}")
97
+ @logger.info ARGV.inspect #Jal
98
+
99
+ config_log = config.dup
100
+ config_log.delete(:token)
64
101
  @logger.info "Initializing bot: #{config_log.inspect}"
65
102
 
66
- File.new("./buffer.log", "w") if config[:testing] and ON_MASTER_BOT
67
- File.new("./buffer_complete.log", "w") if config[:simulate] and ON_MASTER_BOT
103
+ File.new("#{config.path}/buffer.log", "w") if config[:testing] and config.on_master_bot
104
+ File.new("#{config.path}/buffer_complete.log", "w") if config[:simulate] and config.on_master_bot
68
105
 
69
- config[:channel] = CHANNEL
70
106
  self.config = config
71
107
 
72
108
  Slack.configure do |conf|
@@ -101,8 +137,8 @@ class SlackSmartBot
101
137
  @rules_imported = Hash.new()
102
138
  @routines = Hash.new()
103
139
 
104
- if File.exist?("./shortcuts/#{SHORTCUTS_FILE}")
105
- file_sc = IO.readlines("./shortcuts/#{SHORTCUTS_FILE}").join
140
+ if File.exist?("#{config.path}/shortcuts/#{config.shortcuts_file}")
141
+ file_sc = IO.readlines("#{config.path}/shortcuts/#{config.shortcuts_file}").join
106
142
  unless file_sc.to_s() == ""
107
143
  @shortcuts = eval(file_sc)
108
144
  end
@@ -110,14 +146,14 @@ class SlackSmartBot
110
146
 
111
147
  get_routines()
112
148
 
113
- if ON_MASTER_BOT and File.exist?($0.gsub(".rb", "_bots.rb"))
149
+ if config.on_master_bot and File.exist?(config.file_path.gsub(".rb", "_bots.rb"))
114
150
  get_bots_created()
115
- if @bots_created.kind_of?(Hash)
151
+ if @bots_created.kind_of?(Hash) and config.start_bots
116
152
  @bots_created.each { |key, value|
117
153
  if !value.key?(:cloud) or (value.key?(:cloud) and value[:cloud] == false)
118
- @logger.info "ruby #{$0} \"#{value[:channel_name]}\" \"#{value[:admins]}\" \"#{value[:rules_file]}\" #{value[:status].to_sym}"
154
+ @logger.info "ruby #{config.file_path} \"#{value[:channel_name]}\" \"#{value[:admins]}\" \"#{value[:rules_file]}\" #{value[:status].to_sym}"
119
155
  t = Thread.new do
120
- `ruby #{$0} \"#{value[:channel_name]}\" \"#{value[:admins]}\" \"#{value[:rules_file]}\" #{value[:status].to_sym}`
156
+ `ruby #{config.file_path} \"#{value[:channel_name]}\" \"#{value[:admins]}\" \"#{value[:rules_file]}\" #{value[:status].to_sym}`
121
157
  end
122
158
  value[:thread] = t
123
159
  end
@@ -126,8 +162,8 @@ class SlackSmartBot
126
162
  end
127
163
 
128
164
  # rules imported only for DM
129
- if ON_MASTER_BOT and File.exist?("./rules/rules_imported.rb")
130
- file_conf = IO.readlines("./rules/rules_imported.rb").join
165
+ if config.on_master_bot and File.exist?("#{config.path}/rules/rules_imported.rb")
166
+ file_conf = IO.readlines("#{config.path}/rules/rules_imported.rb").join
131
167
  unless file_conf.to_s() == ""
132
168
  @rules_imported = eval(file_conf)
133
169
  end
@@ -146,7 +182,7 @@ class SlackSmartBot
146
182
 
147
183
  begin
148
184
  @admin_users_id = []
149
- ADMIN_USERS.each do |au|
185
+ config.admins.each do |au|
150
186
  user_info = client.web_client.users_info(user: "@#{au}")
151
187
  @admin_users_id << user_info.user.id
152
188
  end
@@ -154,7 +190,7 @@ class SlackSmartBot
154
190
  @logger.fatal "TooManyRequestsError"
155
191
  abort("TooManyRequestsError please re run the bot and be sure of executing first: killall ruby")
156
192
  rescue Exception => stack
157
- abort("The admin user specified on settings: #{ADMIN_USERS.join(", ")}, doesn't exist on Slack. Execution aborted")
193
+ abort("The admin user specified on settings: #{config.admins.join(", ")}, doesn't exist on Slack. Execution aborted")
158
194
  end
159
195
 
160
196
  client.on :hello do
@@ -172,20 +208,20 @@ class SlackSmartBot
172
208
  end
173
209
  @routines.each do |ch, rout|
174
210
  rout.each do |k, v|
175
- if !v[:running] and v[:channel_name] == CHANNEL
211
+ if !v[:running] and v[:channel_name] == config.channel
176
212
  create_routine_thread(k)
177
213
  end
178
214
  end
179
215
  end
180
216
  end
181
217
 
182
- @status = STATUS_INIT
218
+ @status = config.status_init
183
219
  @questions = Hash.new()
184
220
  @channels_id = Hash.new()
185
221
  @channels_name = Hash.new()
186
222
  get_channels_name_and_id()
187
- @channel_id = @channels_id[CHANNEL].dup
188
- @master_bot_id = @channels_id[MASTER_CHANNEL].dup
223
+ @channel_id = @channels_id[config.channel].dup
224
+ @master_bot_id = @channels_id[config.master_channel].dup
189
225
 
190
226
  get_routines()
191
227
  if @routines.key?(@channel_id)
@@ -124,6 +124,6 @@ def rules(user, command, processed, dest, files = [], rules_file = "")
124
124
  end
125
125
  rescue => exception
126
126
  @logger.fatal exception
127
- respond "Unexpected error!! Please contact an admin to solve it: <@#{ADMIN_USERS.join(">, <@")}>"
127
+ respond "Unexpected error!! Please contact an admin to solve it: <@#{config.admins.join(">, <@")}>"
128
128
  end
129
129
  end
@@ -7,27 +7,27 @@ class SlackSmartBot
7
7
 
8
8
  if dest.nil?
9
9
  if config[:simulate]
10
- open("./buffer_complete.log", "a") { |f|
10
+ open("#{config.path}/buffer_complete.log", "a") { |f|
11
11
  f.puts "|#{@channel_id}|#{config[:nick_id]}|#{msg}$$$"
12
12
  }
13
13
  else
14
14
  client.message(channel: @channel_id, text: msg, as_user: true)
15
15
  end
16
- if config[:testing] and ON_MASTER_BOT
17
- open("./buffer.log", "a") { |f|
16
+ if config[:testing] and config.on_master_bot
17
+ open("#{config.path}/buffer.log", "a") { |f|
18
18
  f.puts "|#{@channel_id}|#{config[:nick_id]}|#{msg}"
19
19
  }
20
20
  end
21
21
  elsif dest[0] == "C" or dest[0] == "G" # channel
22
22
  if config[:simulate]
23
- open("./buffer_complete.log", "a") { |f|
23
+ open("#{config.path}/buffer_complete.log", "a") { |f|
24
24
  f.puts "|#{dest}|#{config[:nick_id]}|#{msg}$$$"
25
25
  }
26
26
  else
27
27
  client.message(channel: dest, text: msg, as_user: true)
28
28
  end
29
- if config[:testing] and ON_MASTER_BOT
30
- open("./buffer.log", "a") { |f|
29
+ if config[:testing] and config.on_master_bot
30
+ open("#{config.path}/buffer.log", "a") { |f|
31
31
  f.puts "|#{dest}|#{config[:nick_id]}|#{msg}"
32
32
  }
33
33
  end
@@ -69,27 +69,27 @@ class SlackSmartBot
69
69
  message = "#{to}: #{question}"
70
70
  if dest.nil?
71
71
  if config[:simulate]
72
- open("./buffer_complete.log", "a") { |f|
72
+ open("#{config.path}/buffer_complete.log", "a") { |f|
73
73
  f.puts "|#{@channel_id}|#{config[:nick_id]}|#{message}$$$"
74
74
  }
75
75
  else
76
76
  client.message(channel: @channel_id, text: message, as_user: true)
77
77
  end
78
- if config[:testing] and ON_MASTER_BOT
79
- open("./buffer.log", "a") { |f|
78
+ if config[:testing] and config.on_master_bot
79
+ open("#{config.path}/buffer.log", "a") { |f|
80
80
  f.puts "|#{@channel_id}|#{config[:nick_id]}|#{message}"
81
81
  }
82
82
  end
83
83
  elsif dest[0] == "C" or dest[0] == "G" # channel
84
84
  if config[:simulate]
85
- open("./buffer_complete.log", "a") { |f|
85
+ open("#{config.path}/buffer_complete.log", "a") { |f|
86
86
  f.puts "|#{dest}|#{config[:nick_id]}|#{message}$$$"
87
87
  }
88
88
  else
89
89
  client.message(channel: dest, text: message, as_user: true)
90
90
  end
91
- if config[:testing] and ON_MASTER_BOT
92
- open("./buffer.log", "a") { |f|
91
+ if config[:testing] and config.on_master_bot
92
+ open("#{config.path}/buffer.log", "a") { |f|
93
93
  f.puts "|#{dest}|#{config[:nick_id]}|#{message}"
94
94
  }
95
95
  end
@@ -112,14 +112,14 @@ class SlackSmartBot
112
112
  @logger.fatal "Channel: #{to} not found. Message: #{msg}"
113
113
  end
114
114
  if config[:simulate]
115
- open("./buffer_complete.log", "a") { |f|
115
+ open("#{config.path}/buffer_complete.log", "a") { |f|
116
116
  f.puts "|#{to}|#{config[:nick_id]}|#{msg}$$$"
117
117
  }
118
118
  else
119
119
  client.message(channel: channel_id, text: msg, as_user: true)
120
120
  end
121
- if config[:testing] and ON_MASTER_BOT
122
- open("./buffer.log", "a") { |f|
121
+ if config[:testing] and config.on_master_bot
122
+ open("#{config.path}/buffer.log", "a") { |f|
123
123
  f.puts "|#{to}|#{config[:nick_id]}|#{msg}"
124
124
  }
125
125
  end
@@ -131,28 +131,28 @@ class SlackSmartBot
131
131
  unless msg == ""
132
132
  if id_user[0] == "D"
133
133
  if config[:simulate]
134
- open("./buffer_complete.log", "a") { |f|
134
+ open("#{config.path}/buffer_complete.log", "a") { |f|
135
135
  f.puts "|#{id_user}|#{config[:nick_id]}|#{msg}$$$"
136
136
  }
137
137
  else
138
138
  client.message(channel: id_user, as_user: true, text: msg)
139
139
  end
140
- if config[:testing] and ON_MASTER_BOT
141
- open("./buffer.log", "a") { |f|
140
+ if config[:testing] and config.on_master_bot
141
+ open("#{config.path}/buffer.log", "a") { |f|
142
142
  f.puts "|#{id_user}|#{config[:nick_id]}|#{msg}"
143
143
  }
144
144
  end
145
145
  else
146
146
  im = client.web_client.im_open(user: id_user)
147
147
  if config[:simulate]
148
- open("./buffer_complete.log", "a") { |f|
148
+ open("#{config.path}/buffer_complete.log", "a") { |f|
149
149
  f.puts "|#{im["channel"]["id"]}|#{config[:nick_id]}|#{msg}$$$"
150
150
  }
151
151
  else
152
152
  client.message(channel: im["channel"]["id"], as_user: true, text: msg)
153
153
  end
154
- if config[:testing] and ON_MASTER_BOT
155
- open("./buffer.log", "a") { |f|
154
+ if config[:testing] and config.on_master_bot
155
+ open("#{config.path}/buffer.log", "a") { |f|
156
156
  f.puts "|#{im["channel"]["id"]}|#{config[:nick_id]}|#{msg}"
157
157
  }
158
158
  end
@@ -53,7 +53,7 @@ class SlackSmartBot
53
53
  if specific
54
54
  unless rules_file.empty?
55
55
  begin
56
- eval(File.new(rules_file).read) if File.exist?(rules_file)
56
+ eval(File.new(config.path + rules_file).read) if File.exist?(config.path + rules_file)
57
57
  end
58
58
  end
59
59
  if defined?(git_project) && (git_project.to_s != "") && (help_message_rules != "") && (help_command.to_s == "")
@@ -14,10 +14,10 @@ class SlackSmartBot
14
14
  end
15
15
  require "socket"
16
16
  ip_address = Socket.ip_address_list.find { |ai| ai.ipv4? && !ai.ipv4_loopback? }.ip_address
17
- respond "*#{Socket.gethostname} (#{ip_address})*\n\tStatus: #{@status}.\n\tVersion: #{VERSION}.#{version_message}\n\tRules file: #{File.basename RULES_FILE}\n\tExtended: #{@bots_created[@channel_id][:extended] unless ON_MASTER_BOT}\n\tAdmins: #{ADMIN_USERS}\n\tBot time: #{Time.now}", dest
17
+ respond "*#{Socket.gethostname} (#{ip_address})*\n\tStatus: #{@status}.\n\tVersion: #{VERSION}.#{version_message}\n\tRules file: #{File.basename config.rules_file}\n\tExtended: #{@bots_created[@channel_id][:extended] unless config.on_master_bot}\n\tAdmins: #{config.admins}\n\tBot time: #{Time.now}", dest
18
18
  if @status == :on
19
19
  respond "I'm listening to [#{@listening.join(", ")}]", dest
20
- if ON_MASTER_BOT and ADMIN_USERS.include?(from)
20
+ if config.on_master_bot and config.admins.include?(from)
21
21
  sleep 5
22
22
  @bots_created.each do |k, v|
23
23
  msg = []
@@ -29,8 +29,8 @@ class SlackSmartBot
29
29
  msg << "\trules: #{v[:rules_file]}"
30
30
  msg << "\textended: #{v[:extended]}"
31
31
  msg << "\tcloud: #{v[:cloud]}"
32
- if ON_MASTER_BOT and v.key?(:cloud) and v[:cloud]
33
- msg << "\trunner: `ruby #{$0} \"#{v[:channel_name]}\" \"#{v[:admins]}\" \"#{v[:rules_file]}\" on&`"
32
+ if config.on_master_bot and v.key?(:cloud) and v[:cloud]
33
+ msg << "\trunner: `ruby #{config.file} \"#{v[:channel_name]}\" \"#{v[:admins]}\" \"#{v[:rules_file]}\" on&`"
34
34
  end
35
35
  respond msg.join("\n"), dest
36
36
  end
@@ -15,7 +15,7 @@ class SlackSmartBot
15
15
  respond "You are not using those rules.", dest
16
16
  else
17
17
  @rules_imported[user.id].delete(dchannel)
18
- update_rules_imported() if ON_MASTER_BOT
18
+ update_rules_imported() if config.on_master_bot
19
19
  respond "You won't be using those rules from now on.", dest
20
20
 
21
21
  def git_project() "" end
@@ -30,7 +30,7 @@ class SlackSmartBot
30
30
  respond "You are not using those rules.", dest
31
31
  else
32
32
  @rules_imported[user.id].delete(user.id)
33
- update_rules_imported() if ON_MASTER_BOT
33
+ update_rules_imported() if config.on_master_bot
34
34
  respond "You won't be using those rules from now on.", dest
35
35
 
36
36
  def git_project() "" end
@@ -21,7 +21,7 @@ class SlackSmartBot
21
21
 
22
22
  if channel_found.nil?
23
23
  respond "The channel you are trying to use doesn't exist", dest
24
- elsif channel_found.name == MASTER_CHANNEL
24
+ elsif channel_found.name == config.master_channel
25
25
  respond "You cannot use the rules from Master Channel on any other channel.", dest
26
26
  elsif !@bots_created.key?(@channels_id[channel])
27
27
  respond "There is no bot running on that channel.", dest
@@ -35,7 +35,7 @@ class SlackSmartBot
35
35
  else
36
36
  @rules_imported[user.id][user.id] = channel_found.id
37
37
  end
38
- update_rules_imported() if ON_MASTER_BOT
38
+ update_rules_imported() if config.on_master_bot
39
39
  respond "I'm using now the rules from <##{channel_found.id}>", dest
40
40
 
41
41
  def git_project() "" end
@@ -28,7 +28,7 @@ class SlackSmartBot
28
28
  end
29
29
  }
30
30
  end
31
- if !ADMIN_USERS.include?(from) and @shortcuts[:all].include?(shortcut_name) and !@shortcuts[from].include?(shortcut_name)
31
+ if !config.admins.include?(from) and @shortcuts[:all].include?(shortcut_name) and !@shortcuts[from].include?(shortcut_name)
32
32
  respond "Only the creator of the shortcut can modify it", dest
33
33
  elsif found_other
34
34
  respond "You cannot create a shortcut for all with the same name than other user is using", dest
@@ -19,8 +19,8 @@ class SlackSmartBot
19
19
  # helpadmin: _add routine example at 17:05 ruby puts 'a'_
20
20
  # helpadmin:
21
21
  def add_routine(dest, from, user, name, type, number_time, period, command_to_run, files)
22
- if files.nil? or files.size == 0 or (files.size > 0 and MASTER_USERS.include?(from))
23
- if ADMIN_USERS.include?(from)
22
+ if files.nil? or files.size == 0 or (files.size > 0 and config.masters.include?(from))
23
+ if config.admins.include?(from)
24
24
  if @routines.key?(@channel_id) && @routines[@channel_id].key?(name)
25
25
  respond "I'm sorry but there is already a routine with that name.\nCall `see routines` to see added routines", dest
26
26
  else
@@ -58,18 +58,18 @@ class SlackSmartBot
58
58
  end
59
59
  every_in_seconds = 24 * 60 * 60
60
60
  end
61
- Dir.mkdir("./routines/#{@channel_id}") unless Dir.exist?("./routines/#{@channel_id}")
61
+ Dir.mkdir("#{config.path}/routines/#{@channel_id}") unless Dir.exist?("#{config.path}/routines/#{@channel_id}")
62
62
 
63
63
  if !files.nil? && (files.size == 1)
64
64
  @logger.info files[0].inspect if config.testing
65
- file_path = "./routines/#{@channel_id}/#{name}#{files[0].name.scan(/[^\.]+(\.\w+$)/).join}"
65
+ file_path = "#{config.path}/routines/#{@channel_id}/#{name}#{files[0].name.scan(/[^\.]+(\.\w+$)/).join}"
66
66
  http = NiceHttp.new(host: "https://files.slack.com", headers: { "Authorization" => "Bearer #{config[:token]}" }, log_headers: :partial)
67
67
  http.get(files[0].url_private_download, save_data: file_path)
68
68
  system("chmod +x #{file_path}")
69
69
  end
70
70
 
71
71
  @routines[@channel_id] = {} unless @routines.key?(@channel_id)
72
- @routines[@channel_id][name] = { channel_name: CHANNEL, creator: from, creator_id: user.id, status: :on,
72
+ @routines[@channel_id][name] = { channel_name: config.channel, creator: from, creator_id: user.id, status: :on,
73
73
  every: every, every_in_seconds: every_in_seconds, at: at, file_path: file_path, command: command_to_run.to_s.strip,
74
74
  next_run: next_run.to_s, dest: dest, last_run: "", last_elapsed: "", running: false }
75
75
  update_routines
@@ -7,10 +7,10 @@ class SlackSmartBot
7
7
  # helpadmin:
8
8
  def extend_rules(dest, user, from, channel, typem)
9
9
  unless typem == :on_extended
10
- if ON_MASTER_BOT
10
+ if config.on_master_bot
11
11
  respond "You cannot use the rules from Master Channel on any other channel.", dest
12
- elsif !ADMIN_USERS.include?(from) #not admin
13
- respond "Only admins can extend the rules. Admins on this channel: #{ADMIN_USERS}", dest
12
+ elsif !config.admins.include?(from) #not admin
13
+ respond "Only admins can extend the rules. Admins on this channel: #{config.admins}", dest
14
14
  else
15
15
  #todo: add pagination for case more than 1000 channels on the workspace
16
16
  channels = client.web_client.conversations_list(
@@ -47,7 +47,7 @@ class SlackSmartBot
47
47
  @bots_created[@channel_id][:extended] = [] unless @bots_created[@channel_id].key?(:extended)
48
48
  @bots_created[@channel_id][:extended] << channel
49
49
  update_bots_file()
50
- respond "<@#{user.id}> extended the rules from #{CHANNEL} to be used on #{channel}.", @master_bot_id
50
+ respond "<@#{user.id}> extended the rules from #{config.channel} to be used on #{channel}.", @master_bot_id
51
51
  if @channels_id[channel][0] == "G"
52
52
  respond "Now the rules from <##{@channel_id}> are available on *#{channel}*", dest
53
53
  else
@@ -7,12 +7,12 @@ class SlackSmartBot
7
7
  # helpadmin: You can use this command only if you are an admin user
8
8
  # helpadmin:
9
9
  def pause_bot(dest, from)
10
- if ADMIN_USERS.include?(from) #admin user
10
+ if config.admins.include?(from) #admin user
11
11
  respond "This bot is paused from now on. You can start it again: start this bot", dest
12
12
  respond "zZzzzzZzzzzZZZZZZzzzzzzzz", dest
13
13
  @status = :paused
14
- unless ON_MASTER_BOT
15
- send_msg_channel MASTER_CHANNEL, "Changed status on #{CHANNEL} to :paused"
14
+ unless config.on_master_bot
15
+ send_msg_channel config.master_channel, "Changed status on #{config.channel} to :paused"
16
16
  end
17
17
  else
18
18
  respond "Only admin users can put me on pause", dest
@@ -8,8 +8,8 @@ class SlackSmartBot
8
8
  # helpadmin: _pause routine example_
9
9
  # helpadmin:
10
10
  def pause_routine(dest, from, name)
11
- if ADMIN_USERS.include?(from) #admin user
12
- if !ON_MASTER_BOT and dest[0] == "D"
11
+ if config.admins.include?(from) #admin user
12
+ if !config.on_master_bot and dest[0] == "D"
13
13
  respond "It's only possible to pause routines from MASTER channel from a direct message with the bot.", dest
14
14
  elsif @routines.key?(@channel_id) and @routines[@channel_id].key?(name)
15
15
  @routines[@channel_id][name][:status] = :paused
@@ -11,12 +11,15 @@ class SlackSmartBot
11
11
  # helpadmin: _kill routine example_
12
12
  # helpadmin:
13
13
  def remove_routine(dest, from, name)
14
- if ADMIN_USERS.include?(from) #admin user
15
- if !ON_MASTER_BOT and dest[0] == "D"
14
+ @logger.info "Start:#{Time.now}"
15
+ if config.admins.include?(from) #admin user
16
+ if !config.on_master_bot and dest[0] == "D"
16
17
  respond "It's only possible to remove routines from MASTER channel from a direct message with the bot.", dest
17
18
  elsif @routines.key?(@channel_id) and @routines[@channel_id].key?(name)
18
19
  @routines[@channel_id].delete(name)
20
+ @logger.info "Cont:#{Time.now}"
19
21
  update_routines()
22
+ @logger.info "end:#{Time.now}"
20
23
  respond "The routine *`#{name}`* has been removed.", dest
21
24
  else
22
25
  respond "There isn't a routine with that name: *`#{name}`*.\nCall `see routines` to see added routines", dest
@@ -24,5 +27,6 @@ class SlackSmartBot
24
27
  else
25
28
  respond "Only admin users can delete routines", dest
26
29
  end
30
+ @logger.info "fin:#{Time.now}"
27
31
  end
28
32
  end
@@ -11,8 +11,8 @@ class SlackSmartBot
11
11
  # helpadmin:
12
12
 
13
13
  def run_routine(dest, from, name)
14
- if ADMIN_USERS.include?(from) #admin user
15
- if !ON_MASTER_BOT and dest[0] == "D"
14
+ if config.admins.include?(from) #admin user
15
+ if !config.on_master_bot and dest[0] == "D"
16
16
  respond "It's only possible to run routines from MASTER channel from a direct message with the bot.", dest
17
17
  elsif @routines.key?(@channel_id) and @routines[@channel_id].key?(name)
18
18
  if @routines[@channel_id][name][:file_path] != ""
@@ -7,11 +7,11 @@ class SlackSmartBot
7
7
  # helpadmin: You can use this command only if you are an admin user
8
8
  # helpadmin:
9
9
  def see_routines(dest, from, user, all)
10
- if ADMIN_USERS.include?(from) #admin user
10
+ if config.admins.include?(from) #admin user
11
11
  if all
12
12
  routines = {}
13
- if ON_MASTER_BOT
14
- Dir["./routines/routines_*.rb"].each do |rout|
13
+ if config.on_master_bot
14
+ Dir["#{config.path}/routines/routines_*.rb"].each do |rout|
15
15
  file_conf = IO.readlines(rout).join
16
16
  unless file_conf.to_s() == ""
17
17
  routines.merge!(eval(file_conf))
@@ -23,7 +23,7 @@ class SlackSmartBot
23
23
  end
24
24
  else
25
25
  if @rules_imported.key?(user.id) and @rules_imported[user.id].key?(user.id) and dest[0] == "D"
26
- file_conf = IO.readlines("./routines/routines_#{@rules_imported[user.id][user.id]}").join
26
+ file_conf = IO.readlines("#{config.path}/routines/routines_#{@rules_imported[user.id][user.id]}.rb").join
27
27
  routines = eval(file_conf)
28
28
  else
29
29
  routines = @routines.deep_copy
@@ -46,7 +46,7 @@ class SlackSmartBot
46
46
  msg << "\tNext Run: #{v[:next_run]}"
47
47
  msg << "\tLast Run: #{v[:last_run]}"
48
48
  msg << "\tTime consumed on last run: #{v[:last_elapsed]}" unless v[:command] !=''
49
- msg << "\tCommand: #{v[:command]}" unless v[:command] == ''
49
+ msg << "\tCommand: #{v[:command]}" unless v[:command].to_s.strip == ''
50
50
  msg << "\tFile: #{v[:file_path]}" unless v[:file_path] == ''
51
51
  respond msg.join("\n"), dest
52
52
  end
@@ -7,11 +7,11 @@ class SlackSmartBot
7
7
  # helpadmin: You can use this command only if you are an admin user
8
8
  # helpadmin:
9
9
  def start_bot(dest, from)
10
- if ADMIN_USERS.include?(from) #admin user
10
+ if config.admins.include?(from) #admin user
11
11
  respond "This bot is running and listening from now on. You can pause again: pause this bot", dest
12
12
  @status = :on
13
- unless ON_MASTER_BOT
14
- send_msg_channel MASTER_CHANNEL, "Changed status on #{CHANNEL} to :on"
13
+ unless config.on_master_bot
14
+ send_msg_channel config.master_channel, "Changed status on #{config.channel} to :on"
15
15
  end
16
16
  else
17
17
  respond "Only admin users can change my status", dest
@@ -10,8 +10,8 @@ class SlackSmartBot
10
10
  # helpadmin:
11
11
 
12
12
  def start_routine(dest, from, name)
13
- if ADMIN_USERS.include?(from) #admin user
14
- if !ON_MASTER_BOT and dest[0] == "D"
13
+ if config.admins.include?(from) #admin user
14
+ if !config.on_master_bot and dest[0] == "D"
15
15
  respond "It's only possible to start routines from MASTER channel from a direct message with the bot.", dest
16
16
  elsif @routines.key?(@channel_id) and @routines[@channel_id].key?(name)
17
17
  @routines[@channel_id][name][:status] = :on
@@ -7,14 +7,14 @@ class SlackSmartBot
7
7
 
8
8
  def stop_using_rules_on(dest, user, from, channel, typem)
9
9
  unless typem == :on_extended
10
- if !ADMIN_USERS.include?(from) #not admin
11
- respond "Only admins can extend or stop using the rules. Admins on this channel: #{ADMIN_USERS}", dest
10
+ if !config.admins.include?(from) #not admin
11
+ respond "Only admins can extend or stop using the rules. Admins on this channel: #{config.admins}", dest
12
12
  else
13
13
  get_bots_created()
14
14
  if @bots_created[@channel_id][:extended].include?(channel)
15
15
  @bots_created[@channel_id][:extended].delete(channel)
16
16
  update_bots_file()
17
- respond "<@#{user.id}> removed the access to the rules of #{CHANNEL} from #{channel}.", @master_bot_id
17
+ respond "<@#{user.id}> removed the access to the rules of #{config.channel} from #{channel}.", @master_bot_id
18
18
  if @channels_id[channel][0] == "G"
19
19
  respond "The rules won't be accessible from *#{channel}* from now on.", dest
20
20
  else
@@ -9,10 +9,10 @@ class SlackSmartBot
9
9
  unless typem == :on_extended
10
10
  deleted = false
11
11
 
12
- if !ADMIN_USERS.include?(from) and @shortcuts[:all].include?(shortcut) and !@shortcuts[from].include?(shortcut)
12
+ if !config.admins.include?(from) and @shortcuts[:all].include?(shortcut) and !@shortcuts[from].include?(shortcut)
13
13
  respond "Only the creator of the shortcut or an admin user can delete it", dest
14
14
  elsif (@shortcuts.keys.include?(from) and @shortcuts[from].keys.include?(shortcut)) or
15
- (ADMIN_USERS.include?(from) and @shortcuts[:all].include?(shortcut))
15
+ (config.admins.include?(from) and @shortcuts[:all].include?(shortcut))
16
16
  #are you sure? to avoid deleting by mistake
17
17
  unless @questions.keys.include?(from)
18
18
  ask("are you sure you want to delete it?", command, from, dest)
@@ -14,7 +14,7 @@ class SlackSmartBot
14
14
  code.include?("ENV") or code.match?(/=\s*IO/)
15
15
  unless rules_file.empty?
16
16
  begin
17
- eval(File.new(rules_file).read) if File.exist?(rules_file)
17
+ eval(File.new(config.path+rules_file).read) if File.exist?(config.path+rules_file)
18
18
  end
19
19
  end
20
20
 
@@ -13,7 +13,7 @@ class SlackSmartBot
13
13
  end
14
14
  respond("I didn't find any command starting by `#{help_command}`", dest) unless help_found
15
15
  else
16
- message = "-\n\n\n===================================\n*Rules from channel #{CHANNEL}*\n"
16
+ message = "-\n\n\n===================================\n*Rules from channel #{config.channel}*\n"
17
17
  if typem == :on_extended
18
18
  message += "To run the commands on this extended channel, add `!` before the command.\n"
19
19
  end
@@ -23,7 +23,7 @@ class SlackSmartBot
23
23
 
24
24
  unless rules_file.empty?
25
25
  begin
26
- eval(File.new(rules_file).read) if File.exist?(rules_file)
26
+ eval(File.new(config.path+rules_file).read) if File.exist?(config.path+rules_file)
27
27
  end
28
28
  end
29
29
  if defined?(git_project) and git_project.to_s != "" and help_message_rules != "" and help_command.to_s == ""
@@ -5,7 +5,7 @@ class SlackSmartBot
5
5
  # helpmaster: Only works if you are on Master channel and you created that bot or you are an admin user
6
6
  # helpmaster:
7
7
  def kill_bot_on_channel(dest, from, channel)
8
- if ON_MASTER_BOT
8
+ if config.on_master_bot
9
9
  get_channels_name_and_id() unless @channels_name.keys.include?(channel) or @channels_id.keys.include?(channel)
10
10
  channel_id = nil
11
11
  if @channels_name.key?(channel) #it is an id
@@ -8,8 +8,8 @@ class SlackSmartBot
8
8
  # helpadmin: You can use this command only if you are an admin user and you are on the master channel
9
9
  # helpadmin:
10
10
  def exit_bot(command, from, dest, display_name)
11
- if ON_MASTER_BOT
12
- if ADMIN_USERS.include?(from) #admin user
11
+ if config.on_master_bot
12
+ if config.admins.include?(from) #admin user
13
13
  unless @questions.keys.include?(from)
14
14
  ask("are you sure?", command, from, dest)
15
15
  else
@@ -24,7 +24,13 @@ class SlackSmartBot
24
24
  }
25
25
  update_bots_file()
26
26
  sleep 0.5
27
- exit!
27
+ if config.simulate #jal
28
+ @status = :off
29
+ config.simulate = false
30
+ Thread.exit
31
+ else
32
+ exit!
33
+ end
28
34
  when /no/i, /nope/i, /cancel/i
29
35
  @questions.delete(from)
30
36
  respond "Thanks, I'm happy to be alive", dest
@@ -9,8 +9,8 @@ class SlackSmartBot
9
9
  # helpmaster: Only works if you are on Master channel and you are a master admin user
10
10
  # helpmaster:
11
11
  def notify_message(dest, from, where, message)
12
- if ON_MASTER_BOT
13
- if ADMIN_USERS.include?(from) #admin user
12
+ if config.on_master_bot
13
+ if config.admins.include?(from) #admin user
14
14
  if where.nil? #not all and not channel
15
15
  @bots_created.each do |k, v|
16
16
  respond message, k
@@ -19,7 +19,7 @@ class SlackSmartBot
19
19
  elsif where == "all" #all
20
20
  myconv = client.web_client.users_conversations(exclude_archived: true, limit: 100, types: "im, public_channel,private_channel").channels
21
21
  myconv.each do |c|
22
- respond message, c.id unless c.name == MASTER_CHANNEL
22
+ respond message, c.id unless c.name == config.master_channel
23
23
  end
24
24
  respond "Channels and users have been notified", dest
25
25
  else #channel
@@ -8,7 +8,7 @@ class SlackSmartBot
8
8
  # helpmaster: follow the instructions in case creating cloud bots
9
9
  # helpmaster:
10
10
  def create_bot(dest, from, cloud, channel)
11
- if ON_MASTER_BOT
11
+ if config.on_master_bot
12
12
  get_channels_name_and_id() unless @channels_name.keys.include?(channel) or @channels_id.keys.include?(channel)
13
13
  channel_id = nil
14
14
  if @channels_name.key?(channel) #it is an id
@@ -28,7 +28,7 @@ class SlackSmartBot
28
28
 
29
29
  if channel_id.nil?
30
30
  respond "There is no channel with that name: #{channel}, please be sure is written exactly the same", dest
31
- elsif channel == MASTER_CHANNEL
31
+ elsif channel == config.master_channel
32
32
  respond "There is already a bot in this channel: #{channel}", dest
33
33
  elsif @bots_created.keys.include?(channel_id)
34
34
  respond "There is already a bot in this channel: #{channel}, kill it before", dest
@@ -41,24 +41,24 @@ class SlackSmartBot
41
41
  if defined?(RULES_FOLDER)
42
42
  rules_file = RULES_FOLDER + rules_file
43
43
  else
44
- Dir.mkdir("rules") unless Dir.exist?("rules")
45
- Dir.mkdir("rules/#{channel_id}") unless Dir.exist?("rules/#{channel_id}")
46
- rules_file = "./rules/#{channel_id}/" + rules_file
44
+ Dir.mkdir("#{config.path}/rules") unless Dir.exist?("#{config.path}/rules")
45
+ Dir.mkdir("#{config.path}/rules/#{channel_id}") unless Dir.exist?("#{config.path}/rules/#{channel_id}")
46
+ rules_file = "/rules/#{channel_id}/" + rules_file
47
47
  end
48
48
  default_rules = (__FILE__).gsub(/slack\/smart-bot\/commands\/on_master\/create_bot\.rb$/, "slack-smart-bot_rules.rb")
49
- File.delete(rules_file) if File.exist?(rules_file)
50
- FileUtils.copy_file(default_rules, rules_file) unless File.exist?(rules_file)
49
+ File.delete(config.path + rules_file) if File.exist?(config.path + rules_file)
50
+ FileUtils.copy_file(default_rules, config.path + rules_file) unless File.exist?(config.path + rules_file)
51
51
  admin_users = Array.new()
52
52
  creator_info = client.web_client.users_info(user: channel_found.creator)
53
- admin_users = [from, creator_info.user.name] + MASTER_USERS
53
+ admin_users = [from, creator_info.user.name] + config.masters
54
54
  admin_users.uniq!
55
- @logger.info "ruby #{$0} \"#{channel}\" \"#{admin_users.join(",")}\" \"#{rules_file}\" on"
55
+ @logger.info "ruby #{config.file_path} \"#{channel}\" \"#{admin_users.join(",")}\" \"#{rules_file}\" on"
56
56
 
57
57
  if cloud
58
- respond "Copy the bot folder to your cloud location and run `ruby #{$0} \"#{channel}\" \"#{admin_users.join(",")}\" \"#{rules_file}\" on&`", dest
58
+ respond "Copy the bot folder to your cloud location and run `ruby #{config.file} \"#{channel}\" \"#{admin_users.join(",")}\" \"#{rules_file}\" on&`", dest
59
59
  else
60
60
  t = Thread.new do
61
- `ruby #{$0} \"#{channel}\" \"#{admin_users.join(",")}\" \"#{rules_file}\" on`
61
+ `ruby #{config.file_path} \"#{channel}\" \"#{admin_users.join(",")}\" \"#{rules_file}\" on`
62
62
  end
63
63
  end
64
64
  @bots_created[channel_id] = {
@@ -4,7 +4,7 @@ class SlackSmartBot
4
4
  @pings = []
5
5
  get_bots_created()
6
6
  @buffer_complete = [] unless defined?(@buffer_complete)
7
- b = File.read("./buffer_complete.log")
7
+ b = File.read("#{config.path}/buffer_complete.log")
8
8
  result = b.scan(/^\|(\w+)\|(\w+)\|([^$]+)\$\$\$/m)
9
9
  result.delete(nil)
10
10
  new_messages = result[@buffer_complete.size..-1]
@@ -4,9 +4,9 @@ class SlackSmartBot
4
4
  rules_file = ""
5
5
 
6
6
  if typem == :on_call
7
- rules_file = RULES_FILE
7
+ rules_file = config.rules_file
8
8
  elsif dest[0] == "C" or dest[0] == "G" # on a channel or private channel
9
- rules_file = RULES_FILE
9
+ rules_file = config.rules_file
10
10
 
11
11
  if @rules_imported.key?(user.id) and @rules_imported[user.id].key?(dchannel)
12
12
  unless @bots_created.key?(@rules_imported[user.id][dchannel])
@@ -29,17 +29,23 @@ class SlackSmartBot
29
29
  begin
30
30
  case text
31
31
  when /^Bot has been (closed|killed) by/i
32
- if CHANNEL == @channels_name[dchannel]
32
+ if config.channel == @channels_name[dchannel]
33
33
  @logger.info "#{nick}: #{text}"
34
- exit!
34
+ if config.simulate #jal
35
+ @status = :off
36
+ config.simulate = false
37
+ Thread.exit
38
+ else
39
+ exit!
40
+ end
35
41
  end
36
42
  when /^Changed status on (.+) to :(.+)/i
37
43
  channel_name = $1
38
44
  status = $2
39
- if ON_MASTER_BOT or CHANNEL == channel_name
45
+ if config.on_master_bot or config.channel == channel_name
40
46
  @bots_created[@channels_id[channel_name]][:status] = status.to_sym
41
47
  update_bots_file()
42
- if CHANNEL == channel_name
48
+ if config.channel == channel_name
43
49
  @logger.info "#{nick}: #{text}"
44
50
  else #on master bot
45
51
  @logger.info "Changed status on #{channel_name} to :#{status}"
@@ -48,7 +54,7 @@ class SlackSmartBot
48
54
  when /extended the rules from (.+) to be used on (.+)\.$/i
49
55
  from_name = $1
50
56
  to_name = $2
51
- if ON_MASTER_BOT and @bots_created[@channels_id[from_name]][:cloud]
57
+ if config.on_master_bot and @bots_created[@channels_id[from_name]][:cloud]
52
58
  @bots_created[@channels_id[from_name]][:extended] << to_name
53
59
  @bots_created[@channels_id[from_name]][:extended].uniq!
54
60
  update_bots_file()
@@ -56,7 +62,7 @@ class SlackSmartBot
56
62
  when /removed the access to the rules of (.+) from (.+)\.$/i
57
63
  from_name = $1
58
64
  to_name = $2
59
- if ON_MASTER_BOT and @bots_created[@channels_id[from_name]][:cloud]
65
+ if config.on_master_bot and @bots_created[@channels_id[from_name]][:cloud]
60
66
  @bots_created[@channels_id[from_name]][:extended].delete(to_name)
61
67
  update_bots_file()
62
68
  end
@@ -131,7 +137,7 @@ class SlackSmartBot
131
137
  end
132
138
  unless rules_file.empty?
133
139
  begin
134
- eval(File.new(rules_file).read) if File.exist?(rules_file)
140
+ eval(File.new(config.path+rules_file).read) if File.exist?(config.path+rules_file)
135
141
  rescue Exception => stack
136
142
  @logger.fatal "ERROR ON RULES FILE: #{rules_file}"
137
143
  @logger.fatal stack
@@ -154,7 +160,7 @@ class SlackSmartBot
154
160
  if @bots_created.key?(@rules_imported[user.id][user.id])
155
161
  if @bots_created[@rules_imported[user.id][user.id]][:status] == :on
156
162
  begin
157
- eval(File.new(rules_file).read) if File.exist?(rules_file)
163
+ eval(File.new(config.path+rules_file).read) if File.exist?(config.path+rules_file)
158
164
  rescue Exception => stack
159
165
  @logger.fatal "ERROR ON imported RULES FILE: #{rules_file}"
160
166
  @logger.fatal stack
@@ -1,7 +1,7 @@
1
1
  class SlackSmartBot
2
2
  def treat_message(data)
3
- if config[:testing] and ON_MASTER_BOT
4
- open("./buffer.log", "a") { |f|
3
+ if config[:testing] and config.on_master_bot
4
+ open("#{config.path}/buffer.log", "a") { |f|
5
5
  f.puts "|#{data.channel}|#{data.user}|#{data.text}"
6
6
  }
7
7
  end
@@ -15,7 +15,7 @@ class SlackSmartBot
15
15
  if !data.files.nil? and data.files.size == 1 and data.text.to_s == "" and data.files[0].filetype == "ruby"
16
16
  data.text = "ruby"
17
17
  end
18
- if !dest.nil? and ON_MASTER_BOT and !data.text.nil? and data.text.match(/^ping from (.+)\s*$/) and data.user == config[:nick_id]
18
+ if !dest.nil? and config.on_master_bot and !data.text.nil? and data.text.match(/^ping from (.+)\s*$/) and data.user == config[:nick_id]
19
19
  @pings << $1
20
20
  end
21
21
  typem = :dont_treat
@@ -29,7 +29,7 @@ class SlackSmartBot
29
29
  typem = :on_call
30
30
  end
31
31
  elsif dest == @master_bot_id
32
- if ON_MASTER_BOT #only to be treated on master mot channel
32
+ if config.on_master_bot #only to be treated on master mot channel
33
33
  typem = :on_master
34
34
  end
35
35
  elsif @bots_created.key?(dest)
@@ -37,15 +37,15 @@ class SlackSmartBot
37
37
  typem = :on_bot
38
38
  end
39
39
  elsif dest[0] == "D" #Direct message
40
- if ON_MASTER_BOT #only to be treated by master bot
40
+ if config.on_master_bot #only to be treated by master bot
41
41
  typem = :on_dm
42
42
  end
43
43
  elsif dest[0] == "C" or dest[0] == "G"
44
44
  #only to be treated on the channel of the bot. excluding running ruby
45
- if !ON_MASTER_BOT and @bots_created.key?(@channel_id) and @bots_created[@channel_id][:extended].include?(@channels_name[dest]) and
45
+ if !config.on_master_bot and @bots_created.key?(@channel_id) and @bots_created[@channel_id][:extended].include?(@channels_name[dest]) and
46
46
  !data.text.match?(/^!?\s*(ruby|code)\s+/)
47
47
  typem = :on_extended
48
- elsif ON_MASTER_BOT and data.text.match?(/^!?\s*(ruby|code)\s+/) #or in case of running ruby, the master bot
48
+ elsif config.on_master_bot and data.text.match?(/^!?\s*(ruby|code)\s+/) #or in case of running ruby, the master bot
49
49
  @bots_created.each do |k, v|
50
50
  if v.key?(:extended) and v[:extended].include?(@channels_name[dest])
51
51
  typem = :on_extended
@@ -53,7 +53,7 @@ class SlackSmartBot
53
53
  end
54
54
  end
55
55
  end
56
- if dest[0] == "G" and ON_MASTER_BOT and typem != :on_extended #private group
56
+ if dest[0] == "G" and config.on_master_bot and typem != :on_extended #private group
57
57
  typem = :on_pg
58
58
  end
59
59
  end
@@ -104,7 +104,7 @@ class SlackSmartBot
104
104
  members = client.web_client.conversations_members(channel: @channels_id[channel_rules_name]).members unless channel_found.nil?
105
105
  if channel_found.nil?
106
106
  @logger.fatal "Not possible to find the channel #{channel_rules_name}"
107
- elsif channel_found.name == MASTER_CHANNEL
107
+ elsif channel_found.name == config.master_channel
108
108
  respond "You cannot use the rules from Master Channel on any other channel.", dest
109
109
  elsif @status != :on
110
110
  respond "The bot in that channel is not :on", dest
@@ -113,11 +113,11 @@ class SlackSmartBot
113
113
  else
114
114
  respond "You need to join the channel <##{channel_found.id}> to be able to use the rules.", dest
115
115
  end
116
- elsif ON_MASTER_BOT and typem == :on_extended and
116
+ elsif config.on_master_bot and typem == :on_extended and
117
117
  command.size > 0 and command[0] != "-"
118
118
  # to run ruby only from the master bot for the case more than one extended
119
119
  res = process_first(user_info.user, command, dest, @channel_id, typem, data.files)
120
- elsif !ON_MASTER_BOT and @bots_created[@channel_id].key?(:extended) and
120
+ elsif !config.on_master_bot and @bots_created[@channel_id].key?(:extended) and
121
121
  @bots_created[@channel_id][:extended].include?(@channels_name[data.channel]) and
122
122
  command.size > 0 and command[0] != "-"
123
123
  res = process_first(user_info.user, command, dest, @channel_id, typem, data.files)
@@ -130,9 +130,9 @@ class SlackSmartBot
130
130
  @logger.fatal stack
131
131
  end
132
132
  else
133
- if !ON_MASTER_BOT and !dest.nil? and (dest == @master_bot_id or dest[0] == "D") and
133
+ if !config.on_master_bot and !dest.nil? and (dest == @master_bot_id or dest[0] == "D") and
134
134
  data.text.match?(/^\s*bot\s+status\s*$/i) and @admin_users_id.include?(data.user)
135
- respond "ping from #{CHANNEL}", dest
135
+ respond "ping from #{config.channel}", dest
136
136
  end
137
137
  end
138
138
  end
@@ -1,37 +1,40 @@
1
1
  class SlackSmartBot
2
2
  def update_bots_file
3
- file = File.open($0.gsub(".rb", "_bots.rb"), "w")
3
+ file = File.open(config.file_path.gsub(".rb", "_bots.rb"), "w")
4
4
  bots_created = @bots_created.dup
5
- bots_created.each { |k, v| v[:thread] = "" }
5
+ bots_created.each { |k, v|
6
+ v[:thread] = ""
7
+ }
6
8
  file.write bots_created.inspect
7
9
  file.close
8
10
  end
9
11
 
10
12
  def get_bots_created
11
- if File.exist?($0.gsub(".rb", "_bots.rb"))
12
- if !defined?(@datetime_bots_created) or @datetime_bots_created != File.mtime($0.gsub(".rb", "_bots.rb"))
13
- file_conf = IO.readlines($0.gsub(".rb", "_bots.rb")).join
13
+ if File.exist?(config.file_path.gsub(".rb", "_bots.rb"))
14
+ if !defined?(@datetime_bots_created) or @datetime_bots_created != File.mtime(config.file_path.gsub(".rb", "_bots.rb"))
15
+ file_conf = IO.readlines(config.file_path.gsub(".rb", "_bots.rb")).join
14
16
  if file_conf.to_s() == ""
15
17
  @bots_created = {}
16
18
  else
17
19
  @bots_created = eval(file_conf)
18
20
  end
19
- @datetime_bots_created = File.mtime($0.gsub(".rb", "_bots.rb"))
20
- @bots_created.each do |k, v| # to be compatible with old versions
21
+ @datetime_bots_created = File.mtime(config.file_path.gsub(".rb", "_bots.rb"))
22
+ @bots_created.each do |k, v|
21
23
  v[:extended] = [] unless v.key?(:extended)
24
+ v[:rules_file].gsub!(/^\./, '')
22
25
  end
23
26
  end
24
27
  end
25
28
  end
26
29
 
27
30
  def update_shortcuts_file
28
- file = File.open("./shortcuts/#{SHORTCUTS_FILE}", "w")
31
+ file = File.open("#{config.path}/shortcuts/#{config.shortcuts_file}", "w")
29
32
  file.write @shortcuts.inspect
30
33
  file.close
31
34
  end
32
35
 
33
36
  def update_rules_imported
34
- file = File.open("./rules/rules_imported.rb", "w")
37
+ file = File.open("#{config.path}/rules/rules_imported.rb", "w")
35
38
  file.write @rules_imported.inspect
36
39
  file.close
37
40
  end
@@ -55,8 +58,8 @@ class SlackSmartBot
55
58
  end
56
59
 
57
60
  def get_routines(channel = @channel_id)
58
- if File.exist?("./routines/routines_#{channel}.rb")
59
- file_conf = IO.readlines("./routines/routines_#{channel}.rb").join
61
+ if File.exist?("#{config.path}/routines/routines_#{channel}.rb")
62
+ file_conf = IO.readlines("#{config.path}/routines/routines_#{channel}.rb").join
60
63
  unless file_conf.to_s() == ""
61
64
  @routines = eval(file_conf)
62
65
  end
@@ -64,7 +67,7 @@ class SlackSmartBot
64
67
  end
65
68
 
66
69
  def update_routines(channel = @channel_id)
67
- file = File.open("./routines/routines_#{channel}.rb", "w")
70
+ file = File.open("#{config.path}/routines/routines_#{channel}.rb", "w")
68
71
  file.write (@routines.inspect)
69
72
  file.close
70
73
  end
@@ -72,6 +75,7 @@ class SlackSmartBot
72
75
  def create_routine_thread(name)
73
76
  t = Thread.new do
74
77
  while @routines.key?(@channel_id) and @routines[@channel_id].key?(name)
78
+ @logger.info "Routine: #{@routines[@channel_id][name].inspect}"
75
79
  started = Time.now
76
80
  if @status == :on and @routines[@channel_id][name][:status] == :on
77
81
  if @routines[@channel_id][name][:file_path].match?(/\.rb$/i)
@@ -175,9 +179,9 @@ class SlackSmartBot
175
179
  :see_routines, :start_routine, :pause_routine, :remove_routine, :run_routine],
176
180
  }
177
181
  # user_type: :admin, :user, :admin_master
178
- if MASTER_USERS.include?(from)
182
+ if config.masters.include?(from)
179
183
  user_type = :admin_master
180
- elsif ADMIN_USERS.include?(from)
184
+ elsif config.admins.include?(from)
181
185
  user_type = :admin
182
186
  else
183
187
  user_type = :user
@@ -185,7 +189,7 @@ class SlackSmartBot
185
189
  # channel_type: :bot, :master_bot, :direct, :extended, :external
186
190
  if dest[0] == "D"
187
191
  channel_type = :direct
188
- elsif ON_MASTER_BOT
192
+ elsif config.on_master_bot
189
193
  channel_type = :master_bot
190
194
  elsif @channel_id != dest
191
195
  channel_type = :extended
@@ -200,7 +204,7 @@ class SlackSmartBot
200
204
  help = @help_messages.deep_copy
201
205
  end
202
206
  if rules_file != ""
203
- help[:rules_file] = IO.readlines(rules_file).join.scan(/#\s*help\s*\w*:(.*)/i).join("\n")
207
+ help[:rules_file] = IO.readlines(config.path+rules_file).join.scan(/#\s*help\s*\w*:(.*)/i).join("\n")
204
208
  end
205
209
 
206
210
  help = remove_hash_keys(help, :admin_master) unless user_type == :admin_master
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: slack-smart-bot
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mario Ruiz
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-10-23 00:00:00.000000000 Z
11
+ date: 2019-10-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: slack-ruby-client