slack-smart-bot 0.2.0 → 0.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: 982f9adf5d2a1c795a9d6846565fe81531678c321a80dadfc0ef800ca12beacd
4
- data.tar.gz: 11fc66ba9cddc967dd62b57f5cccc12189afd40358826147fa28a15cc4512aa6
3
+ metadata.gz: cfe8bd620d33775142c024063dd587fa8dfae7bb42c998fc617cf7d75daa991a
4
+ data.tar.gz: cc0829d5ebe5085c54b3f87f7c43c07718e8a0c05415acf49f1dea2c8ff0ca27
5
5
  SHA512:
6
- metadata.gz: 437a90a0344764d1cee245a9572dcb4f942996723c6ef8b256e631e9ae989f0db7886d2930e477aae7f0c2dbb24b9a8789e7e8d6f67e0896da5b4d7c7566e62b
7
- data.tar.gz: 3123a0005e8fb4aaa4f30568dc64c4f101b3cf3c9a2848db5d453516ac9b95e6c91865d8f6db3470bf728b71a79a1ef5e20de4465fbfd38a3d3c85519ffdb067
6
+ metadata.gz: d4381cba75e222058b4ba20c29c9ae2e3caaaa7db0a839645f7109b1e74c6c61da401fc7ae3b9df3fa016fc4624b7e24dfe5f543b76cdff56fdc0b18a86565a0
7
+ data.tar.gz: 62445d54652b4b4fa3ce9155d0736c812be8a31133b4b2def6cadb66f30f78834c1ae7c6a08cd37cea239914d3d7e10932550ed608fa2e3954f5e8d00448196f
data/README.md CHANGED
@@ -57,7 +57,8 @@ The rules file can be edited and will be only affecting this particular bot.
57
57
  You can add all the rules you want for your bot in the rules file, this is an example:
58
58
 
59
59
  ```ruby
60
- def rules(from, command, processed)
60
+ def rules(user, command, processed, id_user)
61
+ from = user.name
61
62
  firstname = from.split(" ").first
62
63
  case command
63
64
 
@@ -52,6 +52,7 @@ class SlackSmartBot
52
52
  @bots_created = Hash.new()
53
53
  @shortcuts = Hash.new()
54
54
  @shortcuts[:all] = Hash.new()
55
+ @rules_imported = Hash.new()
55
56
 
56
57
  if File.exist?("./shortcuts/#{SHORTCUTS_FILE}")
57
58
  file_sc = IO.readlines("./shortcuts/#{SHORTCUTS_FILE}").join
@@ -76,6 +77,13 @@ class SlackSmartBot
76
77
  end
77
78
  end
78
79
 
80
+ # rules imported only for private channels
81
+ if ON_MASTER_BOT and File.exist?('./rules/rules_imported.rb')
82
+ file_conf = IO.readlines('./rules/rules_imported.rb').join
83
+ unless file_conf.to_s() == ""
84
+ @rules_imported = eval(file_conf)
85
+ end
86
+ end
79
87
  wclient.auth_test
80
88
 
81
89
  begin
@@ -90,7 +98,7 @@ class SlackSmartBot
90
98
  m = "Successfully connected, welcome '#{client.self.name}' to the '#{client.team.name}' team at https://#{client.team.domain}.slack.com."
91
99
  puts m
92
100
  @logger.info m
93
- respond "Smart Bot started\nIf you want to know what I can do for you: *`bot help`*\nYou can send me also a direct message."
101
+ respond "Smart Bot started\nIf you want to know what I can do for you: `bot help` or `bot specific help`\nYou can send me also a direct message."
94
102
  end
95
103
 
96
104
  @status = STATUS_INIT
@@ -115,6 +123,13 @@ class SlackSmartBot
115
123
  file.close
116
124
  end
117
125
 
126
+ def update_rules_imported
127
+ file = File.open("./rules/rules_imported.rb", "w")
128
+ file.write @rules_imported.inspect
129
+ file.close
130
+ end
131
+
132
+
118
133
  def get_channels_name_and_id
119
134
  channels = wclient.channels_list.channels
120
135
  @channels_id = Hash.new()
@@ -137,10 +152,15 @@ class SlackSmartBot
137
152
  end
138
153
  # Direct messages are treated only on the master bot
139
154
  if id_user.nil? or (!id_user.nil? and ON_MASTER_BOT)
140
- user_info = wclient.users_info(user: data.user)
141
- if !id_user.nil? or @channels_id[CHANNEL] == data.channel or user_info.user.name == config[:nick]
142
- res = process_first(user_info.user.name, data.text, id_user)
143
- next if res.to_s == "next"
155
+ #todo: sometimes data.user is nil, check the problem.
156
+ if data.user.nil?
157
+ @logger.fatal "user is nil. channel: #{data.channel}, message: #{data.text}"
158
+ else
159
+ user_info = wclient.users_info(user: data.user)
160
+ if !id_user.nil? or @channels_id[CHANNEL] == data.channel or user_info.user.name == config[:nick]
161
+ res = process_first(user_info.user, data.text, id_user, data.channel)
162
+ next if res.to_s == "next"
163
+ end
144
164
  end
145
165
  end
146
166
  end
@@ -149,7 +169,8 @@ class SlackSmartBot
149
169
  client.start!
150
170
  end
151
171
 
152
- def process_first(nick, text, id_user)
172
+ def process_first(user, text, id_user, dchannel)
173
+ nick = user.name
153
174
  #todo: verify if on slack on anytime nick == config[:nick]
154
175
  if nick == config[:nick] or nick == (config[:nick] + " · Bot") #if message is coming from the bot
155
176
  begin
@@ -198,7 +219,7 @@ class SlackSmartBot
198
219
  begin
199
220
  t = Thread.new do
200
221
  begin
201
- processed = process(nick, command, id_user)
222
+ processed = process(user, command, id_user, dchannel)
202
223
  @logger.info "command: #{nick}> #{command}" if processed
203
224
  if @status == :on and
204
225
  ((@questions.keys.include?(nick) or
@@ -210,21 +231,63 @@ class SlackSmartBot
210
231
  @logger.info "command: #{nick}> #{command}" unless processed
211
232
  #todo: verify this
212
233
  if id_user.nil? #only for channels, not for DM
234
+ rules_file = RULES_FILE
235
+ if @rules_imported.key?(user.id) and @rules_imported[user.id].key?(dchannel)
236
+ unless @bots_created.key?(@rules_imported[user.id][dchannel])
237
+ file_conf = IO.readlines($0.gsub(".rb", "_bots.rb")).join
238
+ unless file_conf.to_s() == ""
239
+ @bots_created = eval(file_conf)
240
+ end
241
+ end
242
+ if @bots_created.key?(@rules_imported[user.id][dchannel])
243
+ rules_file = @bots_created[@rules_imported[user.id][dchannel]][:rules_file]
244
+ end
245
+ end
246
+ @logger.info "rules file: #{rules_file}"
213
247
  begin
214
- eval(File.new(RULES_FILE).read) if File.exist?(RULES_FILE)
248
+ eval(File.new(rules_file).read) if File.exist?(rules_file)
215
249
  rescue Exception => stack
216
- @logger.fatal "ERROR ON RULES FILE: #{RULES_FILE}"
250
+ @logger.fatal "ERROR ON RULES FILE: #{rules_file}"
217
251
  @logger.fatal stack
218
252
  end
219
253
  if defined?(rules)
220
254
  command[0] = "" if command[0] == "!"
221
255
  command.gsub!(/^@\w+:*\s*/, "")
222
- rules(nick, command, processed, id_user)
256
+ rules(user, command, processed, id_user)
223
257
  else
224
258
  @logger.warn "It seems like rules method is not defined"
225
259
  end
226
- else
227
- @logger.info "it is a direct message so no rules file executed."
260
+ elsif @rules_imported.key?(user.id) and @rules_imported[user.id].key?(user.id)
261
+ unless @bots_created.key?(@rules_imported[user.id][user.id])
262
+ file_conf = IO.readlines($0.gsub(".rb", "_bots.rb")).join
263
+ unless file_conf.to_s() == ""
264
+ @bots_created = eval(file_conf)
265
+ end
266
+ end
267
+
268
+ if @bots_created.key?(@rules_imported[user.id][user.id])
269
+ rules_file = @bots_created[@rules_imported[user.id][user.id]][:rules_file]
270
+ begin
271
+ eval(File.new(rules_file).read) if File.exist?(rules_file)
272
+ rescue Exception => stack
273
+ @logger.fatal "ERROR ON imported RULES FILE: #{rules_file}"
274
+ @logger.fatal stack
275
+ end
276
+ end
277
+
278
+ if defined?(rules)
279
+ command[0] = "" if command[0] == "!"
280
+ command.gsub!(/^@\w+:*\s*/, "")
281
+ rules(user, command, processed, id_user)
282
+ else
283
+ @logger.warn "It seems like rules method is not defined"
284
+ end
285
+ else #jal9
286
+ @logger.info "it is a direct message with no rules file selected so no rules file executed."
287
+ unless processed
288
+ resp = %w{ what huh sorry }.sample
289
+ respond "#{resp}?", id_user
290
+ end
228
291
  end
229
292
  end
230
293
  rescue Exception => stack
@@ -239,7 +302,8 @@ class SlackSmartBot
239
302
  #help:
240
303
  #help: *General commands:*:
241
304
  #help:
242
- def process(from, command, id_user)
305
+ def process(user, command, id_user, dchannel)
306
+ from = user.name
243
307
  firstname = from.split(/ /).first
244
308
  processed = true
245
309
 
@@ -255,6 +319,11 @@ class SlackSmartBot
255
319
  if @status == :on
256
320
  greetings = ["Hello", "Hallo", "Hi", "Hola", "What's up", "Hey", "Hæ"].sample
257
321
  respond "#{greetings} #{firstname}", id_user
322
+ if @rules_imported.key?(user.id) and @rules_imported[user.id].key?(user.id) and !id_user.nil?
323
+ respond "You are using specific rules for channel: <##{@rules_imported[user.id][user.id]}>", id_user
324
+ elsif @rules_imported.key?(user.id) and @rules_imported[user.id].key?(dchannel) and id_user.nil?
325
+ respond "You are using specific rules for channel: <##{@rules_imported[user.id][dchannel]}>", id_user
326
+ end
258
327
  @listening << from unless @listening.include?(from)
259
328
  end
260
329
 
@@ -307,7 +376,7 @@ class SlackSmartBot
307
376
  respond "Only admin users can kill me", id_user
308
377
  end
309
378
  else
310
- respond "To do this you need to be an admin user in the master channel", id_user
379
+ respond "To do this you need to be an admin user in the master channel: <##{@channels_id[MASTER_CHANNEL]}>", id_user
311
380
  end
312
381
 
313
382
  #helpadmin: `start bot`
@@ -364,7 +433,7 @@ class SlackSmartBot
364
433
  #helpmaster: creates a new bot on the channel specified
365
434
  #helpmaster: it will work only if you are on Master channel
366
435
  #helpmaster:
367
- when /^create\sbot\son\s<#C\w+\|(.+)>\s*/i, /^create\sbot\son\s(.+)\s*/i#jal9
436
+ when /^create\sbot\son\s<#C\w+\|(.+)>\s*/i, /^create\sbot\son\s(.+)\s*/i #jal9
368
437
  if ON_MASTER_BOT
369
438
  channel = $1
370
439
 
@@ -376,12 +445,17 @@ class SlackSmartBot
376
445
  elsif @channels_id.key?(channel) #it is a channel name
377
446
  channel_id = @channels_id[channel]
378
447
  end
448
+ channels = wclient.channels_list.channels
449
+ channel_found = channels.detect { |c| c.name == channel }
450
+
379
451
  if channel_id.nil?
380
452
  respond "There is no channel with that name: #{channel}, please be sure is written exactly the same", id_user
381
453
  elsif channel == MASTER_CHANNEL
382
454
  respond "There is already a bot in this channel: #{channel}", id_user
383
455
  elsif @bots_created.keys.include?(channel_id) #jal9
384
456
  respond "There is already a bot in this channel: #{channel}, kill it before", id_user
457
+ elsif config[:nick_id] != channel_found.creator and !channel_found.members.include?(config[:nick_id])
458
+ respond "You need to add first to the channel the smart bot user: #{config[:nick]}, kill it before", id_user
385
459
  else
386
460
  if channel_id != config[:channel]
387
461
  begin
@@ -424,17 +498,18 @@ class SlackSmartBot
424
498
  else
425
499
  respond "There is already a bot in this channel: #{channel}, and it is the Master Channel!", id_user
426
500
  end
427
-
428
501
  end
429
502
  else
430
- respond "Sorry I cannot create bots from this channel, please visit the master channel", id_user
503
+ @logger.info MASTER_CHANNEL
504
+ @logger.info @channel_id.inspect
505
+ respond "Sorry I cannot create bots from this channel, please visit the master channel: <##{@channels_id[MASTER_CHANNEL]}>", id_user
431
506
  end
432
507
 
433
508
  #helpmaster: `kill bot on CHANNEL_NAME`
434
509
  #helpmaster: kills the bot on the specified channel
435
510
  #helpmaster: Only works if you are on Master channel and you created that bot or you are an admin user
436
511
  #helpmaster:
437
- when /^kill\sbot\son\s<#C\w+\|(.+)>\s*/i,/^kill\sbot\son\s(.+)\s*/i
512
+ when /^kill\sbot\son\s<#C\w+\|(.+)>\s*/i, /^kill\sbot\son\s(.+)\s*/i
438
513
  if ON_MASTER_BOT
439
514
  channel = $1
440
515
 
@@ -464,27 +539,128 @@ class SlackSmartBot
464
539
  respond "There is no bot in this channel: #{channel}", id_user
465
540
  end
466
541
  else
467
- respond "Sorry I cannot kill bots from this channel, please visit the master channel", id_user
542
+ respond "Sorry I cannot kill bots from this channel, please visit the master channel: <##{@channels_id[MASTER_CHANNEL]}>", id_user
543
+ end
544
+
545
+ #help: `use rules from CHANNEL`
546
+ #help: `use rules CHANNEL`
547
+ #help: it will use the rules from the specified channel.
548
+ #help: you need to be part of that channel to be able to use the rules.
549
+ #help:
550
+ when /^use rules (from\s+)?<#C\w+\|(.+)>/i, /^use rules (from\s+)?(.+)/i
551
+ channel = $2
552
+ channels = wclient.channels_list.channels
553
+ channel_found = channels.detect { |c| c.name == channel }
554
+ if channel_found.nil?
555
+ respond "The channel you are trying to use doesn't exist", id_user
556
+ elsif channel_found.name == MASTER_CHANNEL
557
+ respond "You cannot use the rules from Master Channel on any other channel.", id_user
558
+ else
559
+ if user.id == channel_found.creator or channel_found.members.include?(user.id)
560
+ @rules_imported[user.id] = {} unless @rules_imported.key?(user.id)
561
+ if id_user.nil? #todo: take in consideration bots that are not master
562
+ @rules_imported[user.id][dchannel] = channel_found.id
563
+ else
564
+ @rules_imported[user.id][user.id] = channel_found.id
565
+ end
566
+ update_rules_imported() if ON_MASTER_BOT
567
+ respond "I'm using now the rules from <##{channel_found.id}>", id_user
568
+ else
569
+ respond "You need to join the channel <##{channel_found.id}> to be able to use the rules.", id_user
570
+ end
571
+ end
572
+
573
+ #help: `stop using rules from CHANNEL`
574
+ #help: `stop using rules CHANNEL`
575
+ #help: it will stop using the rules from the specified channel.
576
+ #help:
577
+ when /^stop using rules (from\s+)?<#C\w+\|(.+)>/i, /^stop using rules (from\s+)?(.+)/i
578
+ channel = $2
579
+ if @channels_id.key?(channel)
580
+ channel_id = @channels_id[channel]
581
+ else
582
+ channel_id = channel
583
+ end
584
+ if id_user.nil? #channel
585
+ if @rules_imported.key?(user.id) and @rules_imported[user.id].key?(dchannel)
586
+ if @rules_imported[user.id][dchannel]!= channel_id
587
+ respond "You are not using those rules.", id_user
588
+ else
589
+ @rules_imported[user.id].delete(dchannel)
590
+ update_rules_imported() if ON_MASTER_BOT
591
+ respond "You won't be using those rules from now on.", id_user
592
+ end
593
+ else
594
+ respond "You were not using those rules.", id_user
595
+ end
596
+ else #direct message
597
+ if @rules_imported.key?(user.id) and @rules_imported[user.id].key?(user.id)
598
+ if @rules_imported[user.id][user.id]!= channel_id
599
+ respond "You are not using those rules.", id_user
600
+ else
601
+ @rules_imported[user.id].delete(user.id)
602
+ update_rules_imported() if ON_MASTER_BOT
603
+ respond "You won't be using those rules from now on.", id_user
604
+ end
605
+ else
606
+ respond "You were not using those rules.", id_user
607
+ end
468
608
  end
469
609
 
610
+
470
611
  #help: `bot help`
471
612
  #help: `bot what can I do?`
613
+ #help: `bot specific help`
472
614
  #help: it will display this help
473
- #help:
474
- when /^bot help/i, /^bot,? what can I do/i
475
- help_message = IO.readlines(__FILE__).join
476
- if ADMIN_USERS.include?(from) #admin user
477
- respond "*Commands for administrators:*\n#{help_message.scan(/#\s*help\s*admin:(.*)/).join("\n")}", id_user
615
+ #help: bot specific help will show only the specific rules for this channel.
616
+ when /^bot (specific\s)?help/i, /^bot,? what can I do/i
617
+ if $1.to_s != ''
618
+ specific = true
619
+ else
620
+ specific = false
478
621
  end
479
- if ON_MASTER_BOT and id_user.nil?
480
- respond "*Commands only on Master Channel:*\n#{help_message.scan(/#\s*help\s*master:(.*)/).join("\n")}", id_user
622
+ if !specific then
623
+ help_message = IO.readlines(__FILE__).join
624
+ if ADMIN_USERS.include?(from) #admin user
625
+ respond "*Commands for administrators:*\n#{help_message.scan(/#\s*help\s*admin:(.*)/).join("\n")}", id_user
626
+ end
627
+ if ON_MASTER_BOT and id_user.nil?
628
+ respond "*Commands only on Master Channel <##{@channels_id[MASTER_CHANNEL]}>:*\n#{help_message.scan(/#\s*help\s*master:(.*)/).join("\n")}", id_user
629
+ end
630
+ respond help_message.scan(/#\s*help\s*:(.*)/).join("\n"), id_user
481
631
  end
482
- #jal9
483
- respond help_message.scan(/#\s*help\s*:(.*)/).join("\n"), id_user
484
632
  if id_user.nil? # on a channel
485
- help_message_rules = IO.readlines(RULES_FILE).join
633
+ rules_file = RULES_FILE
634
+
635
+ if @rules_imported.key?(user.id) and @rules_imported[user.id].key?(dchannel)
636
+
637
+ unless @bots_created.key?(@rules_imported[user.id][dchannel])
638
+ file_conf = IO.readlines($0.gsub(".rb", "_bots.rb")).join
639
+ unless file_conf.to_s() == ""
640
+ @bots_created = eval(file_conf)
641
+ end
642
+ end
643
+ if @bots_created.key?(@rules_imported[user.id][dchannel])
644
+ rules_file = @bots_created[@rules_imported[user.id][dchannel]][:rules_file]
645
+ respond "*You are using rules from another channel: <##{@rules_imported[user.id][dchannel]}>. These are the specific commands for that channel:*", id_user
646
+ end
647
+ end
648
+ help_message_rules = IO.readlines(rules_file).join
486
649
  respond help_message_rules.scan(/#\s*help\s*:(.*)/).join("\n"), id_user
487
- end
650
+ elsif !id_user.nil? and @rules_imported.key?(user.id) and @rules_imported[user.id].key?(user.id) #direct message
651
+ unless @bots_created.key?(@rules_imported[user.id][user.id])
652
+ file_conf = IO.readlines($0.gsub(".rb", "_bots.rb")).join
653
+ unless file_conf.to_s() == ""
654
+ @bots_created = eval(file_conf)
655
+ end
656
+ end
657
+ if @bots_created.key?(@rules_imported[user.id][user.id])
658
+ rules_file = @bots_created[@rules_imported[user.id][user.id]][:rules_file]
659
+ respond "*You are using rules from channel: <##{@rules_imported[user.id][user.id]}>. These are the specific commands for that channel:*", id_user
660
+ help_message_rules = IO.readlines(rules_file).join
661
+ respond help_message_rules.scan(/#\s*help\s*:(.*)/).join("\n"), id_user
662
+ end
663
+ end
488
664
  respond "Github project: https://github.com/MarioRuiz/slack-smart-bot", id_user
489
665
  else
490
666
  processed = false
@@ -29,7 +29,8 @@ end
29
29
  # help: `@NAME_OF_BOT THE_COMMAND`
30
30
  # help: `NAME_OF_BOT THE_COMMAND`
31
31
  # help:
32
- def rules(from, command, processed, id_user)
32
+ def rules(user, command, processed, id_user)
33
+ from = user.name
33
34
  if @testing
34
35
  puts "#{from}: #{command}"
35
36
  if @questions.keys.include?(from)
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: 0.2.0
4
+ version: 0.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-05-20 00:00:00.000000000 Z
11
+ date: 2019-05-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: slack-ruby-client