slack-smart-bot 1.2.0 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b75a6cea93322f420ba6e0bf6938609a600d889c4788f0e51e4929a075578d15
4
- data.tar.gz: 8d3c0476510dd29d2dfac41eabfceb77f9d604100704815c775c3888002ef35c
3
+ metadata.gz: 184f25cc7baddfde9859cabae8a25e1204fca24b3c85b509d00c5579d86233ff
4
+ data.tar.gz: '08e6e6e647f2ab67f6a56f7022c56469d56f8c768b5dc7f12d3f6cd2338a2334'
5
5
  SHA512:
6
- metadata.gz: 946fbf6bca9a5a704b0b7d1d46e12faeb25e6f7f44e99d867d5a28b07cc2441bac0d93e17d3cdae3f081b4acc8b59092b89b2903fcc9eec6282b84b383d6fb8f
7
- data.tar.gz: c976933c3b0bbb67e9ba86778b2068ff75dd42a7470a1ba48bb08f49ae70415907f560a5454cb3835526732d6a8d3e47b80e68c404d017577428d6f6a791d4ff
6
+ metadata.gz: 320e5c00e0acf8e79414cfb582c3be1009bb790374833fe109009df53dbfbe63739b6148d2499eeb83a0d60c277f1cedad59f4375fff39395e7ce7985406eb79
7
+ data.tar.gz: f21210b0dc5504a700a1f6c9265f42ff4e62a4d7502e5e3da662df76452ce67f7ade3a4d837f1178881ed5511c5f6c96c122239e010fe5d62ff0385696322b93
data/README.md CHANGED
@@ -151,7 +151,7 @@ def rules(user, command, processed, dest)
151
151
 
152
152
  else
153
153
  unless processed
154
- dont_understand()
154
+ dont_understand()
155
155
  end
156
156
  end
157
157
  end
@@ -161,13 +161,7 @@ class SlackSmartBot
161
161
  end
162
162
  end
163
163
 
164
- # rules imported only for DM
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
167
- unless file_conf.to_s() == ""
168
- @rules_imported = eval(file_conf)
169
- end
170
- end
164
+ get_rules_imported()
171
165
 
172
166
  begin
173
167
  user_info = client.web_client.users_info(user: "#{"@" if config[:nick][0] != "@"}#{config[:nick]}")
@@ -118,9 +118,7 @@ def rules(user, command, processed, dest, files = [], rules_file = "")
118
118
 
119
119
  else
120
120
  unless processed
121
- if @channel_id == dest or dest[0] == "D" or dest[0] == "G" #not on extended channels
122
- dont_understand()
123
- end
121
+ dont_understand()
124
122
  end
125
123
  end
126
124
  rescue => exception
@@ -182,29 +182,25 @@ class SlackSmartBot
182
182
  )
183
183
  end
184
184
 
185
- def dont_understand(rules_file = nil, command = nil, user = nil, dest = nil, answer = ["what?", "huh?", "sorry?", "what do you mean?", "I don't understand"])
186
- if command.nil?
187
- command = Thread.current[:command]
188
- end
189
- if user.nil?
190
- user = Thread.current[:user]
191
- end
192
- if dest.nil?
193
- dest = Thread.current[:dest]
194
- end
195
- if rules_file.nil?
196
- rules_file = Thread.current[:rules_file]
185
+ def dont_understand(rules_file = nil, command = nil, user = nil, dest = nil, answer = ["what?", "huh?", "sorry?", "what do you mean?", "I don't understand"], channel_rules: config.channel, typem: nil)
186
+ command = Thread.current[:command] if command.nil?
187
+ user = Thread.current[:user] if user.nil?
188
+ dest = Thread.current[:dest] if dest.nil?
189
+ rules_file = Thread.current[:rules_file] if rules_file.nil?
190
+ typem = Thread.current[:typem] if typem.nil?
191
+ if typem==:on_extended
192
+ get_bots_created()
197
193
  end
198
194
 
199
- if rules_file == ""
200
- res_final = []
201
- else
202
- text = get_help(rules_file, dest, user.name)
195
+ if typem!=:on_extended or (typem==:on_extended and @extended_from[@channels_name[dest]][0]==@channel_id )
196
+ #only will be treated on the first extended channel in case more than one channel extended on DEST channel
197
+ text = get_help(rules_file, dest, user.name, typem==:on_extended)
203
198
 
204
199
  ff = text.scan(/\s*`\s*([^`]+)\s*`\s*/i).flatten
205
200
  ff.delete("!THE_COMMAND")
206
201
  ff.delete("@NAME_OF_BOT THE_COMMAND")
207
202
  ff.delete("NAME_OF_BOT THE_COMMAND")
203
+ ff.delete("@BOT_NAME on #CHANNEL_NAME COMMAND")
208
204
 
209
205
  ff2 = {}
210
206
  acommand = command.split(/\s+/)
@@ -228,12 +224,30 @@ class SlackSmartBot
228
224
  res_final << (ff2.select { |k, v| v == r }).keys
229
225
  end
230
226
  res_final.flatten!
231
- end
232
- if res_final.empty?
233
- resp = answer.sample
234
- respond "#{user.profile.display_name}, #{resp}", dest
235
- else
236
- respond "#{user.profile.display_name}, I don't understand. Maybe you are trying to say:\n`#{res_final[0..4].join("`\n`")}`", dest
227
+
228
+ if typem==:on_extended and @extended_from[@channels_name[dest]][0]==@channel_id
229
+ if rules_file == config.rules_file
230
+ respond "#{user.profile.display_name}, I don't understand.", dest
231
+ end
232
+ if @extended_from[@channels_name[dest]].size > 1 and rules_file == config.rules_file
233
+ @extended_from[@channels_name[dest]][1..-1].each do |ch|
234
+ rf = @bots_created[ch].rules_file
235
+ dont_understand(rf, command, nil, dest, answer, channel_rules: @channels_name[ch], typem: typem)
236
+ end
237
+ end
238
+ unless res_final.empty?
239
+ respond "Similar rules on : *#{channel_rules}*\n`#{res_final[0..4].join("`\n`")}`", dest
240
+ end
241
+ elsif typem!=:on_extended
242
+ message = ''
243
+ message = "\nTake in consideration when on external calls, not all the commands are availalbe." if typem==:on_call
244
+ if res_final.empty?
245
+ resp = answer.sample
246
+ respond "#{user.profile.display_name}, #{resp}#{message}", dest
247
+ else
248
+ respond "#{user.profile.display_name}, I don't understand. Maybe you are trying to say:\n`#{res_final[0..4].join("`\n`")}`#{message}", dest
249
+ end
250
+ end
237
251
  end
238
252
  end
239
253
  end
@@ -26,12 +26,8 @@ class SlackSmartBot
26
26
  end
27
27
  end
28
28
  else
29
- if (dest[0] == "C") || (dest[0] == "G") and @rules_imported.key?(user.id) &&
30
- @rules_imported[user.id].key?(dchannel) && @bots_created.key?(@rules_imported[user.id][dchannel])
31
- message = "*You are using rules from another channel: <##{@rules_imported[user.id][dchannel]}>. These are the specific commands for that channel:*"
32
- elsif dest[0] == "D" && @rules_imported.key?(user.id) && @rules_imported[user.id].key?(user.id) and
33
- @bots_created.key?(@rules_imported[user.id][user.id])
34
- message = "*You are using rules from another channel: <##{@rules_imported[user.id][user.id]}>. These are the specific commands for that channel:*"
29
+ if Thread.current[:using_channel]!=''
30
+ message = "*You are using rules from another channel: <##{Thread.current[:using_channel]}>. These are the specific commands for that channel:*"
35
31
  end
36
32
  respond message, dest
37
33
  end
@@ -13,10 +13,8 @@ class SlackSmartBot
13
13
  if @status == :on
14
14
  greetings = ["Hello", "Hallo", "Hi", "Hola", "What's up", "Hey", "Hæ"].sample
15
15
  respond "#{greetings} #{display_name}", dest
16
- if @rules_imported.key?(user.id) and @rules_imported[user.id].key?(user.id) and dest[0] == "D"
17
- respond "You are using specific rules for channel: <##{@rules_imported[user.id][user.id]}>", dest
18
- elsif @rules_imported.key?(user.id) and @rules_imported[user.id].key?(dchannel) and (dest[0] == "C" or dest[0] == "G")
19
- respond "You are using specific rules for channel: <##{@rules_imported[user.id][dchannel]}>", dest
16
+ if Thread.current[:using_channel]!=''
17
+ respond "You are using specific rules for channel: <##{Thread.current[:using_channel]}>", dest
20
18
  end
21
19
  @listening << from unless @listening.include?(from)
22
20
  end
@@ -9,13 +9,14 @@ class SlackSmartBot
9
9
  else
10
10
  channel_id = channel
11
11
  end
12
+
12
13
  if dest[0] == "C" or dest[0] == "G" #channel
13
14
  if @rules_imported.key?(user.id) and @rules_imported[user.id].key?(dchannel)
14
15
  if @rules_imported[user.id][dchannel] != channel_id
15
16
  respond "You are not using those rules.", dest
16
17
  else
17
18
  @rules_imported[user.id].delete(dchannel)
18
- update_rules_imported() if config.on_master_bot
19
+ update_rules_imported()
19
20
  respond "You won't be using those rules from now on.", dest
20
21
 
21
22
  def git_project() "" end
@@ -30,7 +31,7 @@ class SlackSmartBot
30
31
  respond "You are not using those rules.", dest
31
32
  else
32
33
  @rules_imported[user.id].delete(user.id)
33
- update_rules_imported() if config.on_master_bot
34
+ update_rules_imported()
34
35
  respond "You won't be using those rules from now on.", dest
35
36
 
36
37
  def git_project() "" end
@@ -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 config.on_master_bot
38
+ update_rules_imported()
39
39
  respond "I'm using now the rules from <##{channel_found.id}>", dest
40
40
 
41
41
  def git_project() "" end
@@ -39,7 +39,6 @@ class SlackSmartBot
39
39
  update_shortcuts_file()
40
40
  respond "shortcut added", dest
41
41
  else
42
-
43
42
  #are you sure? to avoid overwriting existing
44
43
  unless @questions.keys.include?(from)
45
44
  ask("The shortcut already exists, are you sure you want to overwrite it?", command, from, dest)
@@ -29,10 +29,9 @@ class SlackSmartBot
29
29
  channels_in_use << v[:channel_name]
30
30
  end
31
31
  end
32
-
33
32
  if channel_found.nil?
34
- respond "The channel you specified doesn't exist", dest
35
- elsif @bots_created.key?(@channels_id[channel])
33
+ respond "The channel you specified doesn't exist or I don't have access to it. Be sure I was invited to that channel.", dest
34
+ elsif @bots_created.key?(@channels_id[channel]) or channel == config.master_channel
36
35
  respond "There is a bot already running on that channel.", dest
37
36
  elsif @bots_created[@channel_id][:extended].include?(channel)
38
37
  respond "The rules are already extended to that channel.", dest
@@ -15,6 +15,7 @@ class SlackSmartBot
15
15
  if !config.on_master_bot and dest[0] == "D"
16
16
  respond "It's only possible to remove 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
+ @routines[@channel_id][name][:thread].exit
18
19
  @routines[@channel_id].delete(name)
19
20
  update_routines()
20
21
  respond "The routine *`#{name}`* has been removed.", dest
@@ -19,14 +19,14 @@ class SlackSmartBot
19
19
  end
20
20
  else
21
21
  respond "To see all routines on all channels you need to run the command on the master channel.\nI'll display only the routines on this channel.", dest
22
- routines = @routines.deep_copy
22
+ routines = @routines.dup
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
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
- routines = @routines.deep_copy
29
+ routines = @routines.dup
30
30
  end
31
31
  end
32
32
 
@@ -7,11 +7,11 @@ class SlackSmartBot
7
7
  help_found = false
8
8
  help_filtered.split(/^\s*-------*$/).each do |h|
9
9
  if h.match?(/[`_]#{help_command}/i)
10
- respond h, dest
10
+ respond "*#{config.channel}*:#{h}", dest
11
11
  help_found = true
12
12
  end
13
13
  end
14
- respond("I didn't find any command starting by `#{help_command}`", dest) unless help_found
14
+ respond("*#{config.channel}*: I didn't find any command starting by `#{help_command}`", dest) unless help_found
15
15
  else
16
16
  message = "-\n\n\n===================================\n*Rules from channel #{config.channel}*\n"
17
17
  if typem == :on_extended
@@ -135,8 +135,8 @@ class SlackSmartBot
135
135
  shortcut_name = $3.to_s.downcase
136
136
  command_to_run = $4
137
137
  add_shortcut(dest, from, typem, for_all, shortcut_name, command, command_to_run)
138
- when /^\s*delete\s+shortcut\s+(.+)/i, /^delete\s+sc\s+(.+)/i
139
- shortcut = $1.to_s.downcase
138
+ when /^\s*(delete|remove)\s+shortcut\s+(.+)/i, /^(delete|remove)\s+sc\s+(.+)/i
139
+ shortcut = $2.to_s.downcase
140
140
  delete_shortcut(dest, from, shortcut, typem, command)
141
141
  when /^\s*see\sshortcuts/i, /^see\ssc/i
142
142
  see_shortcuts(dest, from, typem)
@@ -109,6 +109,17 @@ class SlackSmartBot
109
109
  Thread.current[:user] = user
110
110
  Thread.current[:command] = command
111
111
  Thread.current[:rules_file] = rules_file
112
+ Thread.current[:typem] = typem
113
+ if (dest[0] == "C") || (dest[0] == "G") and @rules_imported.key?(user.id) &&
114
+ @rules_imported[user.id].key?(dchannel) && @bots_created.key?(@rules_imported[user.id][dchannel])
115
+ Thread.current[:using_channel] = @rules_imported[user.id][dchannel]
116
+ elsif dest[0] == "D" && @rules_imported.key?(user.id) && @rules_imported[user.id].key?(user.id) and
117
+ @bots_created.key?(@rules_imported[user.id][user.id])
118
+ Thread.current[:using_channel] = @rules_imported[user.id][user.id]
119
+ else
120
+ Thread.current[:using_channel] = ''
121
+ end
122
+
112
123
  processed = process(user, command, dest, dchannel, rules_file, typem, files)
113
124
  @logger.info "command: #{nick}> #{command}" if processed
114
125
  on_demand = false
@@ -192,7 +203,7 @@ class SlackSmartBot
192
203
  respond "No rules running. You can use the command `use rules from CHANNEL` to specify the rules you want to use on this private conversation.\n`bot help` to see available commands.", dest
193
204
  end
194
205
  unless processed
195
- dont_understand(__FILE__, command, user, dest)
206
+ dont_understand('')
196
207
  end
197
208
  end
198
209
  end
@@ -1,6 +1,10 @@
1
1
  class SlackSmartBot
2
2
  def treat_message(data)
3
- data.text = CGI.unescapeHTML(data.text)
3
+ begin
4
+ data.text = CGI.unescapeHTML(data.text) unless data.text.to_s.match(/\A\s*\z/)
5
+ rescue
6
+ @logger.warn "Impossible to unescape the data.text:#{data.text}"
7
+ end
4
8
  if config[:testing] and config.on_master_bot
5
9
  open("#{config.path}/buffer.log", "a") { |f|
6
10
  f.puts "|#{data.channel}|#{data.user}|#{data.text}"
@@ -30,7 +34,7 @@ class SlackSmartBot
30
34
  typem = :on_call
31
35
  end
32
36
  elsif dest == @master_bot_id
33
- if config.on_master_bot #only to be treated on master mot channel
37
+ if config.on_master_bot #only to be treated on master bot channel
34
38
  typem = :on_master
35
39
  end
36
40
  elsif @bots_created.key?(dest)
@@ -38,7 +42,15 @@ class SlackSmartBot
38
42
  typem = :on_bot
39
43
  end
40
44
  elsif dest[0] == "D" #Direct message
41
- if config.on_master_bot #only to be treated by master bot
45
+ get_rules_imported()
46
+ if @rules_imported.key?(data.user) && @rules_imported[data.user].key?(data.user) and
47
+ @bots_created.key?(@rules_imported[data.user][data.user])
48
+ if @channel_id == @rules_imported[data.user][data.user]
49
+ #only to be treated by the channel we are 'using'
50
+ typem = :on_dm
51
+ end
52
+ elsif config.on_master_bot
53
+ #only to be treated by master bot
42
54
  typem = :on_dm
43
55
  end
44
56
  elsif dest[0] == "C" or dest[0] == "G"
@@ -19,8 +19,13 @@ class SlackSmartBot
19
19
  @bots_created = eval(file_conf)
20
20
  end
21
21
  @datetime_bots_created = File.mtime(config.file_path.gsub(".rb", "_bots.rb"))
22
+ @extended_from = {}
22
23
  @bots_created.each do |k, v|
23
24
  v[:extended] = [] unless v.key?(:extended)
25
+ v[:extended].each do |ch|
26
+ @extended_from[ch] = [] unless @extended_from.key?(ch)
27
+ @extended_from[ch] << k
28
+ end
24
29
  v[:rules_file].gsub!(/^\./, '')
25
30
  end
26
31
  end
@@ -33,6 +38,18 @@ class SlackSmartBot
33
38
  file.close
34
39
  end
35
40
 
41
+ def get_rules_imported
42
+ if File.exist?("#{config.path}/rules/rules_imported.rb")
43
+ if !defined?(@datetime_rules_imported) or @datetime_rules_imported != File.mtime("#{config.path}/rules/rules_imported.rb")
44
+ @datetime_rules_imported = File.mtime("#{config.path}/rules/rules_imported.rb")
45
+ file_conf = IO.readlines("#{config.path}/rules/rules_imported.rb").join
46
+ unless file_conf.to_s() == ""
47
+ @rules_imported = eval(file_conf)
48
+ end
49
+ end
50
+ end
51
+ end
52
+
36
53
  def update_rules_imported
37
54
  file = File.open("#{config.path}/rules/rules_imported.rb", "w")
38
55
  file.write @rules_imported.inspect
@@ -67,14 +84,23 @@ class SlackSmartBot
67
84
  end
68
85
 
69
86
  def update_routines(channel = @channel_id)
87
+ routines = {}
70
88
  file = File.open("#{config.path}/routines/routines_#{channel}.rb", "w")
71
- file.write (@routines.inspect)
89
+ @routines.each do |k,v|
90
+ routines[k]={}
91
+ v.each do |kk,vv|
92
+ routines[k][kk] = vv.dup
93
+ routines[k][kk][:thread]=""
94
+ end
95
+ end
96
+ file.write (routines.inspect)
72
97
  file.close
73
98
  end
74
99
 
75
100
  def create_routine_thread(name)
76
101
  t = Thread.new do
77
102
  while @routines.key?(@channel_id) and @routines[@channel_id].key?(name)
103
+ @routines[@channel_id][name][:thread] = Thread.current
78
104
  started = Time.now
79
105
  if @status == :on and @routines[@channel_id][name][:status] == :on
80
106
  @logger.info "Routine: #{@routines[@channel_id][name].inspect}"
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.2.0
4
+ version: 1.3.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-11-05 00:00:00.000000000 Z
11
+ date: 2019-11-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: slack-ruby-client