slack-smart-bot 0.7.6 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +18 -4
  3. data/lib/slack-smart-bot.rb +498 -379
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1f04209396105787c535cc023a3af0b3e63bb57f5ed62ef379f5d63c3f8b08ef
4
- data.tar.gz: d5fb33126522405993050549bca48fae7ce731941629a5b43d770078d8d630df
3
+ metadata.gz: e67d5995df02ee254fa8718239824db71089ea64978f9faecdcca2314ebf9a65
4
+ data.tar.gz: 057437a83aaf967520f7cb327c0225f2a2bd2194c5a97502131869f537484d81
5
5
  SHA512:
6
- metadata.gz: 31b67a7e6215163517f9eca2e2131763d14709d3fef207fdb305e392ad28d0f7cc5db7d044aa8dd3e6a161a4e553053d74fbf74b71c1e0e625c6635c123adcd1
7
- data.tar.gz: 1d0fdcf1e83bc064e9ae8e0440798c5a066ca87c1383a96fda55e11cfa378bc4fdd8ef3eaddffedd9720a9bbf6fb675eddbc3d14448458aeb94e90420bcd6ec6
6
+ metadata.gz: 9415c8a44f98beafdc03ab06df88064ac7ac83d16de3fd3b1f5c3033937e0956c30db54d74272e867b33e2d9f99e60ea33165ed8ec89a8d3923c95c7e2a829bb
7
+ data.tar.gz: b2d54101b89ecc4a9d048313674d99b0269d40e520f709f24a189f3d490c74c8a8e8ae5b506308e4022daf8d65a3fee79dbd8dccb4652c36a2cd4643a70f8987
data/README.md CHANGED
@@ -241,6 +241,7 @@ Apart of the specific commands you define on the rules file of the channel, you
241
241
 
242
242
  >ruby require 'json'; res=[]; 20.times {res<<rand(100)}; my_json={result: res}; puts my_json.to_json
243
243
 
244
+ >Also it is possible to attach a Ruby file.
244
245
 
245
246
  **_`add shortcut NAME: COMMAND`_**
246
247
 
@@ -278,18 +279,29 @@ Apart of the specific commands you define on the rules file of the channel, you
278
279
 
279
280
  **_`use rules from CHANNEL`_**
280
281
 
281
- **_`use rules CHANNEL`_**
282
-
283
282
  >it will use the rules from the specified channel.
284
283
 
285
284
  >you need to be part of that channel to be able to use the rules.
286
285
 
287
- **_`stop using rules from CHANNEL`_**
286
+ >only when on a private conversation with the bot or in a private group
288
287
 
289
- **_`stop using rules CHANNEL`_**
288
+ **_`stop using rules from CHANNEL`_**
290
289
 
291
290
  >it will stop using the rules from the specified channel.
292
291
 
292
+ >only when on a private conversation with the bot or in a private group
293
+
294
+ **_`extend rules to CHANNEL_NAME`_**
295
+
296
+ **_`use rules on CHANNEL_NAME`_**
297
+
298
+ >it will allow to use the specific rules from this channel on the CHANNEL_NAME
299
+
300
+ **_`stop using rules on CHANNEL_NAME`_**
301
+
302
+ >it will stop using the extended rules on the specified channel.
303
+
304
+
293
305
  ### Available commands from channels without a Smart Bot
294
306
  **_`@BOT_NAME on #CHANNEL_NAME COMMAND`_**
295
307
 
@@ -299,6 +311,8 @@ Apart of the specific commands you define on the rules file of the channel, you
299
311
 
300
312
  >You need to join the specified channel to be able to use those rules.
301
313
 
314
+ >Also you can use this command to call another bot from a channel with a running bot.
315
+
302
316
  ## Contributing
303
317
 
304
318
  Bug reports and pull requests are welcome on GitHub at https://github.com/marioruiz/slack-smart-bot.
@@ -64,18 +64,15 @@ class SlackSmartBot
64
64
  end
65
65
 
66
66
  if ON_MASTER_BOT and File.exist?($0.gsub(".rb", "_bots.rb"))
67
- file_conf = IO.readlines($0.gsub(".rb", "_bots.rb")).join
68
- unless file_conf.to_s() == ""
69
- @bots_created = eval(file_conf)
70
- if @bots_created.kind_of?(Hash)
71
- @bots_created.each { |key, value|
72
- @logger.info "ruby #{$0} \"#{value[:channel_name]}\" \"#{value[:admins]}\" \"#{value[:rules_file]}\" #{value[:status].to_sym}"
73
- t = Thread.new do
74
- `ruby #{$0} \"#{value[:channel_name]}\" \"#{value[:admins]}\" \"#{value[:rules_file]}\" #{value[:status].to_sym}`
75
- end
76
- value[:thread] = t
77
- }
78
- end
67
+ get_bots_created()
68
+ if @bots_created.kind_of?(Hash)
69
+ @bots_created.each { |key, value|
70
+ @logger.info "ruby #{$0} \"#{value[:channel_name]}\" \"#{value[:admins]}\" \"#{value[:rules_file]}\" #{value[:status].to_sym}"
71
+ t = Thread.new do
72
+ `ruby #{$0} \"#{value[:channel_name]}\" \"#{value[:admins]}\" \"#{value[:rules_file]}\" #{value[:status].to_sym}`
73
+ end
74
+ value[:thread] = t
75
+ }
79
76
  end
80
77
  end
81
78
 
@@ -139,6 +136,18 @@ class SlackSmartBot
139
136
  file.close
140
137
  end
141
138
 
139
+ def get_bots_created
140
+ if !defined?(@datetime_bots_created) or @datetime_bots_created!=File.mtime($0.gsub(".rb", "_bots.rb"))
141
+ file_conf = IO.readlines($0.gsub(".rb", "_bots.rb")).join
142
+ if file_conf.to_s() == ""
143
+ @bots_created = {}
144
+ else
145
+ @bots_created = eval(file_conf)
146
+ end
147
+ @datetime_bots_created = File.mtime($0.gsub(".rb", "_bots.rb"))
148
+ end
149
+ end
150
+
142
151
  def update_shortcuts_file
143
152
  file = File.open("./shortcuts/#{SHORTCUTS_FILE}", "w")
144
153
  file.write @shortcuts.inspect
@@ -173,8 +182,10 @@ class SlackSmartBot
173
182
  #help: `@BOT_NAME #CHANNEL_NAME COMMAND`
174
183
  #help: It will run the supplied command using the rules on the channel supplied.
175
184
  #help: You need to join the specified channel to be able to use those rules.
185
+ #help: Also you can use this command to call another bot from a channel with a running bot.
176
186
  def listen
177
187
  @salutations = [config[:nick], config[:nick_id], "bot", "smart"]
188
+ get_bots_created()
178
189
  client.on :message do |data|
179
190
  if data.channel[0] == "D" or data.channel[0] == "C" or data.channel[0] == "G" #Direct message or Channel or Private Channel
180
191
  dest = data.channel
@@ -188,7 +199,6 @@ class SlackSmartBot
188
199
  begin
189
200
  #todo: when changed @questions user_id then move user_info inside the ifs to avoid calling it when not necessary
190
201
  user_info = client.web_client.users_info(user: data.user)
191
- #todo: check to remove user_info.user.name == config[:nick] since I think we will never get messages from the bot on slack
192
202
  # if Direct message or we are in the channel of the bot
193
203
  if data.text.size >= 2 and
194
204
  ((data.text[0] == "`" and data.text[-1] == "`") or (data.text[0] == "*" and data.text[-1] == "*") or (data.text[0] == "_" and data.text[-1] == "_"))
@@ -234,6 +244,10 @@ class SlackSmartBot
234
244
  #todo: @questions key should be the id not the name. change it everywhere
235
245
  dest = data.channel
236
246
  res = process_first(user_info.user, data.text, dest, @channels_id[CHANNEL])
247
+ elsif CHANNEL != MASTER_CHANNEL and @bots_created[@channels_id[CHANNEL]].key?(:extended) and
248
+ @bots_created[@channels_id[CHANNEL]][:extended].include?(@channels_name[data.channel]) and
249
+ data.text.size > 0 and data.text[0] != "-"
250
+ res = process_first(user_info.user, data.text, dest, @channels_id[CHANNEL])
237
251
  elsif (dest[0] == "D" or @channels_id[CHANNEL] == data.channel or data.user == config[:nick_id]) and
238
252
  data.text.size > 0 and data.text[0] != "-"
239
253
  res = process_first(user_info.user, data.text, dest, data.channel)
@@ -258,10 +272,7 @@ class SlackSmartBot
258
272
 
259
273
  if @rules_imported.key?(user.id) and @rules_imported[user.id].key?(dchannel)
260
274
  unless @bots_created.key?(@rules_imported[user.id][dchannel])
261
- file_conf = IO.readlines($0.gsub(".rb", "_bots.rb")).join
262
- unless file_conf.to_s() == ""
263
- @bots_created = eval(file_conf)
264
- end
275
+ get_bots_created()
265
276
  end
266
277
  if @bots_created.key?(@rules_imported[user.id][dchannel])
267
278
  rules_file = @bots_created[@rules_imported[user.id][dchannel]][:rules_file]
@@ -269,10 +280,7 @@ class SlackSmartBot
269
280
  end
270
281
  elsif dest[0] == "D" and @rules_imported.key?(user.id) and @rules_imported[user.id].key?(user.id) #direct message
271
282
  unless @bots_created.key?(@rules_imported[user.id][user.id])
272
- file_conf = IO.readlines($0.gsub(".rb", "_bots.rb")).join
273
- unless file_conf.to_s() == ""
274
- @bots_created = eval(file_conf)
275
- end
283
+ get_bots_created()
276
284
  end
277
285
  if @bots_created.key?(@rules_imported[user.id][user.id])
278
286
  rules_file = @bots_created[@rules_imported[user.id][user.id]][:rules_file]
@@ -350,9 +358,14 @@ class SlackSmartBot
350
358
  command = $2
351
359
  on_demand = true
352
360
  end
361
+ if dest[0]=="C" and dchannel != dest # on extended channel only specific rules
362
+ only_on_demand = true
363
+ else
364
+ only_on_demand = false
365
+ end
353
366
  if @status == :on and
354
367
  (@questions.keys.include?(nick) or
355
- @listening.include?(nick) or
368
+ (@listening.include?(nick) and !only_on_demand) or
356
369
  dest[0] == "D" or on_demand)
357
370
  @logger.info "command: #{nick}> #{command}" unless processed
358
371
  #todo: verify this
@@ -431,417 +444,432 @@ class SlackSmartBot
431
444
  firstname = from.split(/ /).first
432
445
  processed = true
433
446
 
434
- case command
435
-
436
- #help: ----------------------------------------------
437
- #help: `Hello Bot`
438
- #help: `Hello Smart`
439
- #help: `Hello THE_NAME_OF_THE_BOT`
440
- #help: Also apart of Hello you can use _Hallo, Hi, Hola, What's up, Hey, Hæ_
441
- #help: Bot starts listening to you
442
- #help: If you want to avoid a single message to be treated by the smart bot, start the message by -
443
- #help:
444
- when /^(Hello|Hallo|Hi|Hola|What's\sup|Hey|Hæ)\s(#{@salutations.join("|")})\s*$/i
445
- if @status == :on
446
- greetings = ["Hello", "Hallo", "Hi", "Hola", "What's up", "Hey", "Hæ"].sample
447
- respond "#{greetings} #{firstname}", dest
448
- if @rules_imported.key?(user.id) and @rules_imported[user.id].key?(user.id) and dest[0] == "D"
449
- respond "You are using specific rules for channel: <##{@rules_imported[user.id][user.id]}>", dest
450
- elsif @rules_imported.key?(user.id) and @rules_imported[user.id].key?(dchannel) and (dest[0] == "C" or dest[0] == "G")
451
- respond "You are using specific rules for channel: <##{@rules_imported[user.id][dchannel]}>", dest
452
- end
453
- @listening << from unless @listening.include?(from)
454
- end
447
+ unless dest[0]=="C" and dchannel!=dest # on extended channel only specific rules
448
+ case command
455
449
 
456
450
  #help: ----------------------------------------------
457
- #help: `Bye Bot`
458
- #help: `Bye Smart`
459
- #help: `Bye NAME_OF_THE_BOT`
460
- #help: Also apart of Bye you can use _Bæ, Good Bye, Adiós, Ciao, Bless, Bless Bless, Adeu_
461
- #help: Bot stops listening to you
451
+ #help: `Hello Bot`
452
+ #help: `Hello Smart`
453
+ #help: `Hello THE_NAME_OF_THE_BOT`
454
+ #help: Also apart of Hello you can use _Hallo, Hi, Hola, What's up, Hey, Hæ_
455
+ #help: Bot starts listening to you
456
+ #help: If you want to avoid a single message to be treated by the smart bot, start the message by -
462
457
  #help:
463
- when /^(Bye|Bæ|Good\sBye|Adiós|Ciao|Bless|Bless\sBless|Adeu)\s(#{@salutations.join("|")})\s*$/i
464
- if @status == :on
465
- bye = ["Bye", "", "Good Bye", "Adiós", "Ciao", "Bless", "Bless bless", "Adeu"].sample
466
- respond "#{bye} #{firstname}", dest
467
- @listening.delete(from)
468
- end
469
-
470
- #helpadmin: ----------------------------------------------
471
- #helpadmin: `exit bot`
472
- #helpadmin: `quit bot`
473
- #helpadmin: `close bot`
474
- #helpadmin: The bot stops running and also stops all the bots created from this master channel
475
- #helpadmin: You can use this command only if you are an admin user and you are on the master channel
476
- #helpadmin:
477
- when /^exit\sbot\s*$/i, /^quit\sbot\s*$/i, /^close\sbot\s*$/i
478
- if ON_MASTER_BOT
479
- if ADMIN_USERS.include?(from) #admin user
480
- unless @questions.keys.include?(from)
481
- ask("are you sure?", command, from, dest)
482
- else
483
- case @questions[from]
484
- when /yes/i, /yep/i, /sure/i
485
- respond "Game over!", dest
486
- respond "Ciao #{firstname}!", dest
487
- @bots_created.each { |key, value|
488
- value[:thread] = ""
489
- send_msg_channel(key, "Bot has been closed by #{from}")
490
- sleep 0.5
491
- }
492
- update_bots_file()
493
- sleep 0.5
494
- exit!
495
- when /no/i, /nope/i, /cancel/i
496
- @questions.delete(from)
497
- respond "Thanks, I'm happy to be alive", dest
498
- else
499
- respond "I don't understand", dest
500
- ask("are you sure do you want me to close? (yes or no)", "quit bot", from, dest)
501
- end
458
+ when /^(Hello|Hallo|Hi|Hola|What's\sup|Hey|Hæ)\s(#{@salutations.join("|")})\s*$/i
459
+ if @status == :on
460
+ greetings = ["Hello", "Hallo", "Hi", "Hola", "What's up", "Hey", ""].sample
461
+ respond "#{greetings} #{firstname}", dest
462
+ if @rules_imported.key?(user.id) and @rules_imported[user.id].key?(user.id) and dest[0] == "D"
463
+ respond "You are using specific rules for channel: <##{@rules_imported[user.id][user.id]}>", dest
464
+ elsif @rules_imported.key?(user.id) and @rules_imported[user.id].key?(dchannel) and (dest[0] == "C" or dest[0] == "G")
465
+ respond "You are using specific rules for channel: <##{@rules_imported[user.id][dchannel]}>", dest
502
466
  end
503
- else
504
- respond "Only admin users can kill me", dest
467
+ @listening << from unless @listening.include?(from)
505
468
  end
506
- else
507
- respond "To do this you need to be an admin user in the master channel: <##{@channels_id[MASTER_CHANNEL]}>", dest
508
- end
509
469
 
510
- #helpadmin: ----------------------------------------------
511
- #helpadmin: `start bot`
512
- #helpadmin: `start this bot`
513
- #helpadmin: the bot will start to listen
514
- #helpadmin: You can use this command only if you are an admin user
515
- #helpadmin:
516
- when /^start\s(this\s)?bot$/i
517
- if ADMIN_USERS.include?(from) #admin user
518
- respond "This bot is running and listening from now on. You can pause again: pause this bot", dest
519
- @status = :on
520
- unless ON_MASTER_BOT
521
- send_msg_channel MASTER_CHANNEL, "Changed status on #{CHANNEL} to :on"
470
+ #help: ----------------------------------------------
471
+ #help: `Bye Bot`
472
+ #help: `Bye Smart`
473
+ #help: `Bye NAME_OF_THE_BOT`
474
+ #help: Also apart of Bye you can use _Bæ, Good Bye, Adiós, Ciao, Bless, Bless Bless, Adeu_
475
+ #help: Bot stops listening to you
476
+ #help:
477
+ when /^(Bye|Bæ|Good\sBye|Adiós|Ciao|Bless|Bless\sBless|Adeu)\s(#{@salutations.join("|")})\s*$/i
478
+ if @status == :on
479
+ bye = ["Bye", "Bæ", "Good Bye", "Adiós", "Ciao", "Bless", "Bless bless", "Adeu"].sample
480
+ respond "#{bye} #{firstname}", dest
481
+ @listening.delete(from)
522
482
  end
523
- else
524
- respond "Only admin users can change my status", dest
525
- end
526
483
 
527
- #helpadmin: ----------------------------------------------
528
- #helpadmin: `pause bot`
529
- #helpadmin: `pause this bot`
530
- #helpadmin: the bot will pause so it will listen only to admin commands
531
- #helpadmin: You can use this command only if you are an admin user
532
- #helpadmin:
533
- when /^pause\s(this\s)?bot$/i
534
- if ADMIN_USERS.include?(from) #admin user
535
- respond "This bot is paused from now on. You can start it again: start this bot", dest
536
- respond "zZzzzzZzzzzZZZZZZzzzzzzzz", dest
537
- @status = :paused
538
- unless ON_MASTER_BOT
539
- send_msg_channel MASTER_CHANNEL, "Changed status on #{CHANNEL} to :paused"
484
+ #help: ----------------------------------------------
485
+ #help: `bot help`
486
+ #help: `bot what can I do?`
487
+ #help: `bot rules`
488
+ #help: it will display this help
489
+ #help: `bot rules` will show only the specific rules for this channel.
490
+ when /^bot\s+(rules|help)/i, /^bot,? what can I do/i
491
+ if $1.to_s.match?(/rules/i)
492
+ specific = true
493
+ else
494
+ specific = false
540
495
  end
541
- else
542
- respond "Only admin users can put me on pause", dest
543
- end
544
-
545
- #helpadmin: ----------------------------------------------
546
- #helpadmin: `bot status`
547
- #helpadmin: Displays the status of the bot
548
- #helpadmin: If on master channel and admin user also it will display info about bots created
549
- #helpadmin:
550
- when /^bot\sstatus/i
551
- gems_remote = `gem list slack-smart-bot --remote`
552
- version_remote = gems_remote.to_s().scan(/slack-smart-bot \((\d+\.\d+\.\d+)/).join
553
- version_message = ""
554
- if version_remote != VERSION
555
- version_message = " There is a new available version: #{version_remote}."
556
- end
557
- respond "Status: #{@status}. Version: #{VERSION}.#{version_message} Rules file: #{File.basename RULES_FILE} ", dest
558
- if @status == :on
559
- respond "I'm listening to [#{@listening.join(", ")}]", dest
560
- if ON_MASTER_BOT and ADMIN_USERS.include?(from)
561
- @bots_created.each { |key, value|
562
- respond "#{key}: #{value}", dest
563
- }
496
+ help_message_rules = ''
497
+ if !specific
498
+ help_message = IO.readlines(__FILE__).join
499
+ if ADMIN_USERS.include?(from) #admin user
500
+ respond "*Commands for administrators:*\n#{help_message.scan(/#\s*help\s*admin:(.*)/).join("\n")}", dest
501
+ end
502
+ if ON_MASTER_BOT and (dest[0] == "C" or dest[0] == "G")
503
+ respond "*Commands only on Master Channel <##{@channels_id[MASTER_CHANNEL]}>:*\n#{help_message.scan(/#\s*help\s*master:(.*)/).join("\n")}", dest
504
+ end
505
+ respond help_message.scan(/#\s*help\s*:(.*)/).join("\n"), dest
564
506
  end
565
- end
566
-
567
- #helpmaster: ----------------------------------------------
568
- #helpmaster: `notify MESSAGE`
569
- #helpmaster: `notify all MESSAGE`
570
- #helpmaster: It will send a notificaiton message to all bot channels
571
- #helpmaster: It will send a notification message to all channels the bot joined and private conversations with the bot
572
- #helpmaster: Only works if you are on Master channel and you are an admin user
573
- #helpmaster:
574
- when /^notify\s+(all)?\s*(.+)\s*$/i
575
- if ON_MASTER_BOT
576
- if ADMIN_USERS.include?(from) #admin user
577
- all = $1
578
- message = $2
579
- if all.nil?
580
- @bots_created.each do |k, v|
581
- respond message, k
507
+ if dest[0] == "C" or dest[0] == "G" # on a channel
508
+ if @rules_imported.key?(user.id) and @rules_imported[user.id].key?(dchannel)
509
+ if @bots_created.key?(@rules_imported[user.id][dchannel])
510
+ respond "*You are using rules from another channel: <##{@rules_imported[user.id][dchannel]}>. These are the specific commands for that channel:*", dest
582
511
  end
583
- respond "Bot channels have been notified", dest
584
- else
585
- myconv = client.web_client.users_conversations(exclude_archived: true, limit: 100, types: "im, public_channel").channels
586
- myconv.each do |c|
587
- respond message, c.id unless c.name == MASTER_CHANNEL
512
+ end
513
+ help_message_rules = IO.readlines(rules_file).join
514
+ respond help_message_rules.scan(/#\s*help\s*:(.*)/).join("\n"), dest
515
+ elsif dest[0] == "D" and @rules_imported.key?(user.id) and @rules_imported[user.id].key?(user.id) #direct message
516
+ if @bots_created.key?(@rules_imported[user.id][user.id])
517
+ respond "*You are using rules from channel: <##{@rules_imported[user.id][user.id]}>. These are the specific commands for that channel:*", dest
518
+ help_message_rules = IO.readlines(rules_file).join
519
+ respond help_message_rules.scan(/#\s*help\s*:(.*)/).join("\n"), dest
520
+ end
521
+ end
522
+ if specific
523
+ unless rules_file.empty?
524
+ begin
525
+ eval(File.new(rules_file).read) if File.exist?(rules_file)
588
526
  end
589
- respond "Channels and users have been notified", dest
590
527
  end
528
+ if defined?(git_project) and git_project.to_s!='' and help_message_rules != ''
529
+ respond "Git project: #{git_project}", dest
530
+ else
531
+ def git_project() '' end
532
+ def project_folder() '' end
533
+ end
534
+
535
+ else
536
+ respond "Slack Smart Bot Github project: https://github.com/MarioRuiz/slack-smart-bot", dest
591
537
  end
592
- end
593
538
 
594
- #helpmaster: ----------------------------------------------
595
- #helpmaster: `create bot on CHANNEL_NAME`
596
- #helpmaster: creates a new bot on the channel specified
597
- #helpmaster: it will work only if you are on Master channel
598
- #helpmaster:
599
- when /^create\sbot\son\s<#C\w+\|(.+)>\s*/i, /^create\sbot\son\s(.+)\s*/i
600
- if ON_MASTER_BOT
601
- channel = $1
602
-
603
- get_channels_name_and_id() unless @channels_name.keys.include?(channel) or @channels_id.keys.include?(channel)
604
- channel_id = nil
605
- if @channels_name.key?(channel) #it is an id
606
- channel_id = channel
607
- channel = @channels_name[channel_id]
608
- elsif @channels_id.key?(channel) #it is a channel name
609
- channel_id = @channels_id[channel]
610
- end
539
+ #help: ===================================
540
+ #help:
541
+ #help: *These commands will run only when on a private conversation with the bot or in a private group:*
542
+ #help:
543
+ #help: ----------------------------------------------
544
+ #help: `use rules from CHANNEL`
545
+ #help: it will use the rules from the specified channel.
546
+ #help: you need to be part of that channel to be able to use the rules.
547
+ #help:
548
+ when /^use rules (from\s+)<#C\w+\|(.+)>/i, /^use rules (from\s+)(.+)/i
549
+ channel = $2
611
550
  channels = client.web_client.channels_list.channels
612
551
  channel_found = channels.detect { |c| c.name == channel }
613
552
  members = client.web_client.conversations_members(channel: @channels_id[channel]).members unless channel_found.nil?
614
553
 
615
- if channel_id.nil?
616
- respond "There is no channel with that name: #{channel}, please be sure is written exactly the same", dest
617
- elsif channel == MASTER_CHANNEL
618
- respond "There is already a bot in this channel: #{channel}", dest
619
- elsif @bots_created.keys.include?(channel_id)
620
- respond "There is already a bot in this channel: #{channel}, kill it before", dest
621
- elsif config[:nick_id] != channel_found.creator and !members.include?(config[:nick_id])
622
- respond "You need to add first to the channel the smart bot user: #{config[:nick]}", dest
554
+ if channel_found.nil?
555
+ respond "The channel you are trying to use doesn't exist", dest
556
+ elsif channel_found.name == MASTER_CHANNEL
557
+ respond "You cannot use the rules from Master Channel on any other channel.", dest
558
+ elsif !@bots_created.key?(@channels_id[channel])
559
+ respond "There is no bot running on that channel.", dest
560
+ elsif @bots_created.key?(@channels_id[channel]) and @bots_created[@channels_id[channel]][:status] != :on
561
+ respond "The bot in that channel is not :on", dest
623
562
  else
624
- if channel_id != config[:channel]
625
- begin
626
- rules_file = "slack-smart-bot_rules_#{channel_id}_#{from.gsub(" ", "_")}.rb"
627
- if defined?(RULES_FOLDER)
628
- rules_file = RULES_FOLDER + rules_file
629
- else
630
- Dir.mkdir("rules") unless Dir.exist?("rules")
631
- Dir.mkdir("rules/#{channel_id}") unless Dir.exist?("rules/#{channel_id}")
632
- rules_file = "./rules/#{channel_id}/" + rules_file
633
- end
634
- default_rules = (__FILE__).gsub(/\.rb$/, "_rules.rb")
635
- File.delete(rules_file) if File.exist?(rules_file)
636
- FileUtils.copy_file(default_rules, rules_file) unless File.exist?(rules_file)
637
- admin_users = Array.new()
638
- admin_users = [from] + MASTER_USERS
639
- admin_users.uniq!
640
- @logger.info "ruby #{$0} \"#{channel}\" \"#{admin_users.join(",")}\" \"#{rules_file}\" on"
641
- t = Thread.new do
642
- `ruby #{$0} \"#{channel}\" \"#{admin_users.join(",")}\" \"#{rules_file}\" on`
643
- end
644
- @bots_created[channel_id] = {
645
- creator_name: from,
646
- channel_id: channel_id,
647
- channel_name: @channels_name[channel_id],
648
- status: :on,
649
- created: Time.now.strftime("%Y-%m-%dT%H:%M:%S.000Z")[0..18],
650
- rules_file: rules_file,
651
- admins: admin_users.join(","),
652
- thread: t,
653
- }
654
- respond "The bot has been created on channel: #{channel}. Rules file: #{File.basename rules_file}", dest
655
- update_bots_file()
656
- rescue Exception => stack
657
- @logger.fatal stack
658
- message = "Problem creating the bot on channel #{channel}. Error: <#{stack}>."
659
- @logger.error message
660
- respond message, dest
563
+ if user.id == channel_found.creator or members.include?(user.id)
564
+ @rules_imported[user.id] = {} unless @rules_imported.key?(user.id)
565
+ if dest[0] == "C" or dest[0] == "G" #todo: take in consideration bots that are not master
566
+ @rules_imported[user.id][dchannel] = channel_found.id
567
+ else
568
+ @rules_imported[user.id][user.id] = channel_found.id
661
569
  end
570
+ update_rules_imported() if ON_MASTER_BOT
571
+ respond "I'm using now the rules from <##{channel_found.id}>", dest
572
+ def git_project() "" end
573
+ def project_folder() "" end
662
574
  else
663
- respond "There is already a bot in this channel: #{channel}, and it is the Master Channel!", dest
575
+ respond "You need to join the channel <##{channel_found.id}> to be able to use the rules.", dest
664
576
  end
665
577
  end
666
- else
667
- @logger.info MASTER_CHANNEL
668
- @logger.info @channel_id.inspect
669
- respond "Sorry I cannot create bots from this channel, please visit the master channel: <##{@channels_id[MASTER_CHANNEL]}>", dest
670
- end
671
578
 
672
- #helpmaster: ----------------------------------------------
673
- #helpmaster: `kill bot on CHANNEL_NAME`
674
- #helpmaster: kills the bot on the specified channel
675
- #helpmaster: Only works if you are on Master channel and you created that bot or you are an admin user
676
- #helpmaster:
677
- when /^kill\sbot\son\s<#C\w+\|(.+)>\s*$/i, /^kill\sbot\son\s(.+)\s*$/i
678
- if ON_MASTER_BOT
679
- channel = $1
680
-
681
- get_channels_name_and_id() unless @channels_name.keys.include?(channel) or @channels_id.keys.include?(channel)
682
- channel_id = nil
683
- if @channels_name.key?(channel) #it is an id
684
- channel_id = channel
685
- channel = @channels_name[channel_id]
686
- elsif @channels_id.key?(channel) #it is a channel name
579
+ #help: ----------------------------------------------
580
+ #help: `stop using rules from CHANNEL`
581
+ #help: it will stop using the rules from the specified channel.
582
+ #help:
583
+ when /^stop using rules (from\s+)<#C\w+\|(.+)>/i, /^stop using rules (from\s+)(.+)/i
584
+ channel = $2
585
+ if @channels_id.key?(channel)
687
586
  channel_id = @channels_id[channel]
587
+ else
588
+ channel_id = channel
688
589
  end
689
- if channel_id.nil?
690
- respond "There is no channel with that name: #{channel}, please be sure is written exactly the same", dest
691
- elsif @bots_created.keys.include?(channel_id)
692
- if @bots_created[channel_id][:admins].split(",").include?(from)
693
- if @bots_created[channel_id][:thread].kind_of?(Thread) and @bots_created[channel_id][:thread].alive?
694
- @bots_created[channel_id][:thread].kill
590
+ if dest[0] == "C" or dest[0] == "G" #channel
591
+ if @rules_imported.key?(user.id) and @rules_imported[user.id].key?(dchannel)
592
+ if @rules_imported[user.id][dchannel] != channel_id
593
+ respond "You are not using those rules.", dest
594
+ else
595
+ @rules_imported[user.id].delete(dchannel)
596
+ update_rules_imported() if ON_MASTER_BOT
597
+ respond "You won't be using those rules from now on.", dest
598
+ def git_project() "" end
599
+ def project_folder() "" end
695
600
  end
696
- @bots_created.delete(channel_id)
697
- update_bots_file()
698
- respond "Bot on channel: #{channel}, has been killed and deleted.", dest
699
- send_msg_channel(channel, "Bot has been killed by #{from}")
700
601
  else
701
- respond "You need to be the creator or an admin of that bot channel", dest
602
+ respond "You were not using those rules.", dest
702
603
  end
703
- else
704
- respond "There is no bot in this channel: #{channel}", dest
705
- end
706
- else
707
- respond "Sorry I cannot kill bots from this channel, please visit the master channel: <##{@channels_id[MASTER_CHANNEL]}>", dest
708
- end
709
-
710
- #help: ----------------------------------------------
711
- #help: `use rules from CHANNEL`
712
- #help: `use rules CHANNEL`
713
- #help: it will use the rules from the specified channel.
714
- #help: you need to be part of that channel to be able to use the rules.
715
- #help:
716
- when /^use rules (from\s+)?<#C\w+\|(.+)>/i, /^use rules (from\s+)?(.+)/i
717
- channel = $2
718
- channels = client.web_client.channels_list.channels
719
- channel_found = channels.detect { |c| c.name == channel }
720
- members = client.web_client.conversations_members(channel: @channels_id[channel]).members unless channel_found.nil?
721
-
722
- if channel_found.nil?
723
- respond "The channel you are trying to use doesn't exist", dest
724
- elsif channel_found.name == MASTER_CHANNEL
725
- respond "You cannot use the rules from Master Channel on any other channel.", dest
726
- elsif !@bots_created.key?(@channels_id[channel])
727
- respond "There is no bot running on that channel.", dest
728
- elsif @bots_created.key?(@channels_id[channel]) and @bots_created[@channels_id[channel]][:status] != :on
729
- respond "The bot in that channel is not :on", dest
730
- else
731
- if user.id == channel_found.creator or members.include?(user.id)
732
- @rules_imported[user.id] = {} unless @rules_imported.key?(user.id)
733
- if dest[0] == "C" or dest[0] == "G" #todo: take in consideration bots that are not master
734
- @rules_imported[user.id][dchannel] = channel_found.id
604
+ else #direct message
605
+ if @rules_imported.key?(user.id) and @rules_imported[user.id].key?(user.id)
606
+ if @rules_imported[user.id][user.id] != channel_id
607
+ respond "You are not using those rules.", dest
608
+ else
609
+ @rules_imported[user.id].delete(user.id)
610
+ update_rules_imported() if ON_MASTER_BOT
611
+ respond "You won't be using those rules from now on.", dest
612
+ def git_project() "" end
613
+ def project_folder() "" end
614
+ end
735
615
  else
736
- @rules_imported[user.id][user.id] = channel_found.id
616
+ respond "You were not using those rules.", dest
737
617
  end
738
- update_rules_imported() if ON_MASTER_BOT
739
- respond "I'm using now the rules from <##{channel_found.id}>", dest
740
- def git_project() "" end
741
- def project_folder() "" end
742
- else
743
- respond "You need to join the channel <##{channel_found.id}> to be able to use the rules.", dest
744
618
  end
745
- end
746
619
 
747
- #help: ----------------------------------------------
748
- #help: `stop using rules from CHANNEL`
749
- #help: `stop using rules CHANNEL`
750
- #help: it will stop using the rules from the specified channel.
751
- #help:
752
- when /^stop using rules (from\s+)?<#C\w+\|(.+)>/i, /^stop using rules (from\s+)?(.+)/i
753
- channel = $2
754
- if @channels_id.key?(channel)
755
- channel_id = @channels_id[channel]
756
- else
757
- channel_id = channel
758
- end
759
- if dest[0] == "C" or dest[0] == "G" #channel
760
- if @rules_imported.key?(user.id) and @rules_imported[user.id].key?(dchannel)
761
- if @rules_imported[user.id][dchannel] != channel_id
762
- respond "You are not using those rules.", dest
620
+ #helpadmin: ----------------------------------------------
621
+ #helpadmin: `exit bot`
622
+ #helpadmin: `quit bot`
623
+ #helpadmin: `close bot`
624
+ #helpadmin: The bot stops running and also stops all the bots created from this master channel
625
+ #helpadmin: You can use this command only if you are an admin user and you are on the master channel
626
+ #helpadmin:
627
+ when /^exit\sbot\s*$/i, /^quit\sbot\s*$/i, /^close\sbot\s*$/i
628
+ if ON_MASTER_BOT
629
+ if ADMIN_USERS.include?(from) #admin user
630
+ unless @questions.keys.include?(from)
631
+ ask("are you sure?", command, from, dest)
632
+ else
633
+ case @questions[from]
634
+ when /yes/i, /yep/i, /sure/i
635
+ respond "Game over!", dest
636
+ respond "Ciao #{firstname}!", dest
637
+ @bots_created.each { |key, value|
638
+ value[:thread] = ""
639
+ send_msg_channel(key, "Bot has been closed by #{from}")
640
+ sleep 0.5
641
+ }
642
+ update_bots_file()
643
+ sleep 0.5
644
+ exit!
645
+ when /no/i, /nope/i, /cancel/i
646
+ @questions.delete(from)
647
+ respond "Thanks, I'm happy to be alive", dest
648
+ else
649
+ respond "I don't understand", dest
650
+ ask("are you sure do you want me to close? (yes or no)", "quit bot", from, dest)
651
+ end
652
+ end
763
653
  else
764
- @rules_imported[user.id].delete(dchannel)
765
- update_rules_imported() if ON_MASTER_BOT
766
- respond "You won't be using those rules from now on.", dest
767
- def git_project() "" end
768
- def project_folder() "" end
654
+ respond "Only admin users can kill me", dest
769
655
  end
770
656
  else
771
- respond "You were not using those rules.", dest
657
+ respond "To do this you need to be an admin user in the master channel: <##{@channels_id[MASTER_CHANNEL]}>", dest
772
658
  end
773
- else #direct message
774
- if @rules_imported.key?(user.id) and @rules_imported[user.id].key?(user.id)
775
- if @rules_imported[user.id][user.id] != channel_id
776
- respond "You are not using those rules.", dest
777
- else
778
- @rules_imported[user.id].delete(user.id)
779
- update_rules_imported() if ON_MASTER_BOT
780
- respond "You won't be using those rules from now on.", dest
781
- def git_project() "" end
782
- def project_folder() "" end
659
+
660
+ #helpadmin: ----------------------------------------------
661
+ #helpadmin: `start bot`
662
+ #helpadmin: `start this bot`
663
+ #helpadmin: the bot will start to listen
664
+ #helpadmin: You can use this command only if you are an admin user
665
+ #helpadmin:
666
+ when /^start\s(this\s)?bot$/i
667
+ if ADMIN_USERS.include?(from) #admin user
668
+ respond "This bot is running and listening from now on. You can pause again: pause this bot", dest
669
+ @status = :on
670
+ unless ON_MASTER_BOT
671
+ send_msg_channel MASTER_CHANNEL, "Changed status on #{CHANNEL} to :on"
783
672
  end
784
673
  else
785
- respond "You were not using those rules.", dest
674
+ respond "Only admin users can change my status", dest
786
675
  end
787
- end
788
676
 
789
- #help: ----------------------------------------------
790
- #help: `bot help`
791
- #help: `bot what can I do?`
792
- #help: `bot rules`
793
- #help: it will display this help
794
- #help: `bot rules` will show only the specific rules for this channel.
795
- when /^bot (rules|help)/i, /^bot,? what can I do/i
796
- if $1.to_s.match?(/rules/i)
797
- specific = true
798
- else
799
- specific = false
800
- end
801
- help_message_rules = ''
802
- if !specific
803
- help_message = IO.readlines(__FILE__).join
677
+ #helpadmin: ----------------------------------------------
678
+ #helpadmin: `pause bot`
679
+ #helpadmin: `pause this bot`
680
+ #helpadmin: the bot will pause so it will listen only to admin commands
681
+ #helpadmin: You can use this command only if you are an admin user
682
+ #helpadmin:
683
+ when /^pause\s(this\s)?bot$/i
804
684
  if ADMIN_USERS.include?(from) #admin user
805
- respond "*Commands for administrators:*\n#{help_message.scan(/#\s*help\s*admin:(.*)/).join("\n")}", dest
685
+ respond "This bot is paused from now on. You can start it again: start this bot", dest
686
+ respond "zZzzzzZzzzzZZZZZZzzzzzzzz", dest
687
+ @status = :paused
688
+ unless ON_MASTER_BOT
689
+ send_msg_channel MASTER_CHANNEL, "Changed status on #{CHANNEL} to :paused"
690
+ end
691
+ else
692
+ respond "Only admin users can put me on pause", dest
806
693
  end
807
- if ON_MASTER_BOT and (dest[0] == "C" or dest[0] == "G")
808
- respond "*Commands only on Master Channel <##{@channels_id[MASTER_CHANNEL]}>:*\n#{help_message.scan(/#\s*help\s*master:(.*)/).join("\n")}", dest
694
+
695
+ #helpadmin: ----------------------------------------------
696
+ #helpadmin: `bot status`
697
+ #helpadmin: Displays the status of the bot
698
+ #helpadmin: If on master channel and admin user also it will display info about bots created
699
+ #helpadmin:
700
+ when /^bot\sstatus/i
701
+ get_bots_created()
702
+ gems_remote = `gem list slack-smart-bot --remote`
703
+ version_remote = gems_remote.to_s().scan(/slack-smart-bot \((\d+\.\d+\.\d+)/).join
704
+ version_message = ""
705
+ if version_remote != VERSION
706
+ version_message = " There is a new available version: #{version_remote}."
809
707
  end
810
- respond help_message.scan(/#\s*help\s*:(.*)/).join("\n"), dest
811
- end
812
- if dest[0] == "C" or dest[0] == "G" # on a channel
813
- if @rules_imported.key?(user.id) and @rules_imported[user.id].key?(dchannel)
814
- if @bots_created.key?(@rules_imported[user.id][dchannel])
815
- respond "*You are using rules from another channel: <##{@rules_imported[user.id][dchannel]}>. These are the specific commands for that channel:*", dest
708
+ respond "Status: #{@status}. Version: #{VERSION}.#{version_message} Rules file: #{File.basename RULES_FILE} ", dest
709
+ if @status == :on
710
+ respond "I'm listening to [#{@listening.join(", ")}]", dest
711
+ if ON_MASTER_BOT and ADMIN_USERS.include?(from)
712
+ @bots_created.each { |key, value|
713
+ respond "#{key}: #{value}", dest
714
+ }
816
715
  end
817
716
  end
818
- help_message_rules = IO.readlines(rules_file).join
819
- respond help_message_rules.scan(/#\s*help\s*:(.*)/).join("\n"), dest
820
- elsif dest[0] == "D" and @rules_imported.key?(user.id) and @rules_imported[user.id].key?(user.id) #direct message
821
- if @bots_created.key?(@rules_imported[user.id][user.id])
822
- respond "*You are using rules from channel: <##{@rules_imported[user.id][user.id]}>. These are the specific commands for that channel:*", dest
823
- help_message_rules = IO.readlines(rules_file).join
824
- respond help_message_rules.scan(/#\s*help\s*:(.*)/).join("\n"), dest
717
+
718
+ #helpmaster: ----------------------------------------------
719
+ #helpmaster: `notify MESSAGE`
720
+ #helpmaster: `notify all MESSAGE`
721
+ #helpmaster: It will send a notificaiton message to all bot channels
722
+ #helpmaster: It will send a notification message to all channels the bot joined and private conversations with the bot
723
+ #helpmaster: Only works if you are on Master channel and you are an admin user
724
+ #helpmaster:
725
+ when /^notify\s+(all)?\s*(.+)\s*$/i
726
+ if ON_MASTER_BOT
727
+ if ADMIN_USERS.include?(from) #admin user
728
+ all = $1
729
+ message = $2
730
+ if all.nil?
731
+ @bots_created.each do |k, v|
732
+ respond message, k
733
+ end
734
+ respond "Bot channels have been notified", dest
735
+ else
736
+ myconv = client.web_client.users_conversations(exclude_archived: true, limit: 100, types: "im, public_channel").channels
737
+ myconv.each do |c|
738
+ respond message, c.id unless c.name == MASTER_CHANNEL
739
+ end
740
+ respond "Channels and users have been notified", dest
741
+ end
742
+ end
825
743
  end
826
- end
827
- if specific
828
- unless rules_file.empty?
829
- begin
830
- eval(File.new(rules_file).read) if File.exist?(rules_file)
744
+
745
+ #helpmaster: ----------------------------------------------
746
+ #helpmaster: `create bot on CHANNEL_NAME`
747
+ #helpmaster: creates a new bot on the channel specified
748
+ #helpmaster: it will work only if you are on Master channel
749
+ #helpmaster: the admins will be the master admins, the creator of the bot and the creator of the channel
750
+ #helpmaster:
751
+ when /^create\sbot\son\s<#C\w+\|(.+)>\s*/i, /^create\sbot\son\s(.+)\s*/i
752
+ if ON_MASTER_BOT
753
+ channel = $1
754
+
755
+ get_channels_name_and_id() unless @channels_name.keys.include?(channel) or @channels_id.keys.include?(channel)
756
+ channel_id = nil
757
+ if @channels_name.key?(channel) #it is an id
758
+ channel_id = channel
759
+ channel = @channels_name[channel_id]
760
+ elsif @channels_id.key?(channel) #it is a channel name
761
+ channel_id = @channels_id[channel]
831
762
  end
763
+ channels = client.web_client.channels_list.channels
764
+ channel_found = channels.detect { |c| c.name == channel }
765
+ members = client.web_client.conversations_members(channel: @channels_id[channel]).members unless channel_found.nil?
766
+
767
+ if channel_id.nil?
768
+ respond "There is no channel with that name: #{channel}, please be sure is written exactly the same", dest
769
+ elsif channel == MASTER_CHANNEL
770
+ respond "There is already a bot in this channel: #{channel}", dest
771
+ elsif @bots_created.keys.include?(channel_id)
772
+ respond "There is already a bot in this channel: #{channel}, kill it before", dest
773
+ elsif config[:nick_id] != channel_found.creator and !members.include?(config[:nick_id])
774
+ respond "You need to add first to the channel the smart bot user: #{config[:nick]}", dest
775
+ else
776
+ if channel_id != config[:channel]
777
+ begin
778
+ rules_file = "slack-smart-bot_rules_#{channel_id}_#{from.gsub(" ", "_")}.rb"
779
+ if defined?(RULES_FOLDER)
780
+ rules_file = RULES_FOLDER + rules_file
781
+ else
782
+ Dir.mkdir("rules") unless Dir.exist?("rules")
783
+ Dir.mkdir("rules/#{channel_id}") unless Dir.exist?("rules/#{channel_id}")
784
+ rules_file = "./rules/#{channel_id}/" + rules_file
785
+ end
786
+ default_rules = (__FILE__).gsub(/\.rb$/, "_rules.rb")
787
+ File.delete(rules_file) if File.exist?(rules_file)
788
+ FileUtils.copy_file(default_rules, rules_file) unless File.exist?(rules_file)
789
+ admin_users = Array.new()
790
+ creator_info = client.web_client.users_info(user: channel_found.creator)
791
+ admin_users = [from, creator_info.user.name] + MASTER_USERS
792
+ admin_users.uniq!
793
+ @logger.info "ruby #{$0} \"#{channel}\" \"#{admin_users.join(",")}\" \"#{rules_file}\" on"
794
+ t = Thread.new do
795
+ `ruby #{$0} \"#{channel}\" \"#{admin_users.join(",")}\" \"#{rules_file}\" on`
796
+ end
797
+ @bots_created[channel_id] = {
798
+ creator_name: from,
799
+ channel_id: channel_id,
800
+ channel_name: @channels_name[channel_id],
801
+ status: :on,
802
+ created: Time.now.strftime("%Y-%m-%dT%H:%M:%S.000Z")[0..18],
803
+ rules_file: rules_file,
804
+ admins: admin_users.join(","),
805
+ extended: [],
806
+ thread: t,
807
+ }
808
+ respond "The bot has been created on channel: #{channel}. Rules file: #{File.basename rules_file}. Admins: #{admin_users.join(", ")}", dest
809
+ update_bots_file()
810
+ rescue Exception => stack
811
+ @logger.fatal stack
812
+ message = "Problem creating the bot on channel #{channel}. Error: <#{stack}>."
813
+ @logger.error message
814
+ respond message, dest
815
+ end
816
+ else
817
+ respond "There is already a bot in this channel: #{channel}, and it is the Master Channel!", dest
818
+ end
819
+ end
820
+ else
821
+ @logger.info MASTER_CHANNEL
822
+ @logger.info @channel_id.inspect
823
+ respond "Sorry I cannot create bots from this channel, please visit the master channel: <##{@channels_id[MASTER_CHANNEL]}>", dest
832
824
  end
833
- if defined?(git_project) and git_project.to_s!='' and help_message_rules != ''
834
- respond "Git project: #{git_project}", dest
825
+
826
+ #helpmaster: ----------------------------------------------
827
+ #helpmaster: `kill bot on CHANNEL_NAME`
828
+ #helpmaster: kills the bot on the specified channel
829
+ #helpmaster: Only works if you are on Master channel and you created that bot or you are an admin user
830
+ #helpmaster:
831
+ when /^kill\sbot\son\s<#C\w+\|(.+)>\s*$/i, /^kill\sbot\son\s(.+)\s*$/i
832
+ if ON_MASTER_BOT
833
+ channel = $1
834
+
835
+ get_channels_name_and_id() unless @channels_name.keys.include?(channel) or @channels_id.keys.include?(channel)
836
+ channel_id = nil
837
+ if @channels_name.key?(channel) #it is an id
838
+ channel_id = channel
839
+ channel = @channels_name[channel_id]
840
+ elsif @channels_id.key?(channel) #it is a channel name
841
+ channel_id = @channels_id[channel]
842
+ end
843
+ if channel_id.nil?
844
+ respond "There is no channel with that name: #{channel}, please be sure is written exactly the same", dest
845
+ elsif @bots_created.keys.include?(channel_id)
846
+ if @bots_created[channel_id][:admins].split(",").include?(from)
847
+ if @bots_created[channel_id][:thread].kind_of?(Thread) and @bots_created[channel_id][:thread].alive?
848
+ @bots_created[channel_id][:thread].kill
849
+ end
850
+ @bots_created.delete(channel_id)
851
+ update_bots_file()
852
+ respond "Bot on channel: #{channel}, has been killed and deleted.", dest
853
+ send_msg_channel(channel, "Bot has been killed by #{from}")
854
+ else
855
+ respond "You need to be the creator or an admin of that bot channel", dest
856
+ end
857
+ else
858
+ respond "There is no bot in this channel: #{channel}", dest
859
+ end
835
860
  else
836
- def git_project() '' end
837
- def project_folder() '' end
861
+ respond "Sorry I cannot kill bots from this channel, please visit the master channel: <##{@channels_id[MASTER_CHANNEL]}>", dest
838
862
  end
839
-
863
+
864
+
865
+
840
866
  else
841
- respond "Slack Smart Bot Github project: https://github.com/MarioRuiz/slack-smart-bot", dest
867
+ processed = false
842
868
  end
869
+ only_on_demand = false
843
870
  else
844
871
  processed = false
872
+ only_on_demand = true
845
873
  end
846
874
 
847
875
  on_demand = false
@@ -855,7 +883,7 @@ class SlackSmartBot
855
883
  #only when :on and (listening or on demand or direct message)
856
884
  if @status == :on and
857
885
  (@questions.keys.include?(from) or
858
- @listening.include?(from) or
886
+ (@listening.include?(from) and !only_on_demand) or
859
887
  dest[0] == "D" or on_demand)
860
888
  processed2 = true
861
889
 
@@ -868,6 +896,97 @@ class SlackSmartBot
868
896
  #help:
869
897
  case command
870
898
 
899
+ when /^bot\s+rules$/i
900
+ if only_on_demand #extended rules
901
+ help_message_rules = ''
902
+ message = "-\n\n\n===================================\n*Rules from channel <##{@channels_id[CHANNEL]}>*\n"
903
+ message += "To run the commands on this extended channel, add `!` before the command.\n"
904
+ help_message_rules = IO.readlines(rules_file).join
905
+ message += help_message_rules.scan(/#\s*help\s*:(.*)/).join("\n")
906
+ respond message, dest
907
+ unless rules_file.empty?
908
+ begin
909
+ eval(File.new(rules_file).read) if File.exist?(rules_file)
910
+ end
911
+ end
912
+ if defined?(git_project) and git_project.to_s!='' and help_message_rules != ''
913
+ respond "Git project: #{git_project}", dest
914
+ else
915
+ def git_project() '' end
916
+ def project_folder() '' end
917
+ end
918
+ end
919
+
920
+
921
+
922
+ #helpadmin: ----------------------------------------------
923
+ #helpadmin: `extend rules to CHANNEL_NAME`
924
+ #helpadmin: `use rules on CHANNEL_NAME`
925
+ #helpadmin: It will allow to use the specific rules from this channel on the CHANNEL_NAME
926
+ #helpadmin:
927
+ when /^extend\s+rules\s+(to\s+)<#C\w+\|(.+)>/i, /^extend\s+rules\s+(to\s+)(.+)/i,
928
+ /^use\s+rules\s+(on\s+)<#C\w+\|(.+)>/i, /^use\s+rules\s+(on\s+)(.+)/i
929
+ if ON_MASTER_BOT
930
+ respond "You cannot use the rules from Master Channel on any other channel.", dest
931
+ elsif !ADMIN_USERS.include?(from) #not admin
932
+ respond "Only admins can extend the rules. Admins on this channel: #{ADMIN_USERS}", dest
933
+ else
934
+ channel = $2
935
+ channels = client.web_client.channels_list.channels
936
+ channel_found = channels.detect { |c| c.name == channel }
937
+ members = client.web_client.conversations_members(channel: @channels_id[channel]).members unless channel_found.nil?
938
+ get_bots_created()
939
+ channels_in_use = []
940
+ @bots_created.each do |k,v|
941
+ if v.key?(:extended) and v[:extended].include?(channel)
942
+ channels_in_use << v[:channel_name]
943
+ end
944
+ end
945
+
946
+ if channel_found.nil?
947
+ respond "The channel you specified doesn't exist", dest
948
+ elsif @bots_created.key?(@channels_id[channel])
949
+ respond "There is a bot already running on that channel.", dest
950
+ elsif @bots_created[@channels_id[CHANNEL]][:extended].include?(channel)
951
+ respond "The rules are already extended to that channel.", dest
952
+ elsif !members.include?(config[:nick_id])
953
+ respond "You need to add first to the channel the smart bot user: #{config[:nick]}", dest
954
+ elsif !members.include?(user.id)
955
+ respond "You need to join that channel first", dest
956
+ else
957
+ channels_in_use.each do |channel_in_use|
958
+ respond "The rules from channel <##{@channels_id[channel_in_use]}> are already in use on that channel", dest
959
+ end
960
+ @bots_created[@channels_id[CHANNEL]][:extended] = [] unless @bots_created[@channels_id[CHANNEL]].key?(:extended)
961
+ @bots_created[@channels_id[CHANNEL]][:extended] << channel
962
+ update_bots_file()
963
+ respond "Now the rules from <##{@channels_id[CHANNEL]}> are available on <##{@channels_id[channel]}>", dest
964
+ respond "<@#{user.id}> extended the rules from <##{@channels_id[CHANNEL]}> to this channel so now you can talk to the Smart Bot on demand using those rules.", @channels_id[channel]
965
+ respond "Use `!` before the command you want to run", @channels_id[channel]
966
+ respond "To see the specific rules for this bot on this channel: `!bot rules`", @channels_id[channel]
967
+ end
968
+ end
969
+
970
+ #help: ----------------------------------------------
971
+ #help: `stop using rules on CHANNEL_NAME`
972
+ #help: it will stop using the extended rules on the specified channel.
973
+ #help:
974
+ when /^stop using rules (on\s+)<#C\w+\|(.+)>/i, /^stop using rules (on\s+)(.+)/i
975
+ if !ADMIN_USERS.include?(from) #not admin
976
+ respond "Only admins can extend or stop using the rules. Admins on this channel: #{ADMIN_USERS}", dest
977
+ else
978
+ channel = $2
979
+ get_bots_created()
980
+ if @bots_created[@channels_id[CHANNEL]][:extended].include?(channel)
981
+ @bots_created[@channels_id[CHANNEL]][:extended].delete(channel)
982
+ update_bots_file()
983
+ respond "The rules won't be accessible from <##{@channels_id[CHANNEL]}> from now on.", dest
984
+ respond "<@#{user.id}> removed the access to the rules of <##{@channels_id[CHANNEL]}> from this channel.", @channels_id[channel]
985
+ else
986
+ respond "The rules were not accessible from <##{@channels_id[channel]}>", dest
987
+ end
988
+ end
989
+
871
990
  #help: ----------------------------------------------
872
991
  #help: `add shortcut NAME: COMMAND`
873
992
  #help: `add sc NAME: COMMAND`
@@ -1127,5 +1246,5 @@ class SlackSmartBot
1127
1246
  )
1128
1247
  end
1129
1248
 
1130
- private :update_bots_file, :get_channels_name_and_id, :update_shortcuts_file
1249
+ private :update_bots_file, :get_bots_created, :get_channels_name_and_id, :update_shortcuts_file
1131
1250
  end
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.7.6
4
+ version: 0.8.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-08-21 00:00:00.000000000 Z
11
+ date: 2019-08-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: slack-ruby-client