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
@@ -7,8 +7,10 @@ class SlackSmartBot
7
7
  # help: `add sc for all NAME: COMMAND`
8
8
  # help: `shortcut NAME: COMMAND`
9
9
  # help: `shortcut for all NAME: COMMAND`
10
+ # help: `add global sc for all NAME: COMMAND`
10
11
  # help: It will add a shortcut that will execute the command we supply.
11
12
  # help: In case we supply 'for all' then the shorcut will be available for everybody
13
+ # help: If 'global' or 'generic' supplied and in Master channel then the shortcut will be available in all Bot channels.
12
14
  # help: If you want to use a shortcut as a inline shortcut inside a command you can do it by adding a $ fex: _!run tests $cust1_
13
15
  # help: Example:
14
16
  # help: _add shortcut for all Spanish account: code require 'iso/iban'; 10.times {puts ISO::IBAN.random('ES')}_
@@ -16,52 +18,79 @@ class SlackSmartBot
16
18
  # help: _sc spanish account_
17
19
  # help: _shortcut Spanish Account_
18
20
  # help: _Spanish Account_
21
+ # help: <https://github.com/MarioRuiz/slack-smart-bot#shortcuts|more info>
19
22
  # help:
20
- def add_shortcut(dest, user, typem, for_all, shortcut_name, command, command_to_run)
23
+ def add_shortcut(dest, user, typem, for_all, shortcut_name, command, command_to_run, global)
21
24
  save_stats(__method__)
22
25
  unless typem == :on_extended
23
26
  from = user.name
24
- if config[:allow_access].key?(__method__) and !config[:allow_access][__method__].include?(user.name) and !config[:allow_access][__method__].include?(user.id) and
25
- (!user.key?(:enterprise_user) or ( user.key?(:enterprise_user) and !config[:allow_access][__method__].include?(user[:enterprise_user].id)))
26
- respond "You don't have access to use this command, please contact an Admin to be able to use it: <@#{config.admins.join(">, <@")}>"
27
- else
28
- @shortcuts[from] = Hash.new() unless @shortcuts.keys.include?(from)
29
-
30
- found_other = false
31
- if for_all.to_s != ""
32
- @shortcuts.each { |sck, scv|
33
- if sck != :all and sck != from and scv.key?(shortcut_name)
34
- found_other = true
27
+ if has_access?(__method__, user)
28
+ if global
29
+ if !config.on_master_bot or typem != :on_master
30
+ respond "It is only possible to add global shortcuts from Master channel"
31
+ else
32
+ @shortcuts_global[from] = Hash.new() unless @shortcuts_global.keys.include?(from)
33
+ found_other = false
34
+ if for_all.to_s != ""
35
+ @shortcuts_global.each { |sck, scv|
36
+ if sck != :all and sck != from and scv.key?(shortcut_name)
37
+ found_other = true
38
+ end
39
+ }
35
40
  end
36
- }
37
- end
38
- if !config.admins.include?(from) and @shortcuts[:all].include?(shortcut_name) and !@shortcuts[from].include?(shortcut_name)
39
- respond "Only the creator of the shortcut can modify it", dest
40
- elsif found_other
41
- respond "You cannot create a shortcut for all with the same name than other user is using", dest
42
- elsif !@shortcuts[from].include?(shortcut_name)
43
- #new shortcut
44
- @shortcuts[from][shortcut_name] = command_to_run
45
- @shortcuts[:all][shortcut_name] = command_to_run if for_all.to_s != ""
46
- update_shortcuts_file()
47
- respond "shortcut added", dest
41
+ if @shortcuts_global[:all].include?(shortcut_name) or @shortcuts_global[from].include?(shortcut_name)
42
+ respond "Global shortcut name already in use. Please use another shortcut name."
43
+ elsif found_other
44
+ respond "You cannot create a global shortcut for all with the same name than other user is using."
45
+ elsif !@shortcuts_global[from].include?(shortcut_name)
46
+ #new shortcut
47
+ @shortcuts_global[from][shortcut_name] = command_to_run
48
+ @shortcuts_global[:all][shortcut_name] = command_to_run if for_all.to_s != ""
49
+ update_shortcuts_file()
50
+ respond "global shortcut added"
51
+ else
52
+ respond "Not possible to add the global shortcut" #todo: check if this is ever gonna be the case
53
+ end
54
+ end
48
55
  else
49
- #are you sure? to avoid overwriting existing
50
- unless @questions.keys.include?(from)
51
- ask("The shortcut already exists, are you sure you want to overwrite it?", command, from, dest)
56
+ @shortcuts[from] = Hash.new() unless @shortcuts.keys.include?(from)
57
+
58
+ found_other = false
59
+ if for_all.to_s != ""
60
+ @shortcuts.each { |sck, scv|
61
+ if sck != :all and sck != from and scv.key?(shortcut_name)
62
+ found_other = true
63
+ end
64
+ }
65
+ end
66
+ if !config.admins.include?(from) and @shortcuts[:all].include?(shortcut_name) and !@shortcuts[from].include?(shortcut_name)
67
+ respond "Only the creator of the shortcut can modify it", dest
68
+ elsif found_other
69
+ respond "You cannot create a shortcut for all with the same name than other user is using", dest
70
+ elsif !@shortcuts[from].include?(shortcut_name)
71
+ #new shortcut
72
+ @shortcuts[from][shortcut_name] = command_to_run
73
+ @shortcuts[:all][shortcut_name] = command_to_run if for_all.to_s != ""
74
+ update_shortcuts_file()
75
+ respond "shortcut added", dest
52
76
  else
53
- case @questions[from]
54
- when /^(yes|yep)/i
55
- @shortcuts[from][shortcut_name] = command_to_run
56
- @shortcuts[:all][shortcut_name] = command_to_run if for_all.to_s != ""
57
- update_shortcuts_file()
58
- respond "shortcut added", dest
59
- @questions.delete(from)
60
- when /^no/i
61
- respond "ok, I won't add it", dest
62
- @questions.delete(from)
77
+ #are you sure? to avoid overwriting existing
78
+ if answer.empty?
79
+ ask("The shortcut already exists, are you sure you want to overwrite it?", command, from, dest)
63
80
  else
64
- ask "I don't understand, yes or no?", command, from, dest
81
+ case answer
82
+ when /^(yes|yep)/i
83
+ @shortcuts[from][shortcut_name] = command_to_run
84
+ @shortcuts[:all][shortcut_name] = command_to_run if for_all.to_s != ""
85
+ update_shortcuts_file()
86
+ respond "shortcut added", dest
87
+ answer_delete(from)
88
+ when /^no/i
89
+ respond "ok, I won't add it", dest
90
+ answer_delete(from)
91
+ else
92
+ ask "I don't understand, yes or no?", command, from, dest
93
+ end
65
94
  end
66
95
  end
67
96
  end
@@ -1,31 +1,42 @@
1
1
  class SlackSmartBot
2
2
  # helpadmin: ----------------------------------------------
3
3
  # helpadmin: `add routine NAME every NUMBER PERIOD COMMAND`
4
+ # helpadmin: `add routine NAME every NUMBER PERIOD #CHANNEL COMMAND`
4
5
  # helpadmin: `add routine NAME every NUMBER PERIOD`
6
+ # helpadmin: `add bgroutine NAME every NUMBER PERIOD`
5
7
  # helpadmin: `add silent routine NAME every NUMBER PERIOD`
8
+ # helpadmin: `add silent bgroutine NAME every NUMBER PERIOD`
6
9
  # helpadmin: `create routine NAME every NUMBER PERIOD`
7
10
  # helpadmin: `add routine NAME at TIME COMMAND`
11
+ # helpadmin: `add routine NAME at TIME #CHANNEL COMMAND`
8
12
  # helpadmin: `add routine NAME on DAYWEEK at TIME COMMAND`
13
+ # helpadmin: `add routine NAME on DAYWEEK at TIME #CHANNEL COMMAND`
9
14
  # helpadmin: `add routine NAME at TIME`
10
15
  # helpadmin: `add silent routine NAME at TIME`
11
16
  # helpadmin: `create routine NAME at TIME`
12
17
  # helpadmin: It will execute the command/rule supplied. Only for Admin and Master Admins.
13
18
  # helpadmin: If no COMMAND supplied, then it will be necessary to attach a file with the code to be run and add this command as message to the file. ONLY for MASTER ADMINS.
19
+ # helpadmin: In case *bgroutine* then the results of the run won't be published. To see the results call: `see result routine NAME`
14
20
  # helpadmin: In case *silent* provided then when executed will be only displayed if the routine returns a message
15
21
  # helpadmin: NAME: one word to identify the routine
16
22
  # helpadmin: NUMBER: Integer
17
23
  # helpadmin: PERIOD: days, d, hours, h, minutes, mins, min, m, seconds, secs, sec, s
18
24
  # helpadmin: TIME: time at format HH:MM:SS
19
- # helpadmin: DAYWEEK: Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday. And their plurals.
25
+ # helpadmin: DAYWEEK: Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday. And their plurals. Also possible to be used 'weekdays' and 'weekends'
26
+ # helpadmin: #CHANNEL: the destination channel where the results will be published. If not supplied then the bot channel by default or a DM if the command is run from a DM.
20
27
  # helpadmin: COMMAND: any valid smart bot command or rule
28
+ # helpadmin: It is possible to add a script directly. Only master admins can do it.
21
29
  # helpadmin: Examples:
22
- # helpadmin: _add routine example every 30s ruby puts 'a'_
23
- # helpadmin: _add routine example every 3 days ruby puts 'a'_
24
- # helpadmin: _add routine example at 17:05 ruby puts 'a'_
25
- # helpadmin: _create silent routine every 12 hours !Run customer tests_
26
- # helpadmin: _add routine example on Mondays at 05:00 !run customer tests_
30
+ # helpadmin: _add routine example every 30s !ruby puts 'a'_
31
+ # helpadmin: _add bgroutine example every 3 days !ruby puts 'a'_
32
+ # helpadmin: _add routine example at 17:05 !ruby puts 'a'_
33
+ # helpadmin: _create silent routine Example every 12 hours !Run customer tests_
34
+ # helpadmin: _add bgroutine example on Mondays at 05:00 !run customer tests_
35
+ # helpadmin: _add routine example on Tuesdays at 09:00 #SREChannel !run db cleanup_
36
+ # helpadmin: _add routine example on weekdays at 22:00 suggest command_
37
+ # helpadmin: <https://github.com/MarioRuiz/slack-smart-bot#routines|more info>
27
38
  # helpadmin:
28
- def add_routine(dest, from, user, name, type, number_time, period, command_to_run, files, silent)
39
+ def add_routine(dest, from, user, name, type, number_time, period, command_to_run, files, silent, channel, routine_type)
29
40
  save_stats(__method__)
30
41
  if files.nil? or files.size == 0 or (files.size > 0 and config.masters.include?(from))
31
42
  if config.admins.include?(from)
@@ -56,8 +67,9 @@ class SlackSmartBot
56
67
  every = "#{number_time} seconds"
57
68
  every_in_seconds = number_time.to_i
58
69
  else # time
59
- if type != 'at'
70
+ if type != 'at' and type!='weekday' and type!='weekend'
60
71
  dayweek = type.downcase
72
+
61
73
  days = ['sunday','monday','tuesday','wednesday','thursday','friday','saturday']
62
74
  incr = days.index(dayweek) - Time.now.wday
63
75
  if incr < 0
@@ -67,6 +79,14 @@ class SlackSmartBot
67
79
  end
68
80
  days = incr/(24*60*60)
69
81
  every_in_seconds = 7 * 24 * 60 * 60 # one week
82
+ elsif type=='weekend'
83
+ dayweek = type.downcase
84
+ days = 0
85
+ every_in_seconds = 24 * 60 * 60 # one day
86
+ elsif type=='weekday'
87
+ dayweek = type.downcase
88
+ days = 0
89
+ every_in_seconds = 24 * 60 * 60 # one day
70
90
  else
71
91
  days = 0
72
92
  every_in_seconds = 24 * 60 * 60 # one day
@@ -90,20 +110,35 @@ class SlackSmartBot
90
110
  if files[0].filetype == "ruby" and files[0].name.scan(/[^\.]+(\.\w+$)/).join == ''
91
111
  file_path += ".rb"
92
112
  end
93
- http = NiceHttp.new(host: "https://files.slack.com", headers: { "Authorization" => "Bearer #{config[:token]}" }, log_headers: :partial)
94
- http.get(files[0].url_private_download, save_data: file_path)
113
+ if files[0].key?(:content)
114
+ File.open(file_path, 'w') do |f|
115
+ f.write files[0].content
116
+ end
117
+ else
118
+ http = NiceHttp.new(host: "https://files.slack.com", headers: { "Authorization" => "Bearer #{config[:token]}" }, log_headers: :partial)
119
+ http.get(files[0].url_private_download, save_data: file_path)
120
+ end
95
121
  system("chmod +x #{file_path}")
96
122
  end
97
-
123
+ get_channels_name_and_id() unless @channels_name.keys.include?(channel) or @channels_id.keys.include?(channel)
124
+ channel_id = nil
125
+ if @channels_name.key?(channel) #it is an id
126
+ channel_id = channel
127
+ channel = @channels_name[channel_id]
128
+ elsif @channels_id.key?(channel) #it is a channel name
129
+ channel_id = @channels_id[channel]
130
+ end
131
+
132
+ channel_id = dest if channel_id.to_s == ''
98
133
  @routines[@channel_id] = {} unless @routines.key?(@channel_id)
99
134
  @routines[@channel_id][name] = { channel_name: config.channel, creator: from, creator_id: user.id, status: :on,
100
135
  every: every, every_in_seconds: every_in_seconds, at: at, dayweek: dayweek, file_path: file_path,
101
136
  command: command_to_run.to_s.strip, silent: silent,
102
- next_run: next_run.to_s, dest: dest, last_run: "", last_elapsed: "",
103
- running: false }
137
+ next_run: next_run.to_s, dest: channel_id, last_run: "", last_elapsed: "",
138
+ running: false, routine_type: routine_type}
104
139
  update_routines
105
140
  respond "Added routine *`#{name}`* to the channel", dest
106
- create_routine_thread(name)
141
+ create_routine_thread(name, @routines[@channel_id][name])
107
142
  end
108
143
  end
109
144
  else
@@ -4,6 +4,7 @@ class SlackSmartBot
4
4
  # helpadmin: `extend rules to CHANNEL_NAME`
5
5
  # helpadmin: `use rules on CHANNEL_NAME`
6
6
  # helpadmin: It will allow to use the specific rules from this channel on the CHANNEL_NAME
7
+ # helpadmin: <https://github.com/MarioRuiz/slack-smart-bot#extending-rules-to-other-channels|more info>
7
8
  # helpadmin:
8
9
  def extend_rules(dest, user, from, channel, typem)
9
10
  save_stats(__method__)
@@ -14,15 +15,12 @@ class SlackSmartBot
14
15
  respond "Only admins can extend the rules. Admins on this channel: #{config.admins}", dest
15
16
  else
16
17
  #todo: add pagination for case more than 1000 channels on the workspace
17
- channels = client.web_client.conversations_list(
18
- types: "private_channel,public_channel",
19
- limit: "1000",
20
- exclude_archived: "true",
21
- ).channels
18
+ channels = get_channels()
19
+ channel = @channels_name[channel] if @channels_name.key?(channel)
22
20
 
23
21
  channel_found = channels.detect { |c| c.name == channel }
24
22
  get_channels_name_and_id()
25
- members = client.web_client.conversations_members(channel: @channels_id[channel]).members unless channel_found.nil?
23
+ members = get_channel_members(@channels_id[channel]) unless channel_found.nil?
26
24
  get_bots_created()
27
25
  channels_in_use = []
28
26
  @bots_created.each do |k, v|
@@ -39,7 +37,7 @@ class SlackSmartBot
39
37
  elsif !members.include?(user.id)
40
38
  respond "You need to join that channel first", dest
41
39
  elsif !members.include?(config[:nick_id])
42
- respond "You need to add first to the channel the smart bot user: #{config[:nick]}", dest
40
+ respond "You need to add first to the channel the smart bot user: <@#{config[:nick_id]}>", dest
43
41
  else
44
42
  channels_in_use.each do |channel_in_use|
45
43
  respond "The rules from channel <##{@channels_id[channel_in_use]}> are already in use on that channel", dest
@@ -5,6 +5,7 @@ class SlackSmartBot
5
5
  # helpadmin: `pause this bot`
6
6
  # helpadmin: the bot will pause so it will listen only to admin commands
7
7
  # helpadmin: You can use this command only if you are an admin user
8
+ # helpadmin: <https://github.com/MarioRuiz/slack-smart-bot#bot-management|more info>
8
9
  # helpadmin:
9
10
  def pause_bot(dest, from)
10
11
  save_stats(__method__)
@@ -12,10 +13,12 @@ class SlackSmartBot
12
13
  respond "This bot is paused from now on. You can start it again: start this bot", dest
13
14
  respond "zZzzzzZzzzzZZZZZZzzzzzzzz", dest
14
15
  @status = :paused
15
- @bots_created[@channel_id][:status] = :paused
16
16
  unless config.on_master_bot
17
+ @bots_created[@channel_id][:status] = :paused
18
+ update_bots_file()
17
19
  send_msg_channel config.master_channel, "Changed status on #{config.channel} to :paused"
18
20
  end
21
+ save_status :off, :paused, 'The admin paused this bot'
19
22
  else
20
23
  respond "Only admin users can put me on pause", dest
21
24
  end
@@ -6,6 +6,7 @@ class SlackSmartBot
6
6
  # helpadmin: NAME: one word to identify the routine
7
7
  # helpadmin: Examples:
8
8
  # helpadmin: _pause routine example_
9
+ # helpadmin: <https://github.com/MarioRuiz/slack-smart-bot#routines|more info>
9
10
  # helpadmin:
10
11
  def pause_routine(dest, from, name)
11
12
  save_stats(__method__)
@@ -9,13 +9,12 @@ class SlackSmartBot
9
9
  # helpadmin: NAME: one word to identify the routine
10
10
  # helpadmin: Examples:
11
11
  # helpadmin: _kill routine example_
12
+ # helpadmin: <https://github.com/MarioRuiz/slack-smart-bot#routines|more info>
12
13
  # helpadmin:
13
14
  def remove_routine(dest, from, name)
14
15
  save_stats(__method__)
15
16
  if config.admins.include?(from) #admin user
16
- if !config.on_master_bot and dest[0] == "D"
17
- respond "It's only possible to remove routines from MASTER channel from a direct message with the bot.", dest
18
- elsif @routines.key?(@channel_id) and @routines[@channel_id].key?(name)
17
+ if @routines.key?(@channel_id) and @routines[@channel_id].key?(name)
19
18
  @routines[@channel_id][name][:thread].exit
20
19
  @routines[@channel_id].delete(name)
21
20
  update_routines()
@@ -8,6 +8,7 @@ class SlackSmartBot
8
8
  # helpadmin: NAME: one word to identify the routine
9
9
  # helpadmin: Examples:
10
10
  # helpadmin: _run routine example_
11
+ # helpadmin: <https://github.com/MarioRuiz/slack-smart-bot#routines|more info>
11
12
  # helpadmin:
12
13
 
13
14
  def run_routine(dest, from, name)
@@ -16,6 +17,7 @@ class SlackSmartBot
16
17
  if !config.on_master_bot and dest[0] == "D"
17
18
  respond "It's only possible to run routines from MASTER channel from a direct message with the bot.", dest
18
19
  elsif @routines.key?(@channel_id) and @routines[@channel_id].key?(name)
20
+ File.delete "#{config.path}/routines/#{@channel_id}/#{name}_output.txt" if File.exists?("#{config.path}/routines/#{@channel_id}/#{name}_output.txt")
19
21
  if @routines[@channel_id][name][:file_path] != ""
20
22
  if @routines[@channel_id][name][:file_path].match?(/\.rb$/i)
21
23
  ruby = "ruby "
@@ -40,7 +42,10 @@ class SlackSmartBot
40
42
  treat_message({ channel: @routines[@channel_id][name][:dest],
41
43
  user: @routines[@channel_id][name][:creator_id],
42
44
  text: @routines[@channel_id][name][:command],
43
- files: nil })
45
+ files: nil,
46
+ routine_name: name,
47
+ routine_type: @routines[@channel_id][name][:routine_type],
48
+ routine: true })
44
49
  end
45
50
  @routines[@channel_id][name][:last_elapsed] = (Time.now - started)
46
51
  @routines[@channel_id][name][:last_run] = started.to_s
@@ -0,0 +1,32 @@
1
+ class SlackSmartBot
2
+
3
+ # helpadmin: ----------------------------------------------
4
+ # helpadmin: `see routine result NAME`
5
+ # helpadmin: `see result routine NAME`
6
+ # helpadmin: `result routine NAME`
7
+ # helpadmin: It will display the last result of the routine run.
8
+ # helpadmin: You can use this command only if you are an admin user
9
+ # helpadmin: NAME: one word to identify the routine
10
+ # helpadmin: Examples:
11
+ # helpadmin: _see routine result example_
12
+ # helpadmin: <https://github.com/MarioRuiz/slack-smart-bot#routines|more info>
13
+ # helpadmin:
14
+ def see_result_routine(dest, from, name)
15
+ save_stats(__method__)
16
+ if config.admins.include?(from) #admin user
17
+ if @routines.key?(@channel_id) and @routines[@channel_id].key?(name)
18
+ if File.exist?("#{config.path}/routines/#{@channel_id}/#{name}_output.txt")
19
+ msg = "*Results from routine run #{File.mtime("#{config.path}/routines/#{@channel_id}/#{name}_output.txt")}*\n"
20
+ msg += File.read("#{config.path}/routines/#{@channel_id}/#{name}_output.txt")
21
+ respond msg, dest
22
+ else
23
+ respond "The routine *`#{name}`* doesn't have any result yet.", dest
24
+ end
25
+ else
26
+ respond "There isn't a routine with that name: *`#{name}`*.\nCall `see routines` to see added routines", dest
27
+ end
28
+ else
29
+ respond "Only admin users can see the routines results", dest
30
+ end
31
+ end
32
+ end
@@ -5,6 +5,7 @@ class SlackSmartBot
5
5
  # helpadmin: It will show the routines of the channel
6
6
  # helpadmin: In case of `all` and on the master channel, it will show all the routines from all channels
7
7
  # helpadmin: You can use this command only if you are an admin user
8
+ # helpadmin: <https://github.com/MarioRuiz/slack-smart-bot#routines|more info>
8
9
  # helpadmin:
9
10
  def see_routines(dest, from, user, all)
10
11
  save_stats(__method__)
@@ -23,8 +24,8 @@ class SlackSmartBot
23
24
  routines = @routines.dup
24
25
  end
25
26
  else
26
- if @rules_imported.key?(user.id) and @rules_imported[user.id].key?(user.id) and dest[0] == "D"
27
- file_conf = IO.readlines("#{config.path}/routines/routines_#{@rules_imported[user.id][user.id]}.rb").join
27
+ if @rules_imported.key?(user.name) and @rules_imported[user.name].key?(user.name) and dest[0] == "D"
28
+ file_conf = IO.readlines("#{config.path}/routines/routines_#{@rules_imported[user.name][user.name]}.rb").join
28
29
  routines = eval(file_conf)
29
30
  else
30
31
  routines = @routines.dup
@@ -38,8 +39,14 @@ class SlackSmartBot
38
39
  respond "Routines on channel *#{rout_ch.get_values(:channel_name).values.flatten.uniq[0]}*", dest
39
40
  rout_ch.each do |k, v|
40
41
  msg = []
41
- ch != v[:dest] ? directm = " (*DM to #{v[:creator]}*)" : directm = ""
42
- msg << "*`#{k}`*#{directm}"
42
+ if v[:dest][0] == 'D'
43
+ extram = " (*DM to #{v[:creator]}*)"
44
+ elsif v[:dest] != ch
45
+ extram = " (*publish on <##{v[:dest]}>*)"
46
+ else
47
+ extram = ''
48
+ end
49
+ msg << "*`#{k}`*#{extram}"
43
50
  msg << "\tCreator: #{v[:creator]}"
44
51
  msg << "\tStatus: #{v[:status]}"
45
52
  msg << "\tEvery: #{v[:every]}" unless v[:every] == ""
@@ -51,6 +58,7 @@ class SlackSmartBot
51
58
  msg << "\tCommand: #{v[:command]}" unless v[:command].to_s.strip == ''
52
59
  msg << "\tFile: #{v[:file_path]}" unless v[:file_path] == ''
53
60
  msg << "\tSilent: #{v[:silent]}" unless !v[:silent]
61
+ msg << "\tType: #{v[:routine_type]}" if v[:routine_type].to_s == 'bgroutine'
54
62
  respond msg.join("\n"), dest
55
63
  end
56
64
  end
@@ -5,16 +5,19 @@ class SlackSmartBot
5
5
  # helpadmin: `start this bot`
6
6
  # helpadmin: the bot will start to listen
7
7
  # helpadmin: You can use this command only if you are an admin user
8
+ # helpadmin: <https://github.com/MarioRuiz/slack-smart-bot#bot-management|more info>
8
9
  # helpadmin:
9
10
  def start_bot(dest, from)
10
11
  save_stats(__method__)
11
12
  if config.admins.include?(from) #admin user
12
13
  respond "This bot is running and listening from now on. You can pause again: pause this bot", dest
13
14
  @status = :on
14
- @bots_created[@channel_id][:status] = :on
15
15
  unless config.on_master_bot
16
+ @bots_created[@channel_id][:status] = :on
17
+ update_bots_file()
16
18
  send_msg_channel config.master_channel, "Changed status on #{config.channel} to :on"
17
19
  end
20
+ save_status :on, :started, 'The admin started this bot'
18
21
  else
19
22
  respond "Only admin users can change my status", dest
20
23
  end