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
@@ -4,25 +4,22 @@ class SlackSmartBot
4
4
  # help: `run repl SESSION_NAME ENV_VAR=VALUE ENV_VAR=VALUE`
5
5
  # help: `run live SESSION_NAME`
6
6
  # help: `run irb SESSION_NAME`
7
- # help:
8
7
  # help: Will run the repl session specified and return the output.
9
8
  # help: You can supply the Environmental Variables you need for the Session
10
9
  # help: It will return only the values that were print out on the repl with puts, print, p or pp
11
10
  # help: Example:
12
11
  # help: _run repl CreateCustomer LOCATION=spain HOST='https://10.30.40.50:8887'_
12
+ # help: <https://github.com/MarioRuiz/slack-smart-bot#repl|more info>
13
13
  # help:
14
14
  def run_repl(dest, user, session_name, env_vars, rules_file)
15
15
  #todo: add tests
16
16
  from = user.name
17
- if config[:allow_access].key?(__method__) and !config[:allow_access][__method__].include?(user.name) and !config[:allow_access][__method__].include?(user.id) and
18
- (!user.key?(:enterprise_user) or ( user.key?(:enterprise_user) and !config[:allow_access][__method__].include?(user[:enterprise_user].id)))
19
- respond "You don't have access to use this command, please contact an Admin to be able to use it: <@#{config.admins.join(">, <@")}>"
20
- else
17
+ if has_access?(__method__, user)
21
18
  save_stats(__method__)
22
19
  Dir.mkdir("#{config.path}/repl") unless Dir.exist?("#{config.path}/repl")
23
20
  Dir.mkdir("#{config.path}/repl/#{@channel_id}") unless Dir.exist?("#{config.path}/repl/#{@channel_id}")
24
21
  if File.exist?("#{config.path}/repl/#{@channel_id}/#{session_name}.run")
25
- if @repls.key?(session_name) and @repls[session_name][:type] == :private and
22
+ if @repls.key?(session_name) and (@repls[session_name][:type] == :private or @repls[session_name][:type] == :private_clean) and
26
23
  @repls[session_name][:creator_name]!=user.name and
27
24
  !config.admins.include?(user.name)
28
25
  respond "The REPL with session name: #{session_name} is private", dest
@@ -44,7 +41,8 @@ class SlackSmartBot
44
41
  eval(File.new(config.path+rules_file).read) if File.exist?(config.path+rules_file)
45
42
  end
46
43
  end
47
- if File.exist?("#{project_folder}/.smart-bot-repl")
44
+ if File.exist?("#{project_folder}/.smart-bot-repl") and
45
+ ((@repls.key?(session_name) and @repls[session_name][:type] != :private_clean and @repls[session_name][:type] != :public_clean) or !@repls.key?(session_name))
48
46
  content += File.read("#{project_folder}/.smart-bot-repl")
49
47
  content += "\n"
50
48
  end
@@ -2,19 +2,17 @@ class SlackSmartBot
2
2
  # help: ----------------------------------------------
3
3
  # help: `see repls`
4
4
  # help: `see irbs`
5
- # help: It will display the repls
5
+ # help: It will display the repls
6
+ # help: <https://github.com/MarioRuiz/slack-smart-bot#repl|more info>
6
7
  # help:
7
8
  def see_repls(dest, user, typem)
8
9
  #todo: add tests
9
10
  save_stats(__method__)
10
11
  from = user.name
11
- if config[:allow_access].key?(__method__) and !config[:allow_access][__method__].include?(user.name) and !config[:allow_access][__method__].include?(user.id) and
12
- (!user.key?(:enterprise_user) or ( user.key?(:enterprise_user) and !config[:allow_access][__method__].include?(user[:enterprise_user].id)))
13
- respond "You don't have access to use this command, please contact an Admin to be able to use it: <@#{config.admins.join(">, <@")}>"
14
- else
12
+ if has_access?(__method__, user)
15
13
  message = ""
16
14
  @repls.sort.to_h.each do |session_name, repl|
17
- if (repl.creator_name == user.name or repl.type == :public) or (config.admins.include?(user.name) and typem == :on_dm)
15
+ if (repl.creator_name == user.name or repl.type == :public or repl.type == :public_clean) or (config.admins.include?(user.name) and typem == :on_dm)
18
16
  message += "(#{repl.type}) *#{session_name}*: #{repl.description} / created: #{repl.created} / accessed: #{repl.accessed} / creator: #{repl.creator_name} / runs: #{repl.runs_by_creator+repl.runs_by_others} / gets: #{repl.gets} \n"
19
17
  end
20
18
  end
@@ -3,36 +3,52 @@ class SlackSmartBot
3
3
  # help: `see shortcuts`
4
4
  # help: `see sc`
5
5
  # help: It will display the shortcuts stored for the user and for :all
6
+ # help: <https://github.com/MarioRuiz/slack-smart-bot#shortcuts|more info>
6
7
  # help:
7
8
  def see_shortcuts(dest, user, typem)
8
9
  save_stats(__method__)
9
10
  from = user.name
10
- if config[:allow_access].key?(__method__) and !config[:allow_access][__method__].include?(user.name) and !config[:allow_access][__method__].include?(user.id) and
11
- (!user.key?(:enterprise_user) or ( user.key?(:enterprise_user) and !config[:allow_access][__method__].include?(user[:enterprise_user].id)))
12
- respond "You don't have access to use this command, please contact an Admin to be able to use it: <@#{config.admins.join(">, <@")}>"
13
- else
11
+ if has_access?(__method__, user)
14
12
  unless typem == :on_extended
15
13
  msg = ""
16
- if @shortcuts[:all].keys.size > 0
14
+ if @shortcuts[:all].keys.size > 0 or @shortcuts_global[:all].keys.size > 0
17
15
  msg = "*Available shortcuts for all:*\n"
18
- @shortcuts[:all].each { |name, value|
19
- msg += " _#{name}: #{value}_\n"
20
- }
16
+
17
+ if @shortcuts[:all].keys.size > 0
18
+ @shortcuts[:all].each { |name, value|
19
+ msg += " _#{name}: #{value}_\n"
20
+ }
21
+ end
22
+ if @shortcuts_global[:all].keys.size > 0
23
+ @shortcuts_global[:all].each { |name, value|
24
+ msg += " _#{name} (global): #{value}_\n"
25
+ }
26
+ end
21
27
  respond msg, dest
22
28
  end
23
-
29
+ msg2 = ''
24
30
  if @shortcuts.keys.include?(from) and @shortcuts[from].keys.size > 0
25
31
  new_hash = @shortcuts[from].dup
26
32
  @shortcuts[:all].keys.each { |k| new_hash.delete(k) }
27
33
  if new_hash.keys.size > 0
28
- msg = "*Available shortcuts for #{from}:*\n"
34
+ msg2 = "*Available shortcuts for #{from}:*\n"
29
35
  new_hash.each { |name, value|
30
- msg += " _#{name}: #{value}_\n"
36
+ msg2 += " _#{name}: #{value}_\n"
37
+ }
38
+ end
39
+ end
40
+ if @shortcuts_global.keys.include?(from) and @shortcuts_global[from].keys.size > 0
41
+ new_hash = @shortcuts_global[from].dup
42
+ @shortcuts_global[:all].keys.each { |k| new_hash.delete(k) }
43
+ if new_hash.keys.size > 0
44
+ msg2 = "*Available shortcuts for #{from}:*\n" if msg2 == ''
45
+ new_hash.each { |name, value|
46
+ msg2 += " _#{name} (global): #{value}_\n"
31
47
  }
32
- respond msg, dest
33
48
  end
34
49
  end
35
- respond "No shortcuts found", dest if msg == ""
50
+ respond msg2 unless msg2 == ''
51
+ respond "No shortcuts found" if (msg + msg2) == ""
36
52
  end
37
53
  end
38
54
  end
@@ -2,29 +2,59 @@ class SlackSmartBot
2
2
  def bot_rules(dest, help_command, typem, rules_file, user)
3
3
  save_stats(__method__)
4
4
  from = user.name
5
- if config[:allow_access].key?(__method__) and !config[:allow_access][__method__].include?(user.name) and !config[:allow_access][__method__].include?(user.id) and
6
- (!user.key?(:enterprise_user) or ( user.key?(:enterprise_user) and !config[:allow_access][__method__].include?(user[:enterprise_user].id)))
7
- respond "You don't have access to use this command, please contact an Admin to be able to use it: <@#{config.admins.join(">, <@")}>"
8
- else
5
+ if has_access?(__method__, user)
9
6
  if typem == :on_extended or typem == :on_call #for the other cases above.
10
- help_filtered = get_help(rules_file, dest, from, true)
7
+ output = []
8
+ if help_command.to_s != ''
9
+ help_command = '' if help_command.to_s.match?(/^\s*expanded\s*$/i) or help_command.to_s.match?(/^\s*extended\s*$/i)
10
+ expanded = true
11
+ else
12
+ expanded = false
13
+ end
14
+
15
+ help_filtered = get_help(rules_file, dest, from, true, expanded)
11
16
 
17
+ commands = []
18
+ commands_search = []
12
19
  if help_command.to_s != ""
13
20
  help_found = false
14
21
  help_filtered.split(/^\s*-------*$/).each do |h|
15
22
  if h.match?(/[`_]#{help_command}/i)
16
- respond "*#{config.channel}*:#{h}", dest
23
+ output << "*#{config.channel}*:#{h}"
17
24
  help_found = true
25
+ commands << h
26
+ elsif !h.match?(/\A\s*\*/) and !h.match?(/\A\s*=+/) #to avoid general messages for bot help *General rules...*
27
+ all_found = true
28
+ help_command.to_s.split(' ') do |hc|
29
+ unless hc.match?(/^\s*\z/)
30
+ if !h.match?(/#{hc}/i)
31
+ all_found = false
32
+ end
33
+ end
34
+ end
35
+ commands_search << h if all_found
36
+ end
37
+ end
38
+ if commands.size < 10 and help_command.to_s!='' and commands_search.size > 0
39
+ commands_search.shuffle!
40
+ (10-commands.size).times do |n|
41
+ unless commands_search[n].nil?
42
+ output << commands_search[n]
43
+ help_found = true
44
+ end
18
45
  end
19
46
  end
20
- respond("*#{config.channel}*: I didn't find any command starting by `#{help_command}`", dest) unless help_found
47
+ unless help_found
48
+ output << "*#{config.channel}*: I didn't find any command with `#{help_command}`"
49
+ end
50
+
21
51
  else
22
52
  message = "-\n\n\n===================================\n*Rules from channel #{config.channel}*\n"
23
53
  if typem == :on_extended
24
54
  message += "To run the commands on this extended channel, add `!`, `!!` or `^` before the command.\n"
25
55
  end
26
56
  message += help_filtered
27
- respond message, dest
57
+ output << message
28
58
  end
29
59
 
30
60
  unless rules_file.empty?
@@ -33,11 +63,27 @@ class SlackSmartBot
33
63
  end
34
64
  end
35
65
  if defined?(git_project) and git_project.to_s != "" and help_command.to_s == ""
36
- respond "Git project: #{git_project}", dest
66
+ output << "Git project: #{git_project}"
37
67
  else
38
68
  def git_project() "" end
39
69
  def project_folder() "" end
40
70
  end
71
+ unless expanded
72
+ message_not_expanded = "If you want to see the *expanded* version of *`bot rules`*, please call *`bot rules expanded`*\n"
73
+ message_not_expanded += "Also to get specific *expanded* help for a specific command or rule call *`bot rules COMMAND`*\n"
74
+ output << message_not_expanded
75
+ end
76
+ if output.join("\n").lines.count > 50 and dest[0]!='D'
77
+ dest = :on_thread
78
+ output.unshift('Since there are many lines returned the results are returned on a thread by default.')
79
+ end
80
+ output.each do |h|
81
+ msg = h.gsub(/^\s*command_id:\s+:\w+\s*$/,'')
82
+ unless msg.match?(/\A\s*\z/)
83
+ respond msg, dest, unfurl_links: false, unfurl_media: false
84
+ end
85
+ end
86
+
41
87
  end
42
88
  end
43
89
  end
@@ -3,6 +3,7 @@ class SlackSmartBot
3
3
  # helpmaster: `kill bot on CHANNEL_NAME`
4
4
  # helpmaster: kills the bot on the specified channel
5
5
  # helpmaster: Only works if you are on Master channel and you created that bot or you are an admin user
6
+ # helpmaster: <https://github.com/MarioRuiz/slack-smart-bot#bot-management|more info>
6
7
  # helpmaster:
7
8
  def kill_bot_on_channel(dest, from, channel)
8
9
  save_stats(__method__)
@@ -18,7 +19,8 @@ class SlackSmartBot
18
19
  if channel_id.nil?
19
20
  respond "There is no channel with that name: #{channel}, please be sure is written exactly the same", dest
20
21
  elsif @bots_created.keys.include?(channel_id)
21
- if @bots_created[channel_id][:admins].split(",").include?(from)
22
+ @bots_created[channel_id] ||= {}
23
+ if @bots_created[channel_id][:admins].to_s.split(",").include?(from)
22
24
  if @bots_created[channel_id][:thread].kind_of?(Thread) and @bots_created[channel_id][:thread].alive?
23
25
  @bots_created[channel_id][:thread].kill
24
26
  end
@@ -26,6 +28,7 @@ class SlackSmartBot
26
28
  update_bots_file()
27
29
  respond "Bot on channel: #{channel}, has been killed and deleted.", dest
28
30
  send_msg_channel(channel, "Bot has been killed by #{from}")
31
+ save_status :off, :killed, "The admin killed SmartBot on *<##{channel_id}|#{@channels_name[channel_id]}>*"
29
32
  else
30
33
  respond "You need to be the creator or an admin of that bot channel", dest
31
34
  end
@@ -6,21 +6,23 @@ class SlackSmartBot
6
6
  # helpadmin: `close bot`
7
7
  # helpadmin: The bot stops running and also stops all the bots created from this master channel
8
8
  # helpadmin: You can use this command only if you are an admin user and you are on the master channel
9
+ # helpadmin: <https://github.com/MarioRuiz/slack-smart-bot#bot-management|more info>
9
10
  # helpadmin:
10
11
  def exit_bot(command, from, dest, display_name)
11
12
  save_stats(__method__)
12
13
  if config.on_master_bot
13
14
  if config.admins.include?(from) #admin user
14
- unless @questions.keys.include?(from)
15
+ if answer.empty?
15
16
  ask("are you sure?", command, from, dest)
16
17
  else
17
- case @questions[from]
18
+ case answer
18
19
  when /yes/i, /yep/i, /sure/i
19
20
  respond "Game over!", dest
20
21
  respond "Ciao #{display_name}!", dest
21
22
  @bots_created.each { |key, value|
22
23
  value[:thread] = ""
23
24
  send_msg_channel(key, "Bot has been closed by #{from}")
25
+ save_status :off, :exited, "The admin closed SmartBot on *<##{key}|#{value.channel_name}>*"
24
26
  sleep 0.5
25
27
  }
26
28
  update_bots_file()
@@ -33,7 +35,7 @@ class SlackSmartBot
33
35
  exit!
34
36
  end
35
37
  when /no/i, /nope/i, /cancel/i
36
- @questions.delete(from)
38
+ answer_delete(from)
37
39
  respond "Thanks, I'm happy to be alive", dest
38
40
  else
39
41
  ask("I don't understand, are you sure do you want me to close? (yes or no)", command, from, dest)
@@ -7,6 +7,7 @@ class SlackSmartBot
7
7
  # helpmaster: It will send a notification message to all channels the bot joined and private conversations with the bot
8
8
  # helpmaster: It will send a notification message to the specified channel and to its extended channels
9
9
  # helpmaster: Only works if you are on Master channel and you are a master admin user
10
+ # helpmaster: <https://github.com/MarioRuiz/slack-smart-bot#sending-notifications|more info>
10
11
  # helpmaster:
11
12
  def notify_message(dest, from, where, message)
12
13
  save_stats(__method__)
@@ -18,7 +19,7 @@ class SlackSmartBot
18
19
  end
19
20
  respond "Bot channels have been notified", dest
20
21
  elsif where == "all" #all
21
- myconv = client.web_client.users_conversations(exclude_archived: true, limit: 100, types: "im, public_channel,private_channel").channels
22
+ myconv = get_channels(bot_is_in: true)
22
23
  myconv.each do |c|
23
24
  respond message, c.id unless c.name == config.master_channel
24
25
  end
@@ -0,0 +1,32 @@
1
+ class SlackSmartBot
2
+ # helpmaster: ----------------------------------------------
3
+ # helpmaster: `publish announcements`
4
+ # helpmaster: It will publish on all channels the announcements added by using 'add announcement' command.
5
+ # helpmaster: Only works if you are on Master channel and you are a master admin user
6
+ # helpmaster: The messages stored on a DM won't be published.
7
+ # helpmaster: This is very convenient to be called from a *Routine* for example every weekday at 09:00.
8
+ # helpmaster: <https://github.com/MarioRuiz/slack-smart-bot#announcements|more info>
9
+ # helpmaster:
10
+ def publish_announcements(user)
11
+ save_stats(__method__)
12
+ if config.on_master_bot
13
+ if config.admins.include?(user.name) #admin user
14
+ channels = Dir.entries("#{config.path}/announcements/")
15
+ channels.select! {|i| i[/\.csv$/]}
16
+ channels.each do |channel|
17
+ channel.gsub!('.csv','')
18
+ unless channel[0]== 'D'
19
+ see_announcements(user, '', channel, true, true)
20
+ sleep 0.5 # to avoid reach ratelimit
21
+ end
22
+ end
23
+ react :heavy_check_mark
24
+
25
+ else
26
+ respond 'Only master admins on master channel can use this command.'
27
+ end
28
+ else
29
+ respond 'Only master admins on master channel can use this command.'
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,38 @@
1
+ class SlackSmartBot
2
+ # helpmaster: ----------------------------------------------
3
+ # helpmaster: `set general message MESSAGE`
4
+ # helpmaster: `set general message off`
5
+ # helpmaster: The SmartBot will display the specified message after treating every command
6
+ # helpmaster: Only works if you are on Master channel and you are a master admin user
7
+ # helpmaster: You can add interpolation to the message you are adding
8
+ # helpmaster: Examples:
9
+ # helpmaster: _set general message We will be on maintenance at 12:00_
10
+ # helpmaster: _set general message We will be on maintenance in #{((Time.new(2021,6,18,13,30,0)-Time.now)/60).to_i} minutes_
11
+ # helpmaster: _set general message `We will be on *maintenance* at *12:00*`_
12
+ # helpmaster: _set general message `:information_source: Pay attention: We will be on *maintenance* in *#{((Time.new(2021,6,18,13,30,0)-Time.now)/60).to_i} minutes*`_
13
+ # helpmaster: <https://github.com/MarioRuiz/slack-smart-bot#bot-management|more info>
14
+ # helpmaster:
15
+ def set_general_message(from, status, message)
16
+ save_stats(__method__)
17
+ if config.on_master_bot
18
+ if config.admins.include?(from) #admin user
19
+ if status == 'on'
20
+ config.general_message = message
21
+ respond "General message has been set."
22
+ else
23
+ config.general_message = ''
24
+ respond "General message won't be displayed anymore."
25
+ end
26
+
27
+ file = File.open("#{config.path}/config_tmp.status", "w")
28
+ file.write config.inspect
29
+ file.close
30
+
31
+ else
32
+ respond 'Only master admins on master channel can use this command.'
33
+ end
34
+ else
35
+ respond 'Only master admins on master channel can use this command.'
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,49 @@
1
+ class SlackSmartBot
2
+ # helpmaster: ----------------------------------------------
3
+ # helpmaster: `set maintenance on`
4
+ # helpmaster: `set maintenance on MESSAGE`
5
+ # helpmaster: `set maintenance off`
6
+ # helpmaster: `turn maintenance on`
7
+ # helpmaster: `turn maintenance on MESSAGE`
8
+ # helpmaster: `turn maintenance off`
9
+ # helpmaster: The SmartBot will be on maintenance and responding with a generic message
10
+ # helpmaster: Only works if you are on Master channel and you are a master admin user
11
+ # helpmaster: You can add interpolation to the message you are adding
12
+ # helpmaster: Examples:
13
+ # helpmaster: _set maintenance on_
14
+ # helpmaster: _set maintenance on We are on maintenance. We'll be available again in #{((Time.new(2021,6,18,13,30,0)-Time.now)/60).to_i} minutes_
15
+ # helpmaster: _turn maintenance on `We are on *maintenance* until *12:00*`_
16
+ # helpmaster: <https://github.com/MarioRuiz/slack-smart-bot#bot-management|more info>
17
+ # helpmaster:
18
+ def set_maintenance(from, status, message)
19
+ save_stats(__method__)
20
+ if config.on_master_bot
21
+ if config.admins.include?(from) #admin user
22
+ if message == ''
23
+ config.on_maintenance_message = "Sorry I'm on maintenance so I cannot attend your request."
24
+ else
25
+ config.on_maintenance_message = message
26
+ end
27
+
28
+ if status == 'on'
29
+ config.on_maintenance = true
30
+ respond "From now on I'll be on maintenance status so I won't be responding accordingly."
31
+ save_status :off, :maintenance_on, config.on_maintenance_message
32
+ else
33
+ config.on_maintenance = false
34
+ respond "From now on I won't be on maintenance. Everything is back to normal!"
35
+ save_status :on, :maintenance_off, config.on_maintenance_message
36
+ end
37
+
38
+ file = File.open("#{config.path}/config_tmp.status", "w")
39
+ file.write config.inspect
40
+ file.close
41
+
42
+ else
43
+ respond 'Only master admins on master channel can use this command.'
44
+ end
45
+ else
46
+ respond 'Only master admins on master channel can use this command.'
47
+ end
48
+ end
49
+ end
@@ -2,18 +2,20 @@ class SlackSmartBot
2
2
  # helpmaster: ----------------------------------------------
3
3
  # helpmaster: `create bot on CHANNEL_NAME`
4
4
  # helpmaster: `create cloud bot on CHANNEL_NAME`
5
- # helpmaster: creates a new bot on the channel specified
6
- # helpmaster: it will work only if you are on Master channel
7
- # helpmaster: the admins will be the master admins, the creator of the bot and the creator of the channel
8
- # helpmaster: follow the instructions in case creating cloud bots
5
+ # helpmaster: `create silent bot on CHANNEL_NAME`
6
+ # helpmaster: Creates a new bot on the channel specified
7
+ # helpmaster: It will work only if you are on Master channel
8
+ # helpmaster: The admins will be the master admins, the creator of the bot and the creator of the channel
9
+ # helpmaster: Follow the instructions in case creating cloud bots
10
+ # helpmaster: In case 'silent' won't display the Bot initialization message on the CHANNEL_NAME
11
+ # helpmaster: <https://github.com/MarioRuiz/slack-smart-bot#bot-management|more info>
9
12
  # helpmaster:
10
- def create_bot(dest, user, cloud, channel)
13
+ def create_bot(dest, user, type, channel)
14
+ cloud = type.include?('cloud')
15
+ silent = type.include?('silent')
11
16
  save_stats(__method__)
12
17
  from = user.name
13
- if config[:allow_access].key?(__method__) and !config[:allow_access][__method__].include?(user.name) and !config[:allow_access][__method__].include?(user.id) and
14
- (!user.key?(:enterprise_user) or ( user.key?(:enterprise_user) and !config[:allow_access][__method__].include?(user[:enterprise_user].id)))
15
- respond "You don't have access to use this command, please contact an Admin to be able to use it: <@#{config.admins.join(">, <@")}>"
16
- else
18
+ if has_access?(__method__, user)
17
19
  if config.on_master_bot
18
20
  get_channels_name_and_id() unless @channels_name.keys.include?(channel) or @channels_id.keys.include?(channel)
19
21
  channel_id = nil
@@ -24,13 +26,10 @@ class SlackSmartBot
24
26
  channel_id = @channels_id[channel]
25
27
  end
26
28
  #todo: add pagination for case more than 1000 channels on the workspace
27
- channels = client.web_client.conversations_list(
28
- types: "private_channel,public_channel",
29
- limit: "1000",
30
- exclude_archived: "true",
31
- ).channels
29
+ channels = get_channels()
30
+ channel = @channels_name[channel] if @channels_name.key?(channel)
32
31
  channel_found = channels.detect { |c| c.name == channel }
33
- members = client.web_client.conversations_members(channel: @channels_id[channel]).members unless channel_found.nil?
32
+ members = get_channel_members(@channels_id[channel]) unless channel_found.nil?
34
33
 
35
34
  if channel_id.nil?
36
35
  respond "There is no channel with that name: #{channel}, please be sure is written exactly the same", dest
@@ -39,37 +38,45 @@ class SlackSmartBot
39
38
  elsif @bots_created.keys.include?(channel_id)
40
39
  respond "There is already a bot in this channel: #{channel}, kill it before", dest
41
40
  elsif config[:nick_id] != channel_found.creator and !members.include?(config[:nick_id])
42
- respond "You need to add first to the channel the smart bot user: #{config[:nick]}", dest
41
+ respond "You need to add first to the channel the smart bot user: <@#{config[:nick_id]}>", dest
43
42
  else
44
43
  if channel_id != config[:channel]
45
44
  begin
46
45
  rules_file = "slack-smart-bot_rules_#{channel_id}_#{from.gsub(" ", "_")}.rb"
47
- if defined?(RULES_FOLDER)
46
+ if defined?(RULES_FOLDER) # consider removing RULES_FOLDER since we are not using it anywhere else
48
47
  rules_file = RULES_FOLDER + rules_file
49
48
  general_rules_file = RULES_FOLDER + 'general_rules.rb'
49
+ general_commands_file = RULES_FOLDER + 'general_commands.rb'
50
50
  else
51
51
  Dir.mkdir("#{config.path}/rules") unless Dir.exist?("#{config.path}/rules")
52
52
  Dir.mkdir("#{config.path}/rules/#{channel_id}") unless Dir.exist?("#{config.path}/rules/#{channel_id}")
53
53
  rules_file = "/rules/#{channel_id}/" + rules_file
54
54
  general_rules_file = "/rules/general_rules.rb"
55
+ general_commands_file = "/rules/general_commands.rb"
55
56
  end
56
57
  default_rules = (__FILE__).gsub(/slack\/smart-bot\/commands\/on_master\/create_bot\.rb$/, "slack-smart-bot_rules.rb")
57
58
  default_general_rules = (__FILE__).gsub(/slack\/smart-bot\/commands\/on_master\/create_bot\.rb$/, "slack-smart-bot_general_rules.rb")
59
+ default_general_commands = (__FILE__).gsub(/slack\/smart-bot\/commands\/on_master\/create_bot\.rb$/, "slack-smart-bot_general_commands.rb")
58
60
 
59
61
  File.delete(config.path + rules_file) if File.exist?(config.path + rules_file)
60
62
  FileUtils.copy_file(default_rules, config.path + rules_file) unless File.exist?(config.path + rules_file)
61
63
  FileUtils.copy_file(default_general_rules, config.path + general_rules_file) unless File.exist?(config.path + general_rules_file)
64
+ FileUtils.copy_file(default_general_commands, config.path + general_commands_file) unless File.exist?(config.path + general_commands_file)
62
65
  admin_users = Array.new()
63
- creator_info = client.web_client.users_info(user: channel_found.creator)
64
- admin_users = [from, creator_info.user.name] + config.masters
66
+ creator_info = @users.select{|u| u.id == channel_found.creator or (u.key?(:enterprise_user) and u.enterprise_user.id == channel_found.creator)}[-1]
67
+ if creator_info.nil? or creator_info.empty? or creator_info.user.nil?
68
+ admin_users = [from] + config.masters
69
+ else
70
+ admin_users = [from, creator_info.user.name] + config.masters
71
+ end
65
72
  admin_users.uniq!
66
- @logger.info "ruby #{config.file_path} \"#{channel}\" \"#{admin_users.join(",")}\" \"#{rules_file}\" on"
73
+ @logger.info "BOT_SILENT=#{silent} ruby #{config.file_path} \"#{channel}\" \"#{admin_users.join(",")}\" \"#{rules_file}\" on"
67
74
 
68
75
  if cloud
69
76
  respond "Copy the bot folder to your cloud location and run `ruby #{config.file} \"#{channel}\" \"#{admin_users.join(",")}\" \"#{rules_file}\" on&`", dest
70
77
  else
71
78
  t = Thread.new do
72
- `BOT_SILENT=false ruby #{config.file_path} \"#{channel}\" \"#{admin_users.join(",")}\" \"#{rules_file}\" on`
79
+ `BOT_SILENT=#{silent} ruby #{config.file_path} \"#{channel}\" \"#{admin_users.join(",")}\" \"#{rules_file}\" on`
73
80
  end
74
81
  end
75
82
  @bots_created[channel_id] = {
@@ -84,6 +91,8 @@ class SlackSmartBot
84
91
  cloud: cloud,
85
92
  thread: t,
86
93
  }
94
+ @bots_created[channel_id].silent = true if silent
95
+
87
96
  respond "The bot has been created on channel: #{channel}. Rules file: #{File.basename rules_file}. Admins: #{admin_users.join(", ")}", dest
88
97
  update_bots_file()
89
98
  rescue Exception => stack