slack-smart-bot 1.8.1 → 1.10.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (92) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +127 -21
  3. data/lib/slack/smart-bot/comm/ask.rb +55 -42
  4. data/lib/slack/smart-bot/comm/dont_understand.rb +2 -2
  5. data/lib/slack/smart-bot/comm/event_hello.rb +34 -0
  6. data/lib/slack/smart-bot/comm/get_channel_members.rb +13 -0
  7. data/lib/slack/smart-bot/comm/get_channels.rb +35 -0
  8. data/lib/slack/smart-bot/comm/get_user_info.rb +20 -0
  9. data/lib/slack/smart-bot/comm/get_users.rb +24 -0
  10. data/lib/slack/smart-bot/comm/react.rb +38 -8
  11. data/lib/slack/smart-bot/comm/respond.rb +219 -48
  12. data/lib/slack/smart-bot/comm/respond_direct.rb +2 -3
  13. data/lib/slack/smart-bot/comm/respond_thread.rb +5 -0
  14. data/lib/slack/smart-bot/comm/send_file.rb +38 -34
  15. data/lib/slack/smart-bot/comm/send_msg_channel.rb +27 -22
  16. data/lib/slack/smart-bot/comm/send_msg_user.rb +58 -33
  17. data/lib/slack/smart-bot/comm/unreact.rb +24 -7
  18. data/lib/slack/smart-bot/comm.rb +7 -1
  19. data/lib/slack/smart-bot/commands/general/add_announcement.rb +32 -0
  20. data/lib/slack/smart-bot/commands/general/bot_help.rb +68 -28
  21. data/lib/slack/smart-bot/commands/general/bot_stats.rb +314 -0
  22. data/lib/slack/smart-bot/commands/general/bot_status.rb +3 -5
  23. data/lib/slack/smart-bot/commands/general/bye_bot.rb +0 -7
  24. data/lib/slack/smart-bot/commands/general/delete_announcement.rb +34 -0
  25. data/lib/slack/smart-bot/commands/general/delete_share.rb +34 -0
  26. data/lib/slack/smart-bot/commands/general/hi_bot.rb +16 -11
  27. data/lib/slack/smart-bot/commands/general/leaderboard.rb +200 -0
  28. data/lib/slack/smart-bot/commands/general/see_announcements.rb +113 -0
  29. data/lib/slack/smart-bot/commands/general/see_favorite_commands.rb +54 -0
  30. data/lib/slack/smart-bot/commands/general/see_shares.rb +41 -0
  31. data/lib/slack/smart-bot/commands/general/see_statuses.rb +78 -0
  32. data/lib/slack/smart-bot/commands/general/share_messages.rb +58 -0
  33. data/lib/slack/smart-bot/commands/general/stop_using_rules.rb +11 -6
  34. data/lib/slack/smart-bot/commands/general/suggest_command.rb +30 -0
  35. data/lib/slack/smart-bot/commands/general/use_rules.rb +13 -16
  36. data/lib/slack/smart-bot/commands/general/whats_new.rb +19 -0
  37. data/lib/slack/smart-bot/commands/general_bot_commands.rb +243 -0
  38. data/lib/slack/smart-bot/commands/on_bot/add_shortcut.rb +67 -38
  39. data/lib/slack/smart-bot/commands/on_bot/admin/add_routine.rb +49 -14
  40. data/lib/slack/smart-bot/commands/on_bot/admin/extend_rules.rb +5 -7
  41. data/lib/slack/smart-bot/commands/on_bot/admin/pause_bot.rb +4 -1
  42. data/lib/slack/smart-bot/commands/on_bot/admin/pause_routine.rb +1 -0
  43. data/lib/slack/smart-bot/commands/on_bot/admin/remove_routine.rb +2 -3
  44. data/lib/slack/smart-bot/commands/on_bot/admin/run_routine.rb +6 -1
  45. data/lib/slack/smart-bot/commands/on_bot/admin/see_result_routine.rb +32 -0
  46. data/lib/slack/smart-bot/commands/on_bot/admin/see_routines.rb +12 -4
  47. data/lib/slack/smart-bot/commands/on_bot/admin/start_bot.rb +4 -1
  48. data/lib/slack/smart-bot/commands/on_bot/admin/start_routine.rb +1 -0
  49. data/lib/slack/smart-bot/commands/on_bot/admin/stop_using_rules_on.rb +2 -0
  50. data/lib/slack/smart-bot/commands/on_bot/admin_master/react_to.rb +32 -0
  51. data/lib/slack/smart-bot/commands/on_bot/admin_master/send_message.rb +24 -0
  52. data/lib/slack/smart-bot/commands/on_bot/delete_repl.rb +3 -5
  53. data/lib/slack/smart-bot/commands/on_bot/delete_shortcut.rb +54 -25
  54. data/lib/slack/smart-bot/commands/on_bot/get_repl.rb +7 -9
  55. data/lib/slack/smart-bot/commands/on_bot/repl.rb +55 -25
  56. data/lib/slack/smart-bot/commands/on_bot/ruby_code.rb +36 -13
  57. data/lib/slack/smart-bot/commands/on_bot/run_repl.rb +5 -7
  58. data/lib/slack/smart-bot/commands/on_bot/see_repls.rb +4 -6
  59. data/lib/slack/smart-bot/commands/on_bot/see_shortcuts.rb +29 -13
  60. data/lib/slack/smart-bot/commands/on_extended/bot_rules.rb +55 -9
  61. data/lib/slack/smart-bot/commands/on_master/admin/kill_bot_on_channel.rb +4 -1
  62. data/lib/slack/smart-bot/commands/on_master/admin_master/exit_bot.rb +5 -3
  63. data/lib/slack/smart-bot/commands/on_master/admin_master/notify_message.rb +2 -1
  64. data/lib/slack/smart-bot/commands/on_master/admin_master/publish_announcements.rb +32 -0
  65. data/lib/slack/smart-bot/commands/on_master/admin_master/set_general_message.rb +38 -0
  66. data/lib/slack/smart-bot/commands/on_master/admin_master/set_maintenance.rb +49 -0
  67. data/lib/slack/smart-bot/commands/on_master/create_bot.rb +30 -21
  68. data/lib/slack/smart-bot/commands.rb +19 -1
  69. data/lib/slack/smart-bot/listen.rb +7 -8
  70. data/lib/slack/smart-bot/process.rb +373 -192
  71. data/lib/slack/smart-bot/process_first.rb +202 -104
  72. data/lib/slack/smart-bot/treat_message.rb +325 -186
  73. data/lib/slack/smart-bot/utils/answer.rb +18 -0
  74. data/lib/slack/smart-bot/utils/answer_delete.rb +15 -0
  75. data/lib/slack/smart-bot/utils/build_help.rb +57 -5
  76. data/lib/slack/smart-bot/utils/create_routine_thread.rb +83 -30
  77. data/lib/slack/smart-bot/utils/get_bots_created.rb +4 -1
  78. data/lib/slack/smart-bot/utils/get_channels_name_and_id.rb +1 -7
  79. data/lib/slack/smart-bot/utils/get_help.rb +87 -35
  80. data/lib/slack/smart-bot/utils/get_shares.rb +12 -0
  81. data/lib/slack/smart-bot/utils/has_access.rb +12 -0
  82. data/lib/slack/smart-bot/utils/save_stats.rb +23 -8
  83. data/lib/slack/smart-bot/utils/save_status.rb +52 -0
  84. data/lib/slack/smart-bot/utils/update_shortcuts_file.rb +6 -0
  85. data/lib/slack/smart-bot/utils.rb +5 -0
  86. data/lib/slack-smart-bot.rb +88 -47
  87. data/lib/slack-smart-bot_general_commands.rb +46 -0
  88. data/lib/slack-smart-bot_general_rules.rb +5 -2
  89. data/lib/slack-smart-bot_rules.rb +49 -23
  90. data/whats_new.txt +36 -0
  91. metadata +44 -13
  92. data/lib/slack/smart-bot/commands/on_bot/admin_master/bot_stats.rb +0 -195
@@ -0,0 +1,18 @@
1
+ class SlackSmartBot
2
+ def answer(from = Thread.current[:user].name, dest = Thread.current[:dest])
3
+ if @answer.key?(from)
4
+ if Thread.current[:on_thread]
5
+ dest = Thread.current[:thread_ts]
6
+ end
7
+ if @answer[from].key?(dest)
8
+ return @answer[from][dest]
9
+ else
10
+ return ''
11
+ end
12
+ else
13
+ return ''
14
+ end
15
+ end
16
+
17
+ end
18
+
@@ -0,0 +1,15 @@
1
+ class SlackSmartBot
2
+ def answer_delete(from = Thread.current[:user].name, dest = Thread.current[:dest])
3
+ if @answer.key?(from)
4
+ if Thread.current[:on_thread]
5
+ dest = Thread.current[:thread_ts]
6
+ end
7
+ if @answer[from].key?(dest)
8
+ @answer[from].delete(dest)
9
+ end
10
+ @questions.delete(from) # to be backwards compatible #todo: remove when 2.0
11
+ end
12
+ end
13
+
14
+ end
15
+
@@ -1,12 +1,64 @@
1
1
  class SlackSmartBot
2
2
 
3
- def build_help(path)
4
- help_message = {}
5
- Dir["#{path}/*"].each do |t|
3
+ def build_help(path, expanded)
4
+ help_message = {normal: {}, admin: {}, master: {}}
5
+ if Dir.exist?(path)
6
+ files = Dir["#{path}/*"]
7
+ elsif File.exist?(path)
8
+ files = [path]
9
+ else
10
+ return help_message
11
+ end
12
+ files.each do |t|
13
+
6
14
  if Dir.exist?(t)
7
- help_message[t.scan(/\/(\w+)$/).join.to_sym] = build_help(t)
15
+ res = build_help(t, expanded)
16
+ help_message[:master][t.scan(/\/(\w+)$/).join.to_sym] = res[:master]
17
+ help_message[:admin][t.scan(/\/(\w+)$/).join.to_sym] = res[:admin]
18
+ help_message[:normal][t.scan(/\/(\w+)$/).join.to_sym] = res[:normal]
8
19
  else
9
- help_message[t.scan(/\/(\w+)\.rb$/).join.to_sym] = IO.readlines(t).join.scan(/#\s*help\s*\w*:(.*)/).join("\n")
20
+ lines = IO.readlines(t)
21
+ data = {master:{}, admin:{}, normal:{}}
22
+ data.master = lines.join #normal user help
23
+ data.admin = lines.reject {|l| l.match?(/^\s*#\s*help\s*master\s*:.+$/i)}.join #not master help
24
+ data.normal = lines.reject {|l| l.match?(/^\s*#\s*help\s*(admin|master)\s*:.+$/i)}.join #not admin or master help
25
+ if expanded
26
+ help_message[:master][t.scan(/\/(\w+)\.rb$/).join.to_sym] = data.master.scan(/#\s*help\s*\w*:(.*)/i).join("\n")
27
+ help_message[:admin][t.scan(/\/(\w+)\.rb$/).join.to_sym] = data.admin.scan(/#\s*help\s*\w*:(.*)/i).join("\n")
28
+ help_message[:normal][t.scan(/\/(\w+)\.rb$/).join.to_sym] = data.normal.scan(/#\s*help\s*\w*:(.*)/i).join("\n")
29
+ else
30
+ data.keys.each do |key|
31
+ res = data[key].scan(/#\s*help\s*\w*:(.*)/i).join("\n")
32
+ resf = ""
33
+ command_done = false
34
+ explanation_done = false
35
+ example_done = false
36
+
37
+ res.split("\n").each do |line|
38
+ if line.match?(/^\s*======+$/)
39
+ command_done = true
40
+ explanation_done = true
41
+ example_done = true
42
+ elsif line.match?(/^\s*\-\-\-\-+\s*$/i)
43
+ resf += "\n#{line}"
44
+ command_done = false
45
+ explanation_done = false
46
+ example_done = false
47
+ elsif !command_done and line.match?(/^\s*`.+`\s*/i)
48
+ resf += "\n#{line}"
49
+ command_done = true
50
+ elsif !explanation_done and line.match?(/^\s+[^`].+\s*/i)
51
+ resf += "\n#{line}"
52
+ explanation_done = true
53
+ elsif !example_done and line.match?(/^\s*>?\s*_.+_\s*$/i)
54
+ resf += "\n Example: #{line.gsub(/^\s*>/,'')}"
55
+ example_done = true
56
+ end
57
+ end
58
+ resf += "\n\n"
59
+ help_message[key][t.scan(/\/(\w+)\.rb$/).join.to_sym] = resf
60
+ end
61
+ end
10
62
  end
11
63
  end
12
64
  return help_message
@@ -1,11 +1,15 @@
1
1
  class SlackSmartBot
2
2
 
3
- def create_routine_thread(name)
3
+ def create_routine_thread(name, hroutine)
4
4
  t = Thread.new do
5
5
  while @routines.key?(@channel_id) and @routines[@channel_id].key?(name)
6
6
  @routines[@channel_id][name][:thread] = Thread.current
7
7
  started = Time.now
8
8
  if @status == :on and @routines[@channel_id][name][:status] == :on
9
+ if !@routines[@channel_id][name].key?(:creator_id) or @routines[@channel_id][name][:creator_id].to_s == ''
10
+ user_info = @users.select{|u| u.name == @routines[@channel_id][name][:creator]}[-1]
11
+ @routines[@channel_id][name][:creator_id] = user_info.id unless user_info.nil? or user_info.empty?
12
+ end
9
13
  @logger.info "Routine: #{@routines[@channel_id][name].inspect}"
10
14
  if @routines[@channel_id][name][:file_path].match?(/\.rb$/i)
11
15
  ruby = "ruby "
@@ -16,45 +20,84 @@ class SlackSmartBot
16
20
  if @routines[@channel_id][name][:at] == "" or
17
21
  (@routines[@channel_id][name][:at] != "" and @routines[@channel_id][name][:running] and
18
22
  @routines[@channel_id][name][:next_run] != "" and Time.now.to_s >= @routines[@channel_id][name][:next_run])
19
- if @routines[@channel_id][name][:file_path] != ""
20
- process_to_run = "#{ruby}#{Dir.pwd}#{@routines[@channel_id][name][:file_path][1..-1]}"
21
- process_to_run = ("cd #{project_folder} &&" + process_to_run) if defined?(project_folder)
22
-
23
- stdout, stderr, status = Open3.capture3(process_to_run)
24
- if !@routines[@channel_id][name][:silent] or (@routines[@channel_id][name][:silent] and
25
- (!stderr.match?(/\A\s*\z/) or !stdout.match?(/\A\s*\z/)))
26
- respond "routine *`#{name}`*: #{@routines[@channel_id][name][:file_path]}", @routines[@channel_id][name][:dest]
27
- end
28
- if stderr == ""
29
- unless stdout.match?(/\A\s*\z/)
30
- respond stdout, @routines[@channel_id][name][:dest]
23
+
24
+ if !@routines[@channel_id][name].key?(:dayweek) or
25
+ (@routines[@channel_id][name].key?(:dayweek) and @routines[@channel_id][name][:dayweek].to_s!='weekday' and @routines[@channel_id][name][:dayweek].to_s!='weekend') or
26
+ (@routines[@channel_id][name].key?(:dayweek) and @routines[@channel_id][name][:dayweek].to_s=='weekday' and Date.today.wday>=1 and Date.today.wday<=5) or
27
+ (@routines[@channel_id][name].key?(:dayweek) and @routines[@channel_id][name][:dayweek].to_s=='weekend' and (Date.today.wday==6 or Date.today.wday==0))
28
+ File.delete "#{config.path}/routines/#{@channel_id}/#{name}_output.txt" if File.exists?("#{config.path}/routines/#{@channel_id}/#{name}_output.txt")
29
+ if @routines[@channel_id][name][:file_path] != ""
30
+ process_to_run = "#{ruby}#{Dir.pwd}#{@routines[@channel_id][name][:file_path][1..-1]}"
31
+ process_to_run = ("cd #{project_folder} &&" + process_to_run) if defined?(project_folder)
32
+ data = {
33
+ dest: @routines[@channel_id][name][:dest],
34
+ typem: 'routine_file',
35
+ user: {id: @routines[@channel_id][name][:creator_id], name: @routines[@channel_id][name][:creator]},
36
+ files: false,
37
+ command: @routines[@channel_id][name][:file_path],
38
+ routine: true,
39
+ routine_name: name,
40
+ routine_type: hroutine[:routine_type]
41
+ }
42
+ save_stats(name, data: data)
43
+ stdout, stderr, status = Open3.capture3(process_to_run)
44
+ if !@routines[@channel_id][name][:silent]
45
+ unless config.on_maintenance
46
+ if @routines[@channel_id][name][:dest]!=@channel_id
47
+ respond "routine from <##{@channel_id}> *`#{name}`*: #{@routines[@channel_id][name][:file_path]}", @routines[@channel_id][name][:dest]
48
+ else
49
+ respond "routine *`#{name}`*: #{@routines[@channel_id][name][:file_path]}", @routines[@channel_id][name][:dest]
50
+ end
51
+ end
31
52
  end
32
- else
33
- respond "#{stdout} #{stderr}", @routines[@channel_id][name][:dest]
53
+ if hroutine[:routine_type].to_s!='bgroutine'
54
+ if stderr == ""
55
+ unless stdout.match?(/\A\s*\z/)
56
+ respond stdout, @routines[@channel_id][name][:dest]
57
+ end
58
+ else
59
+ respond "#{stdout} #{stderr}", @routines[@channel_id][name][:dest]
60
+ end
61
+ else
62
+ File.write("#{config.path}/routines/#{@channel_id}/#{name}_output.txt", stdout.to_s+stderr.to_s, mode: "a+")
63
+ end
64
+ else #command
65
+ if !@routines[@channel_id][name][:silent] and !config.on_maintenance
66
+ if @routines[@channel_id][name][:dest]!=@channel_id
67
+ respond "routine from <##{@channel_id}> *`#{name}`*: #{@routines[@channel_id][name][:command]}", @routines[@channel_id][name][:dest]
68
+ else
69
+ respond "routine *`#{name}`*: #{@routines[@channel_id][name][:command]}", @routines[@channel_id][name][:dest]
70
+ end
71
+ end
72
+ started = Time.now
73
+ data = { channel: @channel_id,
74
+ dest: @routines[@channel_id][name][:dest],
75
+ user: @routines[@channel_id][name][:creator_id],
76
+ text: @routines[@channel_id][name][:command],
77
+ files: nil,
78
+ routine: true,
79
+ routine_name: name,
80
+ routine_type: hroutine[:routine_type] }
81
+ treat_message(data)
34
82
  end
35
- else #command
36
- if !@routines[@channel_id][name][:silent]
37
- respond "routine *`#{name}`*: #{@routines[@channel_id][name][:command]}", @routines[@channel_id][name][:dest]
83
+ # in case the routine was deleted while running the process
84
+ if !@routines.key?(@channel_id) or !@routines[@channel_id].key?(name)
85
+ Thread.exit
38
86
  end
39
- started = Time.now
40
- data = { channel: @routines[@channel_id][name][:dest],
41
- user: @routines[@channel_id][name][:creator_id],
42
- text: @routines[@channel_id][name][:command],
43
- files: nil }
44
- treat_message(data)
45
- end
46
- # in case the routine was deleted while running the process
47
- if !@routines.key?(@channel_id) or !@routines[@channel_id].key?(name)
48
- Thread.exit
87
+ @routines[@channel_id][name][:last_run] = started.to_s
88
+ elsif (@routines[@channel_id][name].key?(:dayweek) and @routines[@channel_id][name][:dayweek].to_s=='weekday' and (Date.today.wday==6 or Date.today.wday==0)) or
89
+ (@routines[@channel_id][name].key?(:dayweek) and @routines[@channel_id][name][:dayweek].to_s=='weekend' and Date.today.wday>=1 and Date.today.wday<=5)
90
+ @routines[@channel_id][name][:last_run] = started.to_s
49
91
  end
50
- @routines[@channel_id][name][:last_run] = started.to_s
51
92
  end
52
93
  if @routines[@channel_id][name][:last_run] == "" and @routines[@channel_id][name][:next_run] != "" #for the first create_routine of one routine with at
53
94
  elapsed = 0
54
95
  require "time"
55
96
  every_in_seconds = Time.parse(@routines[@channel_id][name][:next_run]) - Time.now
56
97
  elsif @routines[@channel_id][name][:at] != "" #coming from start after pause for 'at'
57
- if @routines[@channel_id][name].key?(:dayweek) and @routines[@channel_id][name][:dayweek].to_s!=''
98
+ if @routines[@channel_id][name].key?(:dayweek) and @routines[@channel_id][name][:dayweek].to_s!=''and
99
+ @routines[@channel_id][name][:dayweek].to_s!='weekend' and @routines[@channel_id][name][:dayweek].to_s!='weekday'
100
+
58
101
  day = @routines[@channel_id][name][:dayweek]
59
102
  days = ['sunday','monday','tuesday','wednesday','thursday','friday','saturday']
60
103
  incr = days.index(day) - Time.now.wday
@@ -65,6 +108,16 @@ class SlackSmartBot
65
108
  end
66
109
  days = incr/(24*60*60)
67
110
  weekly = true
111
+ elsif @routines[@channel_id][name].key?(:dayweek) and @routines[@channel_id][name][:dayweek].to_s!='' and
112
+ @routines[@channel_id][name][:dayweek].to_s=='weekend'
113
+
114
+ weekly = false
115
+ days = 0
116
+ elsif @routines[@channel_id][name].key?(:dayweek) and @routines[@channel_id][name][:dayweek].to_s!='' and
117
+ @routines[@channel_id][name][:dayweek].to_s=='weekday'
118
+
119
+ weekly = false
120
+ days = 0
68
121
  else
69
122
  days = 0
70
123
  weekly = false
@@ -1,7 +1,9 @@
1
1
  class SlackSmartBot
2
2
  def get_bots_created
3
3
  if File.exist?(config.file_path.gsub(".rb", "_bots.rb"))
4
+
4
5
  if !defined?(@datetime_bots_created) or @datetime_bots_created != File.mtime(config.file_path.gsub(".rb", "_bots.rb"))
6
+
5
7
  file_conf = IO.readlines(config.file_path.gsub(".rb", "_bots.rb")).join
6
8
  if file_conf.to_s() == ""
7
9
  @bots_created = {}
@@ -16,8 +18,9 @@ class SlackSmartBot
16
18
  @extended_from[ch] = [] unless @extended_from.key?(ch)
17
19
  @extended_from[ch] << k
18
20
  end
21
+ v[:rules_file] ||= ''
19
22
  v[:rules_file].gsub!(/^\./, '')
20
- end
23
+ end
21
24
  end
22
25
  end
23
26
  end
@@ -1,13 +1,7 @@
1
1
  class SlackSmartBot
2
2
 
3
3
  def get_channels_name_and_id
4
- #todo: add pagination for case more than 1000 channels on the workspace
5
- channels = client.web_client.conversations_list(
6
- types: "private_channel,public_channel",
7
- limit: "1000",
8
- exclude_archived: "true",
9
- ).channels
10
-
4
+ channels = get_channels()
11
5
  @channels_id = Hash.new()
12
6
  @channels_name = Hash.new()
13
7
  channels.each do |ch|
@@ -1,19 +1,19 @@
1
1
  class SlackSmartBot
2
- def get_help(rules_file, dest, from, only_rules = false)
2
+ def get_help(rules_file, dest, from, only_rules, expanded, descriptions: true, only_normal_user: false)
3
3
  order = {
4
- general: [:hi_bot, :bye_bot, :bot_help, :bot_status, :use_rules, :stop_using_rules],
4
+ general: [:whats_new, :hi_bot, :bye_bot, :bot_help, :suggest_command, :bot_status, :use_rules, :stop_using_rules, :bot_stats, :leaderboard],
5
5
  on_bot: [:ruby_code, :repl, :get_repl, :run_repl, :delete_repl, :see_repls, :add_shortcut, :delete_shortcut, :see_shortcuts],
6
6
  on_bot_admin: [:extend_rules, :stop_using_rules_on, :start_bot, :pause_bot, :add_routine,
7
- :see_routines, :start_routine, :pause_routine, :remove_routine, :run_routine]
7
+ :see_routines, :start_routine, :pause_routine, :remove_routine, :see_result_routine, :run_routine]
8
8
  }
9
- # user_type: :admin, :user, :admin_master
10
9
  if config.masters.include?(from)
11
- user_type = :admin_master
10
+ user_type = :master # master admin
12
11
  elsif config.admins.include?(from)
13
12
  user_type = :admin
14
13
  else
15
- user_type = :user
14
+ user_type = :normal #normal user
16
15
  end
16
+
17
17
  # channel_type: :bot, :master_bot, :direct, :extended, :external
18
18
  if dest[0] == "D"
19
19
  channel_type = :direct
@@ -25,27 +25,58 @@ class SlackSmartBot
25
25
  channel_type = :bot
26
26
  end
27
27
 
28
- @help_messages ||= build_help("#{__dir__}/../commands")
28
+ if Thread.current[:typem] == :on_pg or Thread.current[:typem] == :on_pub
29
+ channel_type = :external
30
+ end
31
+
32
+ if only_normal_user
33
+ user_type = :normal
34
+ channel_type = :bot
35
+ end
36
+
37
+ @help_messages_expanded ||= build_help("#{__dir__}/../commands", true)
38
+ @help_messages_not_expanded ||= build_help("#{__dir__}/../commands", false)
29
39
  if only_rules
30
40
  help = {}
41
+ elsif expanded
42
+ help = @help_messages_expanded.deep_copy[user_type]
31
43
  else
32
- help = @help_messages.deep_copy
44
+ help = @help_messages_not_expanded.deep_copy[user_type]
33
45
  end
34
46
  if rules_file != ""
35
- help[:rules_file] = ''
36
- help[:rules_file] += IO.readlines(config.path+rules_file).join.scan(/#\s*help\s*\w*:(.*)/i).join("\n") + "\n"
37
- if File.exist?(config.path+'/rules/general_rules.rb')
38
- help[:rules_file] += IO.readlines(config.path+'/rules/general_rules.rb').join.scan(/#\s*help\s*\w*:(.*)/i).join("\n")
47
+ help[:rules_file] = build_help(config.path+rules_file, expanded)[user_type].values.join("\n") + "\n"
48
+
49
+ # to get all the help from other rules files added to the main rules file by using require or load. For example general_rules.rb
50
+ res = IO.readlines(config.path+rules_file).join.scan(/$\s*(load|require)\s("|')(.+)("|')/)
51
+ rules_help = []
52
+ txt = ''
53
+ if res.size>0
54
+ res.each do |r|
55
+ begin
56
+ eval("txt = \"#{r[2]}\"")
57
+ rules_help << txt if File.exist?(txt)
58
+ rescue
59
+ end
60
+ end
39
61
  end
62
+ rules_help.each do |rh|
63
+ rhelp = build_help(rh, expanded)
64
+ help[:rules_file] += rhelp[user_type].values.join("\n") + "\n"
65
+ end
66
+ end
67
+
68
+ help[:general_commands_file] = build_help("#{__dir__}/../commands/general_bot_commands.rb", expanded)[user_type].values.join("\n") + "\n" unless only_rules
69
+ if File.exists?(config.path + '/rules/general_commands.rb') and !only_rules
70
+ help[:general_commands_file] += build_help(config.path+'/rules/general_commands.rb', expanded)[user_type].values.join("\n") + "\n"
40
71
  end
41
- help = remove_hash_keys(help, :admin_master) unless user_type == :admin_master
42
- help = remove_hash_keys(help, :admin) unless user_type == :admin or user_type == :admin_master
72
+ help = remove_hash_keys(help, :admin_master) unless user_type == :master
73
+ help = remove_hash_keys(help, :admin) unless user_type == :admin or user_type == :master
43
74
  help = remove_hash_keys(help, :on_master) unless channel_type == :master_bot
44
75
  help = remove_hash_keys(help, :on_extended) unless channel_type == :extended
45
76
  help = remove_hash_keys(help, :on_dm) unless channel_type == :direct
46
77
  txt = ""
47
78
 
48
- if channel_type == :bot or channel_type == :master_bot
79
+ if (channel_type == :bot or channel_type == :master_bot) and expanded and descriptions
49
80
  txt += "===================================
50
81
  For the Smart Bot start listening to you say *hi bot*
51
82
  To run a command on demand even when the Smart Bot is not listening to you:
@@ -56,11 +87,11 @@ class SlackSmartBot
56
87
  *^THE_COMMAND*
57
88
  *!!THE_COMMAND*\n"
58
89
  end
59
- if channel_type == :direct
90
+ if channel_type == :direct and expanded and descriptions
60
91
  txt += "===================================
61
92
  When on a private conversation with the Smart Bot, I'm always listening to you.\n"
62
93
  end
63
- unless channel_type == :master_bot or channel_type == :extended
94
+ unless channel_type == :master_bot or channel_type == :extended or !expanded or !descriptions
64
95
  txt += "===================================
65
96
  *Commands from Channels without a bot:*
66
97
  ----------------------------------------------
@@ -69,16 +100,22 @@ class SlackSmartBot
69
100
  It will run the supplied command using the rules on the channel supplied.
70
101
  You need to join the specified channel to be able to use those rules.
71
102
  Also you can use this command to call another bot from a channel with a running bot.
72
-
103
+ \n"
104
+ txt +="
73
105
  The commands you will be able to use from a channel without a bot:
74
106
  *bot rules*, *ruby CODE*, *add shortcut NAME: COMMAND*, *delete shortcut NAME*, *see shortcuts*, *shortcut NAME*
75
107
  *And all the specific rules of the Channel*\n"
76
108
  end
77
109
 
78
- if help.key?(:general)
79
- unless channel_type == :direct
80
- txt += "===================================
81
- *General commands even when the Smart Bot is not listening to you:*\n"
110
+ if help.key?(:general) and channel_type != :external and channel_type != :extended
111
+ if descriptions
112
+ if channel_type == :direct
113
+ txt += "===================================
114
+ *General commands:*\n"
115
+ else
116
+ txt += "===================================
117
+ *General commands even when the Smart Bot is not listening to you:*\n"
118
+ end
82
119
  end
83
120
  order.general.each do |o|
84
121
  txt += help.general[o]
@@ -88,19 +125,29 @@ class SlackSmartBot
88
125
  end
89
126
  end
90
127
 
91
- if help.key?(:on_bot)
92
- unless channel_type == :direct
93
- txt += "===================================
94
- *General commands only when the Smart Bot is listening to you or on demand:*\n"
128
+ if help.key?(:general_commands_file)
129
+ txt += "===================================
130
+ *General commands on any channel where the Smart Bot is a member:*\n" if descriptions
131
+ txt += help.general_commands_file
132
+ end
133
+
134
+ if help.key?(:on_bot) and channel_type != :external and channel_type != :extended
135
+ if descriptions
136
+ if channel_type == :direct
137
+ txt += "===================================
138
+ *General commands on bot, DM or on external call on demand:*\n"
139
+ else
140
+ txt += "===================================
141
+ *General commands only when the Smart Bot is listening to you or on demand:*\n"
142
+ end
95
143
  end
96
144
  order.on_bot.each do |o|
97
145
  txt += help.on_bot[o]
98
146
  end
99
147
  end
100
- if help.key?(:on_bot) and help.on_bot.key?(:admin)
148
+ if help.key?(:on_bot) and help.on_bot.key?(:admin) and channel_type != :external and channel_type != :extended
101
149
  txt += "===================================
102
- *Admin commands:*\n"
103
- txt += "\n\n"
150
+ *Admin commands:*\n\n" if descriptions
104
151
  order.on_bot_admin.each do |o|
105
152
  txt += help.on_bot.admin[o]
106
153
  end
@@ -111,31 +158,36 @@ class SlackSmartBot
111
158
  end
112
159
  end
113
160
 
114
- if help.key?(:on_bot) and help.on_bot.key?(:admin_master) and help.on_bot.admin_master.size > 0
161
+ if help.key?(:on_bot) and help.on_bot.key?(:admin_master) and help.on_bot.admin_master.size > 0 and channel_type != :external and channel_type != :extended
115
162
  txt += "===================================
116
- *Master Admin commands:*\n"
163
+ *Master Admin commands:*\n" if descriptions
117
164
  help.on_bot.admin_master.each do |k, v|
118
165
  txt += v if v.is_a?(String)
119
166
  end
120
167
  end
121
168
 
122
- if help.key?(:on_master) and help.on_master.key?(:admin_master) and help.on_master.admin_master.size > 0
169
+ if help.key?(:on_master) and help.on_master.key?(:admin_master) and help.on_master.admin_master.size > 0 and channel_type != :external and channel_type != :extended
123
170
  txt += "===================================
124
- *Master Admin commands:*\n"
171
+ *Master Admin commands:*\n" unless txt.include?('*Master Admin commands*') or !descriptions
125
172
  help.on_master.admin_master.each do |k, v|
126
173
  txt += v if v.is_a?(String)
127
174
  end
128
175
  end
129
176
 
130
- if help.key?(:rules_file)
177
+ if help.key?(:rules_file) and channel_type != :external
131
178
  @logger.info channel_type if config.testing
132
179
  if channel_type == :extended or channel_type == :direct
133
180
  @logger.info help.rules_file if config.testing
134
181
  help.rules_file = help.rules_file.gsub(/^\s*\*These are specific commands.+NAME_OF_BOT THE_COMMAND`\s*$/im, "")
135
182
  end
183
+
184
+ if !help.rules_file.to_s.include?('These are specific commands') and help.rules_file!=''
185
+ txt += "===================================
186
+ *Specific commands on this Channel, call them !THE_COMMAND or !!THE_COMMAND:*\n" if descriptions
187
+ end
188
+
136
189
  txt += help.rules_file
137
190
  end
138
-
139
191
  return txt
140
192
  end
141
193
  end