slack-smart-bot 1.8.1 → 1.10.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (92) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +127 -21
  3. data/lib/slack/smart-bot/comm/ask.rb +55 -42
  4. data/lib/slack/smart-bot/comm/dont_understand.rb +2 -2
  5. data/lib/slack/smart-bot/comm/event_hello.rb +34 -0
  6. data/lib/slack/smart-bot/comm/get_channel_members.rb +13 -0
  7. data/lib/slack/smart-bot/comm/get_channels.rb +35 -0
  8. data/lib/slack/smart-bot/comm/get_user_info.rb +20 -0
  9. data/lib/slack/smart-bot/comm/get_users.rb +24 -0
  10. data/lib/slack/smart-bot/comm/react.rb +38 -8
  11. data/lib/slack/smart-bot/comm/respond.rb +219 -48
  12. data/lib/slack/smart-bot/comm/respond_direct.rb +2 -3
  13. data/lib/slack/smart-bot/comm/respond_thread.rb +5 -0
  14. data/lib/slack/smart-bot/comm/send_file.rb +38 -34
  15. data/lib/slack/smart-bot/comm/send_msg_channel.rb +27 -22
  16. data/lib/slack/smart-bot/comm/send_msg_user.rb +58 -33
  17. data/lib/slack/smart-bot/comm/unreact.rb +24 -7
  18. data/lib/slack/smart-bot/comm.rb +7 -1
  19. data/lib/slack/smart-bot/commands/general/add_announcement.rb +32 -0
  20. data/lib/slack/smart-bot/commands/general/bot_help.rb +68 -28
  21. data/lib/slack/smart-bot/commands/general/bot_stats.rb +314 -0
  22. data/lib/slack/smart-bot/commands/general/bot_status.rb +3 -5
  23. data/lib/slack/smart-bot/commands/general/bye_bot.rb +0 -7
  24. data/lib/slack/smart-bot/commands/general/delete_announcement.rb +34 -0
  25. data/lib/slack/smart-bot/commands/general/delete_share.rb +34 -0
  26. data/lib/slack/smart-bot/commands/general/hi_bot.rb +16 -11
  27. data/lib/slack/smart-bot/commands/general/leaderboard.rb +200 -0
  28. data/lib/slack/smart-bot/commands/general/see_announcements.rb +113 -0
  29. data/lib/slack/smart-bot/commands/general/see_favorite_commands.rb +54 -0
  30. data/lib/slack/smart-bot/commands/general/see_shares.rb +41 -0
  31. data/lib/slack/smart-bot/commands/general/see_statuses.rb +78 -0
  32. data/lib/slack/smart-bot/commands/general/share_messages.rb +58 -0
  33. data/lib/slack/smart-bot/commands/general/stop_using_rules.rb +11 -6
  34. data/lib/slack/smart-bot/commands/general/suggest_command.rb +30 -0
  35. data/lib/slack/smart-bot/commands/general/use_rules.rb +13 -16
  36. data/lib/slack/smart-bot/commands/general/whats_new.rb +19 -0
  37. data/lib/slack/smart-bot/commands/general_bot_commands.rb +243 -0
  38. data/lib/slack/smart-bot/commands/on_bot/add_shortcut.rb +67 -38
  39. data/lib/slack/smart-bot/commands/on_bot/admin/add_routine.rb +49 -14
  40. data/lib/slack/smart-bot/commands/on_bot/admin/extend_rules.rb +5 -7
  41. data/lib/slack/smart-bot/commands/on_bot/admin/pause_bot.rb +4 -1
  42. data/lib/slack/smart-bot/commands/on_bot/admin/pause_routine.rb +1 -0
  43. data/lib/slack/smart-bot/commands/on_bot/admin/remove_routine.rb +2 -3
  44. data/lib/slack/smart-bot/commands/on_bot/admin/run_routine.rb +6 -1
  45. data/lib/slack/smart-bot/commands/on_bot/admin/see_result_routine.rb +32 -0
  46. data/lib/slack/smart-bot/commands/on_bot/admin/see_routines.rb +12 -4
  47. data/lib/slack/smart-bot/commands/on_bot/admin/start_bot.rb +4 -1
  48. data/lib/slack/smart-bot/commands/on_bot/admin/start_routine.rb +1 -0
  49. data/lib/slack/smart-bot/commands/on_bot/admin/stop_using_rules_on.rb +2 -0
  50. data/lib/slack/smart-bot/commands/on_bot/admin_master/react_to.rb +32 -0
  51. data/lib/slack/smart-bot/commands/on_bot/admin_master/send_message.rb +24 -0
  52. data/lib/slack/smart-bot/commands/on_bot/delete_repl.rb +3 -5
  53. data/lib/slack/smart-bot/commands/on_bot/delete_shortcut.rb +54 -25
  54. data/lib/slack/smart-bot/commands/on_bot/get_repl.rb +7 -9
  55. data/lib/slack/smart-bot/commands/on_bot/repl.rb +55 -25
  56. data/lib/slack/smart-bot/commands/on_bot/ruby_code.rb +36 -13
  57. data/lib/slack/smart-bot/commands/on_bot/run_repl.rb +5 -7
  58. data/lib/slack/smart-bot/commands/on_bot/see_repls.rb +4 -6
  59. data/lib/slack/smart-bot/commands/on_bot/see_shortcuts.rb +29 -13
  60. data/lib/slack/smart-bot/commands/on_extended/bot_rules.rb +55 -9
  61. data/lib/slack/smart-bot/commands/on_master/admin/kill_bot_on_channel.rb +4 -1
  62. data/lib/slack/smart-bot/commands/on_master/admin_master/exit_bot.rb +5 -3
  63. data/lib/slack/smart-bot/commands/on_master/admin_master/notify_message.rb +2 -1
  64. data/lib/slack/smart-bot/commands/on_master/admin_master/publish_announcements.rb +32 -0
  65. data/lib/slack/smart-bot/commands/on_master/admin_master/set_general_message.rb +38 -0
  66. data/lib/slack/smart-bot/commands/on_master/admin_master/set_maintenance.rb +49 -0
  67. data/lib/slack/smart-bot/commands/on_master/create_bot.rb +30 -21
  68. data/lib/slack/smart-bot/commands.rb +19 -1
  69. data/lib/slack/smart-bot/listen.rb +7 -8
  70. data/lib/slack/smart-bot/process.rb +373 -192
  71. data/lib/slack/smart-bot/process_first.rb +202 -104
  72. data/lib/slack/smart-bot/treat_message.rb +325 -186
  73. data/lib/slack/smart-bot/utils/answer.rb +18 -0
  74. data/lib/slack/smart-bot/utils/answer_delete.rb +15 -0
  75. data/lib/slack/smart-bot/utils/build_help.rb +57 -5
  76. data/lib/slack/smart-bot/utils/create_routine_thread.rb +83 -30
  77. data/lib/slack/smart-bot/utils/get_bots_created.rb +4 -1
  78. data/lib/slack/smart-bot/utils/get_channels_name_and_id.rb +1 -7
  79. data/lib/slack/smart-bot/utils/get_help.rb +87 -35
  80. data/lib/slack/smart-bot/utils/get_shares.rb +12 -0
  81. data/lib/slack/smart-bot/utils/has_access.rb +12 -0
  82. data/lib/slack/smart-bot/utils/save_stats.rb +23 -8
  83. data/lib/slack/smart-bot/utils/save_status.rb +52 -0
  84. data/lib/slack/smart-bot/utils/update_shortcuts_file.rb +6 -0
  85. data/lib/slack/smart-bot/utils.rb +5 -0
  86. data/lib/slack-smart-bot.rb +88 -47
  87. data/lib/slack-smart-bot_general_commands.rb +46 -0
  88. data/lib/slack-smart-bot_general_rules.rb +5 -2
  89. data/lib/slack-smart-bot_rules.rb +49 -23
  90. data/whats_new.txt +36 -0
  91. metadata +44 -13
  92. data/lib/slack/smart-bot/commands/on_bot/admin_master/bot_stats.rb +0 -195
@@ -0,0 +1,12 @@
1
+ class SlackSmartBot
2
+
3
+ def get_shares()
4
+ channel = @channels_name[@channel_id]
5
+ if File.exist?("#{config.path}/shares/#{channel}.csv")
6
+ "#{config.path}/shares/#{channel}.csv"
7
+ t = CSV.table("#{config.path}/shares/#{channel}.csv", headers: ['share_id', 'user_deleted', 'user_created', 'date', 'time', 'type', 'to_channel', 'condition'])
8
+ t.delete_if {|row| row[:user_deleted] != ''}
9
+ @shares[channel] = t
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ class SlackSmartBot
2
+ def has_access?(method, user=nil)
3
+ user = Thread.current[:user] if user.nil?
4
+ if config[:allow_access].key?(method) and !config[:allow_access][method].include?(user.name) and !config[:allow_access][method].include?(user.id) and
5
+ (!user.key?(:enterprise_user) or ( user.key?(:enterprise_user) and !config[:allow_access][method].include?(user[:enterprise_user].id)))
6
+ respond "You don't have access to use this command, please contact an Admin to be able to use it: <@#{config.admins.join(">, <@")}>"
7
+ return false
8
+ else
9
+ return true
10
+ end
11
+ end
12
+ end
@@ -1,6 +1,6 @@
1
1
  class SlackSmartBot
2
2
 
3
- def save_stats(method)
3
+ def save_stats(method, data: {})
4
4
  if config.stats
5
5
  begin
6
6
  require 'csv'
@@ -9,18 +9,33 @@ class SlackSmartBot
9
9
  csv << ['date','bot_channel', 'bot_channel_id', 'dest_channel', 'dest_channel_id', 'type_message', 'user_name', 'user_id', 'text', 'command', 'files']
10
10
  end
11
11
  end
12
- dest = Thread.current[:dest]
13
- typem = Thread.current[:typem]
14
- user = Thread.current[:user]
15
- files = Thread.current[:files?]
16
- if method.to_s == 'ruby_code' and files
12
+ if data.empty?
13
+ data = {
14
+ dest: Thread.current[:dest],
15
+ typem: Thread.current[:typem],
16
+ user: Thread.current[:user],
17
+ files: Thread.current[:files?],
18
+ command: Thread.current[:command],
19
+ routine: Thread.current[:routine]
20
+ }
21
+ end
22
+ if method.to_s == 'ruby_code' and data.files
17
23
  command_txt = 'ruby'
18
24
  else
19
- command_txt = Thread.current[:command]
25
+ command_txt = data.command
20
26
  end
27
+ command_txt.gsub!(/```.+```/m,'```CODE```')
28
+ command_txt = "#{command_txt[0..99]}..." if command_txt.size > 100
21
29
 
30
+ if data.routine
31
+ user_name = "routine/#{data.user.name}"
32
+ user_id = "routine/#{data.user.id}"
33
+ else
34
+ user_name = data.user.name
35
+ user_id = data.user.id
36
+ end
22
37
  CSV.open("#{config.stats_path}.#{Time.now.strftime("%Y-%m")}.log", "a+") do |csv|
23
- csv << [Time.now, config.channel, @channel_id, @channels_name[dest], dest, typem, user.name, user.id, command_txt, method, files]
38
+ csv << [Time.now, config.channel, @channel_id, @channels_name[data.dest], data.dest, data.typem, user_name, user_id, command_txt, method, data.files]
24
39
  end
25
40
  rescue Exception => exception
26
41
  @logger.fatal "There was a problem on the stats"
@@ -0,0 +1,52 @@
1
+ class SlackSmartBot
2
+
3
+ def save_status(status, status_id, message)
4
+ require 'csv'
5
+ Dir.mkdir("#{config.path}/status") unless Dir.exist?("#{config.path}/status")
6
+
7
+ CSV.open("#{config.path}/status/#{config.channel}_status.csv", "a+") do |csv|
8
+ csv << [Time.now.strftime("%Y/%m/%d"), Time.now.strftime("%H:%M:%S"), status, status_id, message]
9
+ end
10
+ if status_id == :disconnected
11
+ Thread.new do
12
+ sleep 50
13
+ @logger.info "check disconnection 50 scs later #{@last_notified_status_id}"
14
+ unless @last_notified_status_id == :connected
15
+ respond ":red_circle: The *SmartBot* on *<##{@channel_id}|#{config.channel}>* is down. An admin will take a look. <@#{config.admins.join(">, <@")}>", config.status_channel
16
+ end
17
+ end
18
+ end
19
+ if @channels_id.is_a?(Hash) and @channels_id.keys.include?(config.status_channel)
20
+ is_back = false
21
+ m = ''
22
+ if (Time.now-@last_status_change) > 20 or !defined?(@last_notified_status_id)
23
+ if status_id == :connected
24
+ if defined?(@last_notified_status_id)
25
+ m = ":exclamation: :large_green_circle: The *SmartBot* on *<##{@channel_id}|#{config.channel}>* was not available for #{(Time.now-@last_status_change).round(0)} secs. *Now it is up and running again.*"
26
+ else
27
+ m = ":large_green_circle: The *SmartBot* on *<##{@channel_id}|#{config.channel}>* is up and running again."
28
+ end
29
+ end
30
+ end
31
+ if status_id == :paused
32
+ m = ":red_circle: #{message} *<##{@channel_id}|#{config.channel}>*"
33
+ elsif status_id == :started
34
+ m = ":large_green_circle: #{message} *<##{@channel_id}|#{config.channel}>*"
35
+ elsif status_id == :killed or status_id == :exited
36
+ m = ":red_circle: #{message}"
37
+ elsif config.on_master_bot and status_id == :maintenance_on
38
+ m = ":red_circle: The *SmartBot* is on maintenance so not possible to attend any request."
39
+ elsif config.on_master_bot and status_id == :maintenance_off
40
+ m = ":large_green_circle: The *SmartBot* is up and running again."
41
+ end
42
+ @last_status_change = Time.now
43
+ @last_notified_status_id = status_id
44
+ unless m == ''
45
+ respond m, config.status_channel
46
+ end
47
+ end
48
+
49
+
50
+ end
51
+
52
+ end
@@ -3,5 +3,11 @@ class SlackSmartBot
3
3
  file = File.open("#{config.path}/shortcuts/#{config.shortcuts_file}", "w")
4
4
  file.write @shortcuts.inspect
5
5
  file.close
6
+
7
+ if config.on_master_bot
8
+ file = File.open("#{config.path}/shortcuts/shortcuts_global.rb", "w")
9
+ file.write @shortcuts_global.inspect
10
+ file.close
11
+ end
6
12
  end
7
13
  end
@@ -4,6 +4,7 @@ require_relative 'utils/get_bots_created'
4
4
  require_relative 'utils/get_channels_name_and_id'
5
5
  require_relative 'utils/get_help'
6
6
  require_relative 'utils/get_routines'
7
+ require_relative 'utils/get_shares'
7
8
  require_relative 'utils/get_rules_imported'
8
9
  require_relative 'utils/remove_hash_keys'
9
10
  require_relative 'utils/update_bots_file'
@@ -13,4 +14,8 @@ require_relative 'utils/update_shortcuts_file'
13
14
  require_relative 'utils/save_stats'
14
15
  require_relative 'utils/get_repls'
15
16
  require_relative 'utils/update_repls'
17
+ require_relative 'utils/answer'
18
+ require_relative 'utils/answer_delete'
19
+ require_relative 'utils/has_access'
20
+ require_relative 'utils/save_status'
16
21
 
@@ -41,6 +41,11 @@ class SlackSmartBot
41
41
  config[:simulate] = false unless config.key?(:simulate)
42
42
  config[:stats] = false unless config.key?(:stats)
43
43
  config[:allow_access] = Hash.new unless config.key?(:allow_access)
44
+ config[:on_maintenance] = false unless config.key?(:on_maintenance)
45
+ config[:on_maintenance_message] = "Sorry I'm on maintenance so I cannot attend your request." unless config.key?(:on_maintenance_message)
46
+ config[:general_message] = "" unless config.key?(:general_message)
47
+ config[:logrtm] = false unless config.key?(:logrtm)
48
+ config[:status_channel] = 'smartbot-status' unless config.key?(:status_channel)
44
49
 
45
50
  if config.path.to_s!='' and config.file.to_s==''
46
51
  config.file = File.basename($0)
@@ -59,6 +64,9 @@ class SlackSmartBot
59
64
  Dir.mkdir("#{config.path}/logs") unless Dir.exist?("#{config.path}/logs")
60
65
  Dir.mkdir("#{config.path}/shortcuts") unless Dir.exist?("#{config.path}/shortcuts")
61
66
  Dir.mkdir("#{config.path}/routines") unless Dir.exist?("#{config.path}/routines")
67
+ Dir.mkdir("#{config.path}/announcements") unless Dir.exist?("#{config.path}/announcements")
68
+ Dir.mkdir("#{config.path}/shares") unless Dir.exist?("#{config.path}/shares")
69
+ File.delete("#{config.path}/config_tmp.status") if File.exist?("#{config.path}/config_tmp.status")
62
70
 
63
71
  config.masters = MASTER_USERS if config.masters.to_s=='' and defined?(MASTER_USERS)
64
72
  config.master_channel = MASTER_CHANNEL if config.master_channel.to_s=='' and defined?(MASTER_CHANNEL)
@@ -89,7 +97,7 @@ class SlackSmartBot
89
97
  config.on_master_bot = false
90
98
  end
91
99
 
92
- if !config.key?(:token) or config.token.to_s == ''
100
+ if (!config.key?(:token) or config.token.to_s == '') and !config.simulate
93
101
  abort "You need to supply a valid token key on the settings. key: :token"
94
102
  elsif !config.key?(:masters) or !config.masters.is_a?(Array) or config.masters.size == 0
95
103
  abort "You need to supply a masters array on the settings containing the user names of the master admins. key: :masters"
@@ -104,7 +112,8 @@ class SlackSmartBot
104
112
  logfile = File.basename(config.rules_file.gsub("_rules_", "_logs_"), ".rb") + ".log"
105
113
  config.log_file = logfile
106
114
  @logger = Logger.new("#{config.path}/logs/#{logfile}")
107
-
115
+ @last_respond = Time.now
116
+
108
117
  config_log = config.dup
109
118
  config_log.delete(:token)
110
119
  @logger.info "Initializing bot: #{config_log.inspect}"
@@ -113,16 +122,31 @@ class SlackSmartBot
113
122
  File.new("#{config.path}/buffer_complete.log", "w") if config[:simulate] and config.on_master_bot
114
123
 
115
124
  self.config = config
125
+ save_status :off, :initializing, "Initializing bot: #{config_log.inspect}"
116
126
 
117
- Slack.configure do |conf|
118
- conf.token = config[:token]
127
+ unless config.simulate and config.key?(:client)
128
+ Slack.configure do |conf|
129
+ conf.token = config[:token]
130
+ end
119
131
  end
120
132
  restarts = 0
121
133
  created = false
122
134
  while restarts < 200 and !created
123
135
  begin
124
136
  @logger.info "Connecting #{config_log.inspect}"
125
- self.client = Slack::RealTime::Client.new(start_method: :rtm_connect)
137
+ save_status :off, :connecting, "Connecting #{config_log.inspect}"
138
+ if config.simulate and config.key?(:client)
139
+ self.client = config.client
140
+ else
141
+ if config.logrtm
142
+ logrtmname = "#{config.path}/logs/rtm_#{config.channel}.log"
143
+ File.delete(logrtmname) if File.exists?(logrtmname)
144
+ @logrtm = Logger.new(logrtmname)
145
+ self.client = Slack::RealTime::Client.new(start_method: :rtm_connect, logger: @logrtm)
146
+ else
147
+ self.client = Slack::RealTime::Client.new(start_method: :rtm_connect)
148
+ end
149
+ end
126
150
  created = true
127
151
  rescue Exception => e
128
152
  restarts += 1
@@ -131,6 +155,8 @@ class SlackSmartBot
131
155
  @logger.fatal "Rescued on creation: #{e.inspect}"
132
156
  @logger.info "Waiting 60 seconds to retry. restarts: #{restarts}"
133
157
  puts "#{Time.now}: Not able to create client. Waiting 60 seconds to retry: #{config_log.inspect}"
158
+ save_status :off, :waiting, "Not able to create client. Waiting 60 seconds to retry: #{config_log.inspect}"
159
+
134
160
  sleep 60
135
161
  else
136
162
  exit!
@@ -143,9 +169,16 @@ class SlackSmartBot
143
169
  @bots_created = Hash.new()
144
170
  @shortcuts = Hash.new()
145
171
  @shortcuts[:all] = Hash.new()
172
+ @shortcuts_global = Hash.new()
173
+ @shortcuts_global[:all] = Hash.new()
146
174
  @rules_imported = Hash.new()
147
175
  @routines = Hash.new()
148
176
  @repls = Hash.new()
177
+ @users = Hash.new()
178
+ @announcements = Hash.new()
179
+ @shares = Hash.new()
180
+ @last_status_change = Time.now
181
+
149
182
 
150
183
  if File.exist?("#{config.path}/shortcuts/#{config.shortcuts_file}")
151
184
  file_sc = IO.readlines("#{config.path}/shortcuts/#{config.shortcuts_file}").join
@@ -153,6 +186,12 @@ class SlackSmartBot
153
186
  @shortcuts = eval(file_sc)
154
187
  end
155
188
  end
189
+ if File.exist?("#{config.path}/shortcuts/shortcuts_global.rb")
190
+ file_sc = IO.readlines("#{config.path}/shortcuts/shortcuts_global.rb").join
191
+ unless file_sc.to_s() == ""
192
+ @shortcuts_global = eval(file_sc)
193
+ end
194
+ end
156
195
 
157
196
  get_routines()
158
197
  get_repls()
@@ -162,10 +201,17 @@ class SlackSmartBot
162
201
  if @bots_created.kind_of?(Hash) and config.start_bots
163
202
  @bots_created.each { |key, value|
164
203
  if !value.key?(:cloud) or (value.key?(:cloud) and value[:cloud] == false)
165
- @logger.info "ruby #{config.file_path} \"#{value[:channel_name]}\" \"#{value[:admins]}\" \"#{value[:rules_file]}\" #{value[:status].to_sym}"
204
+ if value.key?(:silent) and value.silent!=config.silent
205
+ silent = value.silent
206
+ else
207
+ silent = config.silent
208
+ end
209
+ @logger.info "BOT_SILENT=#{silent} ruby #{config.file_path} \"#{value[:channel_name]}\" \"#{value[:admins]}\" \"#{value[:rules_file]}\" #{value[:status].to_sym}"
166
210
  puts "Starting #{value[:channel_name]} Smart Bot"
211
+ save_status :off, :starting, "Starting #{value[:channel_name]} Smart Bot"
212
+
167
213
  t = Thread.new do
168
- `ruby #{config.file_path} \"#{value[:channel_name]}\" \"#{value[:admins]}\" \"#{value[:rules_file]}\" #{value[:status].to_sym}`
214
+ `BOT_SILENT=#{silent} ruby #{config.file_path} \"#{value[:channel_name]}\" \"#{value[:admins]}\" \"#{value[:rules_file]}\" #{value[:status].to_sym}`
169
215
  end
170
216
  value[:thread] = t
171
217
  sleep value[:admins].size
@@ -173,6 +219,12 @@ class SlackSmartBot
173
219
  }
174
220
  end
175
221
  end
222
+ general_rules_file = "/rules/general_rules.rb"
223
+ general_commands_file = "/rules/general_commands.rb"
224
+ default_general_rules = (__FILE__).gsub(/\/slack-smart-bot\.rb$/, "/slack-smart-bot_general_rules.rb")
225
+ default_general_commands = (__FILE__).gsub(/\/slack-smart-bot\.rb$/, "/slack-smart-bot_general_commands.rb")
226
+ FileUtils.copy_file(default_general_rules, config.path + general_rules_file) unless File.exist?(config.path + general_rules_file)
227
+ FileUtils.copy_file(default_general_commands, config.path + general_commands_file) unless File.exist?(config.path + general_commands_file)
176
228
 
177
229
  get_rules_imported()
178
230
 
@@ -181,7 +233,7 @@ class SlackSmartBot
181
233
  @admin_users_id = []
182
234
  @master_admin_users_id = []
183
235
  config.admins.each do |au|
184
- user_info = client.web_client.users_info(user: "@#{au}")
236
+ user_info = get_user_info("@#{au}")
185
237
  @admin_users_id << user_info.user.id
186
238
  if config.masters.include?(au)
187
239
  @master_admin_users_id << user_info.user.id
@@ -189,47 +241,33 @@ class SlackSmartBot
189
241
  sleep 1
190
242
  end
191
243
  (config.masters-config.admins).each do |au|
192
- user_info = client.web_client.users_info(user: "@#{au}")
244
+ user_info = get_user_info("@#{au}")
193
245
  @master_admin_users_id << user_info.user.id
194
246
  sleep 1
195
247
  end
196
248
  rescue Slack::Web::Api::Errors::TooManyRequestsError
197
249
  @logger.fatal "TooManyRequestsError"
250
+ save_status :off, :TooManyRequestsError, "TooManyRequestsError please re run the bot and be sure of executing first: killall ruby"
198
251
  abort("TooManyRequestsError please re run the bot and be sure of executing first: killall ruby")
199
252
  rescue Exception => stack
253
+ pp stack if config.testing
254
+ save_status :off, :wrong_admin_user, "The admin user specified on settings: #{config.admins.join(", ")}, doesn't exist on Slack. Execution aborted"
200
255
  abort("The admin user specified on settings: #{config.admins.join(", ")}, doesn't exist on Slack. Execution aborted")
201
256
  end
202
257
 
203
- client.on :hello do
204
- m = "Successfully connected, welcome '#{client.self.name}' to the '#{client.team.name}' team at https://#{client.team.domain}.slack.com."
205
- puts m
206
- @logger.info m
207
- config.nick = client.self.name
208
- config.nick_id = client.self.id
209
- @salutations = [config[:nick], "<@#{config[:nick_id]}>", "bot", "smart"]
210
-
211
- gems_remote = `gem list slack-smart-bot --remote`
212
- version_remote = gems_remote.to_s().scan(/slack-smart-bot \((\d+\.\d+\.\d+)/).join
213
- version_message = ""
214
- if version_remote != VERSION
215
- version_message = ". There is a new available version: #{version_remote}."
216
- end
217
- if !config[:silent] or ENV['BOT_SILENT'].to_s == 'false'
218
- ENV['BOT_SILENT'] = 'true' if config[:silent] == 'true' and ENV['BOT_SILENT'].to_s != 'true'
219
- respond "Smart Bot started v#{VERSION}#{version_message}\nIf you want to know what I can do for you: `bot help`.\n`bot rules` if you want to display just the specific rules of this channel.\nYou can talk to me privately if you prefer it."
220
- end
221
- @routines.each do |ch, rout|
222
- rout.each do |k, v|
223
- if !v[:running] and v[:channel_name] == config.channel
224
- create_routine_thread(k)
225
- end
226
- end
258
+ if config.simulate and config.key?(:client)
259
+ event_hello()
260
+ else
261
+ client.on :hello do
262
+ event_hello()
227
263
  end
228
264
  end
229
265
 
230
266
  @status = config.status_init
231
267
  @questions = Hash.new()
268
+ @answer = Hash.new()
232
269
  @repl_sessions = Hash.new()
270
+ @datetime_general_commands = 0
233
271
  @channels_id = Hash.new()
234
272
  @channels_name = Hash.new()
235
273
  get_channels_name_and_id()
@@ -238,6 +276,8 @@ class SlackSmartBot
238
276
 
239
277
  get_routines()
240
278
  get_repls()
279
+ get_shares()
280
+
241
281
  if @routines.key?(@channel_id)
242
282
  @routines[@channel_id].each do |k, v|
243
283
  @routines[@channel_id][k][:running] = false
@@ -249,24 +289,25 @@ class SlackSmartBot
249
289
  @routines.each do |ch, rout|
250
290
  rout.each do |k, v|
251
291
  if !v[:running] and v[:channel_name] == config.channel
252
- create_routine_thread(k)
292
+ create_routine_thread(k, v)
253
293
  end
254
294
  end
255
295
  end
296
+ else
297
+ client.on :close do |_data|
298
+ m = "Connection closing, exiting. #{Time.now}"
299
+ @logger.info m
300
+ @logger.info _data
301
+ #save_status :off, :closing, "Connection closing, exiting." #todo: don't notify for the moment, remove when checked
302
+ end
303
+
304
+ client.on :closed do |_data|
305
+ m = "Connection has been disconnected. #{Time.now}"
306
+ @logger.info m
307
+ @logger.info _data
308
+ save_status :off, :disconnected, "Connection has been disconnected."
309
+ end
256
310
  end
257
-
258
- client.on :close do |_data|
259
- m = "Connection closing, exiting. #{Time.now}"
260
- @logger.info m
261
- @logger.info _data
262
- end
263
-
264
- client.on :closed do |_data|
265
- m = "Connection has been disconnected. #{Time.now}"
266
- @logger.info m
267
- @logger.info _data
268
- end
269
-
270
311
  self
271
312
  end
272
313
 
@@ -0,0 +1,46 @@
1
+ # add here the general commands you will be using in any channel where The SmartBot is part of. Not necessary to use ! or ^, it will answer directly.
2
+ def general_commands(user, command, dest, files = [])
3
+
4
+ begin
5
+ case command
6
+
7
+ # help: ----------------------------------------------
8
+ # help: `cls`
9
+ # help: `clear`
10
+ # help: `clear screen`
11
+ # help: `NUMBER cls`
12
+ # help: It will send a big empty message.
13
+ # help: NUMBER (optional): number of lines. Default 100. Max 200.
14
+ # help: command_id: :cls
15
+ # help:
16
+ when /\A\s*(\d*)\s*(clear|cls|clear\s+screen)\s*\z/i
17
+ save_stats :cls
18
+ $1.to_s == '' ? lines = 100 : lines = $1.to_i
19
+ lines = 200 if lines > 200
20
+ respond (">#{"\n"*lines}<")
21
+
22
+
23
+ # this is a hidden command that it is not listed when calling bot help
24
+ when /\A\s*(that's\s+)?(thanks|thank\s+you|I\s+love\s+you|nice|cool)\s+(#{@salutations.join("|")})\s*!*\s*$/i
25
+ save_stats :thanks
26
+ reactions = [:heart, :heart_eyes, :blush, :relaxed, :simple_smile, :smiley, :two_hearts, :heartbeat, :green_heart ]
27
+ reactions.sample(rand(3)+1).each {|rt| react rt }
28
+ responses = ['Thank YOU', "You're welcome", "You're very welcome", 'No problem', 'No worries', "Don't mention it", 'My pleasure',
29
+ 'Anytime', 'It was the least I could do', 'Glad to help', 'Sure', 'Pleasure', 'The pleasure is mine', 'It was nothing', 'Much obliged', "I'm happy to help",
30
+ 'Það var ekkert', 'De nada', 'No hay de qué', 'De rien', 'Bitte', 'Prego', 'मेरा सौभाग्य है', '不客氣', 'Παρακαλώ']
31
+ respond "#{responses.sample}#{'!'*rand(4)}"
32
+
33
+ else
34
+ return false
35
+ end
36
+ return true
37
+ rescue => exception
38
+ if defined?(@logger)
39
+ @logger.fatal exception
40
+ respond "Unexpected error!! Please contact an admin to solve it: <@#{config.admins.join(">, <@")}>"
41
+ else
42
+ puts exception
43
+ end
44
+ return false
45
+ end
46
+ end