slack-smart-bot 1.9.1 → 1.11.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +184 -16
- data/lib/slack/smart-bot/comm/ask.rb +55 -49
- data/lib/slack/smart-bot/comm/delete.rb +13 -0
- data/lib/slack/smart-bot/comm/dont_understand.rb +3 -3
- data/lib/slack/smart-bot/comm/event_hello.rb +8 -4
- data/lib/slack/smart-bot/comm/get_channel_members.rb +13 -4
- data/lib/slack/smart-bot/comm/get_channels.rb +31 -16
- data/lib/slack/smart-bot/comm/get_presence.rb +20 -0
- data/lib/slack/smart-bot/comm/get_user_info.rb +12 -8
- data/lib/slack/smart-bot/comm/get_users.rb +24 -0
- data/lib/slack/smart-bot/comm/react.rb +19 -2
- data/lib/slack/smart-bot/comm/respond.rb +224 -53
- data/lib/slack/smart-bot/comm/respond_direct.rb +2 -3
- data/lib/slack/smart-bot/comm/respond_thread.rb +5 -0
- data/lib/slack/smart-bot/comm/send_file.rb +38 -34
- data/lib/slack/smart-bot/comm/send_msg_channel.rb +27 -22
- data/lib/slack/smart-bot/comm/send_msg_user.rb +59 -33
- data/lib/slack/smart-bot/comm/unreact.rb +22 -18
- data/lib/slack/smart-bot/comm.rb +4 -0
- data/lib/slack/smart-bot/commands/general/add_admin.rb +51 -0
- data/lib/slack/smart-bot/commands/general/add_announcement.rb +32 -0
- data/lib/slack/smart-bot/commands/general/add_team.rb +80 -0
- data/lib/slack/smart-bot/commands/general/allow_access.rb +67 -0
- data/lib/slack/smart-bot/commands/general/bot_help.rb +69 -33
- data/lib/slack/smart-bot/commands/general/bye_bot.rb +0 -7
- data/lib/slack/smart-bot/commands/general/delete_announcement.rb +34 -0
- data/lib/slack/smart-bot/commands/general/delete_share.rb +34 -0
- data/lib/slack/smart-bot/commands/general/delete_team.rb +34 -0
- data/lib/slack/smart-bot/commands/general/deny_access.rb +36 -0
- data/lib/slack/smart-bot/commands/general/hi_bot.rb +16 -11
- data/lib/slack/smart-bot/commands/general/ping_team.rb +100 -0
- data/lib/slack/smart-bot/commands/general/poster.rb +116 -0
- data/lib/slack/smart-bot/commands/general/remove_admin.rb +58 -0
- data/lib/slack/smart-bot/commands/general/see_access.rb +24 -0
- data/lib/slack/smart-bot/commands/general/see_admins.rb +33 -0
- data/lib/slack/smart-bot/commands/general/see_announcements.rb +115 -0
- data/lib/slack/smart-bot/commands/general/see_command_ids.rb +29 -0
- data/lib/slack/smart-bot/commands/general/see_favorite_commands.rb +53 -0
- data/lib/slack/smart-bot/commands/general/see_shares.rb +41 -0
- data/lib/slack/smart-bot/commands/general/see_statuses.rb +91 -0
- data/lib/slack/smart-bot/commands/general/see_teams.rb +252 -0
- data/lib/slack/smart-bot/commands/general/share_messages.rb +58 -0
- data/lib/slack/smart-bot/commands/general/update_team.rb +109 -0
- data/lib/slack/smart-bot/commands/general_bot_commands.rb +504 -0
- data/lib/slack/smart-bot/commands/on_bot/add_shortcut.rb +4 -6
- data/lib/slack/smart-bot/commands/on_bot/admin/add_routine.rb +45 -14
- data/lib/slack/smart-bot/commands/on_bot/admin/extend_rules.rb +4 -1
- data/lib/slack/smart-bot/commands/on_bot/admin/pause_bot.rb +6 -3
- data/lib/slack/smart-bot/commands/on_bot/admin/pause_routine.rb +3 -1
- data/lib/slack/smart-bot/commands/on_bot/admin/remove_routine.rb +4 -4
- data/lib/slack/smart-bot/commands/on_bot/admin/run_routine.rb +8 -2
- data/lib/slack/smart-bot/commands/on_bot/admin/see_result_routine.rb +33 -0
- data/lib/slack/smart-bot/commands/on_bot/admin/see_routines.rb +13 -10
- data/lib/slack/smart-bot/commands/on_bot/admin/start_bot.rb +6 -3
- data/lib/slack/smart-bot/commands/on_bot/admin/start_routine.rb +3 -1
- data/lib/slack/smart-bot/commands/on_bot/admin/stop_using_rules_on.rb +4 -1
- data/lib/slack/smart-bot/commands/on_bot/admin_master/delete_message.rb +25 -0
- data/lib/slack/smart-bot/commands/on_bot/admin_master/get_bot_logs.rb +1 -0
- data/lib/slack/smart-bot/commands/on_bot/admin_master/react_to.rb +34 -0
- data/lib/slack/smart-bot/commands/on_bot/admin_master/send_message.rb +37 -0
- data/lib/slack/smart-bot/commands/on_bot/delete_repl.rb +4 -5
- data/lib/slack/smart-bot/commands/on_bot/delete_shortcut.rb +7 -8
- data/lib/slack/smart-bot/commands/on_bot/general/bot_stats.rb +400 -0
- data/lib/slack/smart-bot/commands/{general → on_bot/general}/bot_status.rb +3 -4
- data/lib/slack/smart-bot/commands/on_bot/general/leaderboard.rb +201 -0
- data/lib/slack/smart-bot/commands/{general → on_bot/general}/stop_using_rules.rb +12 -6
- data/lib/slack/smart-bot/commands/on_bot/general/suggest_command.rb +36 -0
- data/lib/slack/smart-bot/commands/{general → on_bot/general}/use_rules.rb +13 -11
- data/lib/slack/smart-bot/commands/{general → on_bot/general}/whats_new.rb +2 -1
- data/lib/slack/smart-bot/commands/on_bot/get_repl.rb +4 -5
- data/lib/slack/smart-bot/commands/on_bot/repl.rb +76 -21
- data/lib/slack/smart-bot/commands/on_bot/ruby_code.rb +3 -4
- data/lib/slack/smart-bot/commands/on_bot/run_repl.rb +15 -7
- data/lib/slack/smart-bot/commands/on_bot/see_repls.rb +5 -6
- data/lib/slack/smart-bot/commands/on_bot/see_shortcuts.rb +5 -6
- data/lib/slack/smart-bot/commands/on_extended/bot_rules.rb +45 -12
- data/lib/slack/smart-bot/commands/on_master/admin/kill_bot_on_channel.rb +7 -3
- data/lib/slack/smart-bot/commands/on_master/admin_master/exit_bot.rb +4 -1
- data/lib/slack/smart-bot/commands/on_master/admin_master/notify_message.rb +3 -1
- data/lib/slack/smart-bot/commands/on_master/admin_master/publish_announcements.rb +33 -0
- data/lib/slack/smart-bot/commands/on_master/admin_master/set_general_message.rb +39 -0
- data/lib/slack/smart-bot/commands/on_master/admin_master/set_maintenance.rb +10 -1
- data/lib/slack/smart-bot/commands/on_master/create_bot.rb +28 -14
- data/lib/slack/smart-bot/commands/on_master/where_smartbot.rb +41 -0
- data/lib/slack/smart-bot/commands.rb +36 -5
- data/lib/slack/smart-bot/listen.rb +31 -33
- data/lib/slack/smart-bot/process.rb +234 -73
- data/lib/slack/smart-bot/process_first.rb +119 -38
- data/lib/slack/smart-bot/treat_message.rb +310 -237
- data/lib/slack/smart-bot/utils/build_help.rb +2 -2
- data/lib/slack/smart-bot/utils/create_routine_thread.rb +81 -46
- data/lib/slack/smart-bot/utils/get_access_channels.rb +13 -0
- data/lib/slack/smart-bot/utils/get_admins_channels.rb +13 -0
- data/lib/slack/smart-bot/utils/get_bots_created.rb +28 -8
- data/lib/slack/smart-bot/utils/get_channels_name_and_id.rb +7 -2
- data/lib/slack/smart-bot/utils/get_command_ids.rb +84 -0
- data/lib/slack/smart-bot/utils/get_help.rb +79 -73
- data/lib/slack/smart-bot/utils/get_repls.rb +22 -2
- data/lib/slack/smart-bot/utils/get_routines.rb +22 -2
- data/lib/slack/smart-bot/utils/get_shares.rb +12 -0
- data/lib/slack/smart-bot/utils/get_teams.rb +22 -0
- data/lib/slack/smart-bot/utils/has_access.rb +28 -0
- data/lib/slack/smart-bot/utils/is_admin.rb +27 -0
- data/lib/slack/smart-bot/utils/save_stats.rb +46 -41
- data/lib/slack/smart-bot/utils/save_status.rb +67 -0
- data/lib/slack/smart-bot/utils/update_access_channels.rb +8 -0
- data/lib/slack/smart-bot/utils/update_admins_channels.rb +8 -0
- data/lib/slack/smart-bot/utils/update_bots_file.rb +28 -7
- data/lib/slack/smart-bot/utils/update_repls.rb +7 -4
- data/lib/slack/smart-bot/utils/update_routines.rb +9 -3
- data/lib/slack/smart-bot/utils/update_shortcuts_file.rb +13 -6
- data/lib/slack/smart-bot/utils/update_teams.rb +16 -0
- data/lib/slack/smart-bot/utils.rb +11 -0
- data/lib/slack-smart-bot.rb +72 -12
- data/lib/slack-smart-bot_general_commands.rb +61 -0
- data/lib/slack-smart-bot_general_rules.rb +5 -2
- data/lib/slack-smart-bot_rules.rb +43 -17
- data/whats_new.txt +20 -15
- metadata +76 -9
- data/lib/slack/smart-bot/commands/general/bot_stats.rb +0 -313
@@ -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,63 +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
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
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.exist?("#{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
|
52
|
+
end
|
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
|
36
61
|
else
|
37
|
-
|
62
|
+
File.write("#{config.path}/routines/#{@channel_id}/#{name}_output.txt", stdout.to_s+stderr.to_s, mode: "a+")
|
38
63
|
end
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
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
|
43
71
|
end
|
44
|
-
|
45
|
-
|
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)
|
46
82
|
end
|
47
|
-
|
48
|
-
if !@routines[@channel_id]
|
49
|
-
|
50
|
-
respond "routine from <##{@channel_id}> *`#{name}`*: #{@routines[@channel_id][name][:command]}", @routines[@channel_id][name][:dest]
|
51
|
-
else
|
52
|
-
respond "routine *`#{name}`*: #{@routines[@channel_id][name][:command]}", @routines[@channel_id][name][:dest]
|
53
|
-
end
|
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
|
54
86
|
end
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
text: @routines[@channel_id][name][:command],
|
60
|
-
files: nil,
|
61
|
-
routine: true }
|
62
|
-
treat_message(data)
|
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
|
63
91
|
end
|
64
|
-
# in case the routine was deleted while running the process
|
65
|
-
if !@routines.key?(@channel_id) or !@routines[@channel_id].key?(name)
|
66
|
-
Thread.exit
|
67
|
-
end
|
68
|
-
@routines[@channel_id][name][:last_run] = started.to_s
|
69
92
|
end
|
70
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
|
71
94
|
elapsed = 0
|
72
95
|
require "time"
|
73
96
|
every_in_seconds = Time.parse(@routines[@channel_id][name][:next_run]) - Time.now
|
74
97
|
elsif @routines[@channel_id][name][:at] != "" #coming from start after pause for 'at'
|
75
|
-
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
|
+
|
76
101
|
day = @routines[@channel_id][name][:dayweek]
|
77
102
|
days = ['sunday','monday','tuesday','wednesday','thursday','friday','saturday']
|
78
103
|
incr = days.index(day) - Time.now.wday
|
@@ -83,6 +108,16 @@ class SlackSmartBot
|
|
83
108
|
end
|
84
109
|
days = incr/(24*60*60)
|
85
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
|
86
121
|
else
|
87
122
|
days = 0
|
88
123
|
weekly = false
|
@@ -0,0 +1,13 @@
|
|
1
|
+
class SlackSmartBot
|
2
|
+
|
3
|
+
def get_access_channels()
|
4
|
+
if File.exist?("#{config.path}/rules/#{@channel_id}/access_channels.rb")
|
5
|
+
file_conf = IO.readlines("#{config.path}/rules/#{@channel_id}/access_channels.rb").join
|
6
|
+
unless file_conf.to_s() == ""
|
7
|
+
@access_channels = eval(file_conf)
|
8
|
+
end
|
9
|
+
else
|
10
|
+
@access_channels = {}
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
class SlackSmartBot
|
2
|
+
|
3
|
+
def get_admins_channels()
|
4
|
+
if File.exist?("#{config.path}/rules/#{@channel_id}/admins_channels.rb")
|
5
|
+
file_conf = IO.readlines("#{config.path}/rules/#{@channel_id}/admins_channels.rb").join
|
6
|
+
unless file_conf.to_s() == ""
|
7
|
+
@admins_channels = eval(file_conf)
|
8
|
+
end
|
9
|
+
else
|
10
|
+
@admins_channels = {}
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -1,14 +1,33 @@
|
|
1
1
|
class SlackSmartBot
|
2
2
|
def get_bots_created
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
3
|
+
require 'yaml'
|
4
|
+
bots_file = config.file_path.gsub(".rb", "_bots.yaml")
|
5
|
+
|
6
|
+
if File.exist?(config.file_path.gsub(".rb", "_bots.rb")) #backwards compatible
|
7
|
+
file_conf = IO.readlines(config.file_path.gsub(".rb", "_bots.rb")).join
|
8
|
+
if file_conf.to_s() == ""
|
9
|
+
@bots_created = {}
|
10
|
+
else
|
11
|
+
@bots_created = eval(file_conf)
|
12
|
+
end
|
13
|
+
File.open(bots_file, 'w') {|file| file.write(@bots_created.to_yaml) }
|
14
|
+
File.delete(config.file_path.gsub(".rb", "_bots.rb"))
|
15
|
+
end
|
16
|
+
|
17
|
+
if File.exist?(bots_file)
|
18
|
+
|
19
|
+
if !defined?(@datetime_bots_created) or @datetime_bots_created != File.mtime(bots_file)
|
20
|
+
bots_created = @bots_created
|
21
|
+
10.times do
|
22
|
+
bots_created = YAML.load(File.read(bots_file))
|
23
|
+
if bots_created.is_a?(Hash)
|
24
|
+
break
|
25
|
+
else
|
26
|
+
sleep (0.1*(rand(2)+1))
|
27
|
+
end
|
10
28
|
end
|
11
|
-
@
|
29
|
+
@bots_created = bots_created unless bots_created.is_a?(FalseClass)
|
30
|
+
@datetime_bots_created = File.mtime(bots_file)
|
12
31
|
@extended_from = {}
|
13
32
|
@bots_created.each do |k, v|
|
14
33
|
v[:extended] = [] unless v.key?(:extended)
|
@@ -16,6 +35,7 @@ class SlackSmartBot
|
|
16
35
|
@extended_from[ch] = [] unless @extended_from.key?(ch)
|
17
36
|
@extended_from[ch] << k
|
18
37
|
end
|
38
|
+
v[:rules_file] ||= ''
|
19
39
|
v[:rules_file].gsub!(/^\./, '')
|
20
40
|
end
|
21
41
|
end
|
@@ -1,13 +1,18 @@
|
|
1
1
|
class SlackSmartBot
|
2
2
|
|
3
3
|
def get_channels_name_and_id
|
4
|
-
|
4
|
+
@channels_list = get_channels()
|
5
5
|
@channels_id = Hash.new()
|
6
6
|
@channels_name = Hash.new()
|
7
|
-
|
7
|
+
@channels_creator = Hash.new()
|
8
|
+
@users = get_users() if @users.empty?
|
9
|
+
@channels_list.each do |ch|
|
8
10
|
unless ch.is_archived
|
9
11
|
@channels_id[ch.name] = ch.id
|
10
12
|
@channels_name[ch.id] = ch.name
|
13
|
+
user_info = @users.select{|u| u.id == ch.creator or (u.key?(:enterprise_user) and u.enterprise_user.id == ch.creator)}[-1]
|
14
|
+
@channels_creator[ch.id] = user_info.name unless user_info.nil?
|
15
|
+
@channels_creator[ch.name] = user_info.name unless user_info.nil?
|
11
16
|
end
|
12
17
|
end
|
13
18
|
end
|
@@ -0,0 +1,84 @@
|
|
1
|
+
class SlackSmartBot
|
2
|
+
def get_command_ids
|
3
|
+
commands = {
|
4
|
+
general: [],
|
5
|
+
on_bot_general: [],
|
6
|
+
on_bot_on_demand: [],
|
7
|
+
on_bot_admin: [],
|
8
|
+
on_bot_master_admin: [],
|
9
|
+
on_extended: [],
|
10
|
+
on_master: [],
|
11
|
+
on_master_admin: [],
|
12
|
+
on_master_master_admin: [],
|
13
|
+
general_commands: [],
|
14
|
+
general_rules: [],
|
15
|
+
rules: []
|
16
|
+
}
|
17
|
+
typem = Thread.current[:typem]
|
18
|
+
user = Thread.current[:user]
|
19
|
+
# :on_call, :on_bot, :on_extended, :on_dm, :on_master, :on_pg, :on_pub
|
20
|
+
admin = is_admin?(user.name)
|
21
|
+
|
22
|
+
commands[:general] = (Dir.entries("#{__dir__}/../commands/general/").select { |e| e.match?(/\.rb/) }).sort.join('|').gsub('.rb','').split('|')
|
23
|
+
general = File.read("#{__dir__}/../commands/general_bot_commands.rb")
|
24
|
+
commands[:general] += general.scan(/^\s*#\s*help\w*:\s+command_id:\s+:(\w+)\s*$/i).flatten
|
25
|
+
commands[:general].uniq!
|
26
|
+
|
27
|
+
if typem == :on_bot or typem == :on_master
|
28
|
+
commands[:on_bot_general] = (Dir.entries("#{__dir__}/../commands/on_bot/general/").select { |e| e.match?(/\.rb/) }).sort.join('|').gsub('.rb','').split('|')
|
29
|
+
end
|
30
|
+
|
31
|
+
if typem == :on_bot or typem == :on_master
|
32
|
+
commands[:on_bot_on_demand] = (Dir.entries("#{__dir__}/../commands/on_bot/").select { |e| e.match?(/\.rb/) }).sort.join('|').gsub('.rb','').split('|')
|
33
|
+
end
|
34
|
+
|
35
|
+
if (typem == :on_bot or typem == :on_master) and admin
|
36
|
+
commands[:on_bot_admin] = (Dir.entries("#{__dir__}/../commands/on_bot/admin/").select { |e| e.match?(/\.rb/) }).sort.join('|').gsub('.rb','').split('|')
|
37
|
+
end
|
38
|
+
|
39
|
+
if (typem == :on_bot or typem == :on_master) and config.masters.include?(user.name)
|
40
|
+
commands[:on_bot_master_admin] = (Dir.entries("#{__dir__}/../commands/on_bot/admin_master/").select { |e| e.match?(/\.rb/) }).sort.join('|').gsub('.rb','').split('|')
|
41
|
+
end
|
42
|
+
|
43
|
+
if typem == :on_extended
|
44
|
+
commands[:on_extended] = (Dir.entries("#{__dir__}/../commands/on_extended/").select { |e| e.match?(/\.rb/) }).sort.join('|').gsub('.rb','').split('|')
|
45
|
+
commands[:on_extended]+= ['repl', 'see_repls', 'get_repl', 'run_repl', 'delete_repl', 'ruby_code']
|
46
|
+
end
|
47
|
+
|
48
|
+
if typem == :on_master
|
49
|
+
commands[:on_master] = (Dir.entries("#{__dir__}/../commands/on_master/").select { |e| e.match?(/\.rb/) }).sort.join('|').gsub('.rb','').split('|')
|
50
|
+
end
|
51
|
+
|
52
|
+
if typem == :on_master and admin
|
53
|
+
commands[:on_master_admin] = (Dir.entries("#{__dir__}/../commands/on_master/admin/").select { |e| e.match?(/\.rb/) }).sort.join('|').gsub('.rb','').split('|')
|
54
|
+
end
|
55
|
+
|
56
|
+
if typem == :on_master and config.masters.include?(user.name)
|
57
|
+
commands[:on_master_master_admin] = (Dir.entries("#{__dir__}/../commands/on_master/admin_master/").select { |e| e.match?(/\.rb/) }).sort.join('|').gsub('.rb','').split('|')
|
58
|
+
end
|
59
|
+
|
60
|
+
if File.exist?("#{config.path}/rules/general_commands.rb")
|
61
|
+
general_commands = File.read("#{config.path}/rules/general_commands.rb")
|
62
|
+
commands[:general_commands] = general_commands.scan(/^\s*#\s*help\w*:\s+command_id:\s+:(\w+)\s*$/i).flatten
|
63
|
+
commands[:general_commands]+= general_commands.scan(/^\s*save_stats\(?\s*:(\w+)\s*,?/i).flatten
|
64
|
+
commands[:general_commands].uniq!
|
65
|
+
end
|
66
|
+
|
67
|
+
if typem == :on_extended or typem ==:on_call or typem == :on_bot or typem == :on_master or (typem == :on_dm and Thread.current[:using_channel].to_s != '')
|
68
|
+
if Thread.current.key?(:rules_file) and File.exist?(config.path + Thread.current[:rules_file])
|
69
|
+
rules = File.read(config.path + Thread.current[:rules_file])
|
70
|
+
commands[:rules] = rules.scan(/^\s*#\s*help\w*:\s+command_id:\s+:(\w+)\s*$/i).flatten
|
71
|
+
commands[:rules]+= rules.scan(/^\s*save_stats\(?\s*:(\w+)\s*,?/i).flatten
|
72
|
+
commands[:rules].uniq!
|
73
|
+
|
74
|
+
if File.exist?("#{config.path}/rules/general_rules.rb")
|
75
|
+
general_rules = File.read("#{config.path}/rules/general_rules.rb")
|
76
|
+
commands[:general_rules] = general_rules.scan(/^\s*#\s*help\w*:\s+command_id:\s+:(\w+)\s*$/i).flatten
|
77
|
+
commands[:general_rules]+= general_rules.scan(/^\s*save_stats\(?\s*:(\w+)\s*,?/i).flatten
|
78
|
+
commands[:general_rules].uniq!
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
return commands
|
83
|
+
end
|
84
|
+
end
|
@@ -1,18 +1,22 @@
|
|
1
1
|
class SlackSmartBot
|
2
|
-
def get_help(rules_file, dest, from, only_rules, expanded)
|
2
|
+
def get_help(rules_file, dest, from, only_rules, expanded, descriptions: true, only_normal_user: false)
|
3
3
|
order = {
|
4
|
-
general: [:
|
4
|
+
general: [:bot_help, :hi_bot, :bye_bot, :add_admin, :remove_admin, :see_admins, :poster, :add_announcement, :delete_announcement,
|
5
|
+
:see_announcements, :see_command_ids, :share_messages, :see_shares, :delete_share, :see_favorite_commands, :see_statuses,
|
6
|
+
:allow_access, :see_access, :deny_access, :add_team, :see_teams, :update_team, :ping_team, :delete_team],
|
7
|
+
on_bot_general: [:whats_new, :suggest_command, :bot_status, :use_rules, :stop_using_rules, :bot_stats, :leaderboard],
|
5
8
|
on_bot: [:ruby_code, :repl, :get_repl, :run_repl, :delete_repl, :see_repls, :add_shortcut, :delete_shortcut, :see_shortcuts],
|
6
9
|
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]
|
10
|
+
:see_routines, :start_routine, :pause_routine, :remove_routine, :see_result_routine, :run_routine]
|
8
11
|
}
|
9
12
|
if config.masters.include?(from)
|
10
13
|
user_type = :master # master admin
|
11
|
-
elsif
|
14
|
+
elsif is_admin?(from)
|
12
15
|
user_type = :admin
|
13
16
|
else
|
14
17
|
user_type = :normal #normal user
|
15
18
|
end
|
19
|
+
|
16
20
|
# channel_type: :bot, :master_bot, :direct, :extended, :external
|
17
21
|
if dest[0] == "D"
|
18
22
|
channel_type = :direct
|
@@ -24,6 +28,15 @@ class SlackSmartBot
|
|
24
28
|
channel_type = :bot
|
25
29
|
end
|
26
30
|
|
31
|
+
if Thread.current[:typem] == :on_pg or Thread.current[:typem] == :on_pub
|
32
|
+
channel_type = :external
|
33
|
+
end
|
34
|
+
|
35
|
+
if only_normal_user
|
36
|
+
user_type = :normal
|
37
|
+
channel_type = :bot
|
38
|
+
end
|
39
|
+
|
27
40
|
@help_messages_expanded ||= build_help("#{__dir__}/../commands", true)
|
28
41
|
@help_messages_not_expanded ||= build_help("#{__dir__}/../commands", false)
|
29
42
|
if only_rules
|
@@ -33,7 +46,6 @@ class SlackSmartBot
|
|
33
46
|
else
|
34
47
|
help = @help_messages_not_expanded.deep_copy[user_type]
|
35
48
|
end
|
36
|
-
|
37
49
|
if rules_file != ""
|
38
50
|
help[:rules_file] = build_help(config.path+rules_file, expanded)[user_type].values.join("\n") + "\n"
|
39
51
|
|
@@ -55,6 +67,17 @@ class SlackSmartBot
|
|
55
67
|
help[:rules_file] += rhelp[user_type].values.join("\n") + "\n"
|
56
68
|
end
|
57
69
|
end
|
70
|
+
|
71
|
+
help[:general_commands_file] = build_help("#{__dir__}/../commands/general_bot_commands.rb", expanded)[user_type].values.join("\n") + "\n" unless only_rules
|
72
|
+
if File.exist?(config.path + '/rules/general_commands.rb') and !only_rules
|
73
|
+
help[:general_commands_file] += build_help(config.path+'/rules/general_commands.rb', expanded)[user_type].values.join("\n") + "\n"
|
74
|
+
end
|
75
|
+
if help.key?(:on_bot)
|
76
|
+
commands_on_extended_from_on_bot = [:repl, :see_repls, :get_repl, :run_repl, :delete_repl, :ruby_code]
|
77
|
+
commands_on_extended_from_on_bot.each do |cm|
|
78
|
+
help[:on_extended][cm] = help[:on_bot][cm] if help[:on_bot].key?(cm)
|
79
|
+
end
|
80
|
+
end
|
58
81
|
help = remove_hash_keys(help, :admin_master) unless user_type == :master
|
59
82
|
help = remove_hash_keys(help, :admin) unless user_type == :admin or user_type == :master
|
60
83
|
help = remove_hash_keys(help, :on_master) unless channel_type == :master_bot
|
@@ -62,7 +85,7 @@ class SlackSmartBot
|
|
62
85
|
help = remove_hash_keys(help, :on_dm) unless channel_type == :direct
|
63
86
|
txt = ""
|
64
87
|
|
65
|
-
if (channel_type == :bot or channel_type == :master_bot) and expanded
|
88
|
+
if (channel_type == :bot or channel_type == :master_bot) and expanded and descriptions
|
66
89
|
txt += "===================================
|
67
90
|
For the Smart Bot start listening to you say *hi bot*
|
68
91
|
To run a command on demand even when the Smart Bot is not listening to you:
|
@@ -73,11 +96,11 @@ class SlackSmartBot
|
|
73
96
|
*^THE_COMMAND*
|
74
97
|
*!!THE_COMMAND*\n"
|
75
98
|
end
|
76
|
-
if channel_type == :direct and expanded
|
99
|
+
if channel_type == :direct and expanded and descriptions
|
77
100
|
txt += "===================================
|
78
101
|
When on a private conversation with the Smart Bot, I'm always listening to you.\n"
|
79
102
|
end
|
80
|
-
unless channel_type == :master_bot or channel_type == :extended or !expanded
|
103
|
+
unless channel_type == :master_bot or channel_type == :extended or !expanded or !descriptions
|
81
104
|
txt += "===================================
|
82
105
|
*Commands from Channels without a bot:*
|
83
106
|
----------------------------------------------
|
@@ -86,38 +109,61 @@ class SlackSmartBot
|
|
86
109
|
It will run the supplied command using the rules on the channel supplied.
|
87
110
|
You need to join the specified channel to be able to use those rules.
|
88
111
|
Also you can use this command to call another bot from a channel with a running bot.
|
112
|
+
\n"
|
113
|
+
end
|
89
114
|
|
90
|
-
|
91
|
-
|
92
|
-
|
115
|
+
if help.key?(:general_commands_file)
|
116
|
+
txt += "===================================
|
117
|
+
*General commands on any channel where the Smart Bot is a member:*\n" if descriptions
|
118
|
+
txt += help.general_commands_file
|
93
119
|
end
|
94
120
|
|
95
|
-
if help.key?(:general)
|
96
|
-
|
97
|
-
|
98
|
-
|
121
|
+
if help.key?(:on_bot) and help.on_bot.key?(:general) and channel_type != :external and channel_type != :extended
|
122
|
+
if descriptions
|
123
|
+
if channel_type == :direct
|
124
|
+
txt += "===================================
|
125
|
+
*General commands:*\n"
|
126
|
+
else
|
127
|
+
txt += "===================================
|
128
|
+
*General commands on Bot channel even when the Smart Bot is not listening to you:*\n"
|
129
|
+
end
|
99
130
|
end
|
100
|
-
order.
|
101
|
-
txt += help.general[o]
|
131
|
+
order.on_bot_general.each do |o|
|
132
|
+
txt += help.on_bot.general[o]
|
102
133
|
end
|
103
134
|
if channel_type == :master_bot
|
104
135
|
txt += help.on_master.create_bot
|
136
|
+
txt += help.on_master.where_smartbot
|
105
137
|
end
|
106
138
|
end
|
107
139
|
|
108
|
-
if help.key?(:on_bot)
|
109
|
-
|
110
|
-
|
111
|
-
|
140
|
+
if help.key?(:on_bot) and channel_type != :external and channel_type != :extended
|
141
|
+
if descriptions
|
142
|
+
if channel_type == :direct
|
143
|
+
txt += "===================================
|
144
|
+
*General commands on bot, DM or on external call on demand:*\n"
|
145
|
+
else
|
146
|
+
txt += "===================================
|
147
|
+
*General commands on Bot channel only when the Smart Bot is listening to you or on demand:*\n"
|
148
|
+
end
|
112
149
|
end
|
113
150
|
order.on_bot.each do |o|
|
114
151
|
txt += help.on_bot[o]
|
115
152
|
end
|
116
153
|
end
|
117
|
-
if help.key?(:
|
154
|
+
if help.key?(:on_extended) and channel_type == :extended and help[:on_extended].keys.size > 0
|
155
|
+
if descriptions
|
156
|
+
txt += "===================================
|
157
|
+
*General commands on Extended channel only on demand:*\n"
|
158
|
+
end
|
159
|
+
commands_on_extended_from_on_bot.each do |o|
|
160
|
+
txt += help.on_extended[o]
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
164
|
+
if help.key?(:on_bot) and help.on_bot.key?(:admin) and channel_type != :external and channel_type != :extended
|
118
165
|
txt += "===================================
|
119
|
-
*Admin commands:*\n"
|
120
|
-
txt += "\n\n"
|
166
|
+
*Admin commands:*\n\n" if descriptions
|
121
167
|
order.on_bot_admin.each do |o|
|
122
168
|
txt += help.on_bot.admin[o]
|
123
169
|
end
|
@@ -128,76 +174,36 @@ class SlackSmartBot
|
|
128
174
|
end
|
129
175
|
end
|
130
176
|
|
131
|
-
if help.key?(:on_bot) and help.on_bot.key?(:admin_master) and help.on_bot.admin_master.size > 0
|
177
|
+
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
|
132
178
|
txt += "===================================
|
133
|
-
|
179
|
+
*Master Admin commands:*\n" if descriptions
|
134
180
|
help.on_bot.admin_master.each do |k, v|
|
135
181
|
txt += v if v.is_a?(String)
|
136
182
|
end
|
137
183
|
end
|
138
184
|
|
139
|
-
if help.key?(:on_master) and help.on_master.key?(:admin_master) and help.on_master.admin_master.size > 0
|
185
|
+
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
|
140
186
|
txt += "===================================
|
141
|
-
|
187
|
+
*Master Admin commands:*\n" unless txt.include?('*Master Admin commands*') or !descriptions
|
142
188
|
help.on_master.admin_master.each do |k, v|
|
143
189
|
txt += v if v.is_a?(String)
|
144
190
|
end
|
145
191
|
end
|
146
192
|
|
147
|
-
if help.key?(:rules_file)
|
193
|
+
if help.key?(:rules_file) and channel_type != :external
|
148
194
|
@logger.info channel_type if config.testing
|
149
195
|
if channel_type == :extended or channel_type == :direct
|
150
196
|
@logger.info help.rules_file if config.testing
|
151
197
|
help.rules_file = help.rules_file.gsub(/^\s*\*These are specific commands.+NAME_OF_BOT THE_COMMAND`\s*$/im, "")
|
152
198
|
end
|
153
199
|
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
command_done = false
|
158
|
-
explanation_done = false
|
159
|
-
example_done = false
|
160
|
-
if rule.match?(/These are specific commands for this/i)
|
161
|
-
resf += rule
|
162
|
-
resf += "-"*50
|
163
|
-
resf += "\n"
|
164
|
-
elsif rule.match?(/To run a command on demand and add the respond on a thread/i)
|
165
|
-
resf += rule
|
166
|
-
resf += "-"*50
|
167
|
-
resf += "\n"
|
168
|
-
else
|
169
|
-
rule.split("\n").each do |line|
|
170
|
-
if line.match?(/^\s*\-+\s*/i)
|
171
|
-
resf += line
|
172
|
-
elsif !command_done and line.match?(/^\s*`.+`\s*/i)
|
173
|
-
resf += "\n#{line}"
|
174
|
-
command_done = true
|
175
|
-
elsif !explanation_done and line.match?(/^\s+[^`].+\s*/i)
|
176
|
-
resf += "\n#{line}"
|
177
|
-
explanation_done = true
|
178
|
-
elsif !example_done and line.match?(/^\s*_.+_\s*/i)
|
179
|
-
resf += "\n Example: #{line}"
|
180
|
-
example_done = true
|
181
|
-
end
|
182
|
-
end
|
183
|
-
resf += "\n\n"
|
184
|
-
end
|
185
|
-
end
|
186
|
-
unless resf.match?(/These are specific commands for this bot on this Channel/i)
|
187
|
-
if resf.match?(/\A\s*[=\-]+$/)
|
188
|
-
pre = ''
|
189
|
-
post = ''
|
190
|
-
else
|
191
|
-
pre = ('='*50) + "\n"
|
192
|
-
post = ('-'*50) + "\n"
|
193
|
-
end
|
194
|
-
resf = "#{pre}*These are specific commands for this bot on this Channel:*\n#{post}" + resf
|
195
|
-
end
|
196
|
-
help.rules_file = resf
|
200
|
+
if !help.rules_file.to_s.include?('These are specific commands') and help.rules_file!=''
|
201
|
+
txt += "===================================
|
202
|
+
*Specific commands on this Channel, call them !THE_COMMAND or !!THE_COMMAND:*\n" if descriptions
|
197
203
|
end
|
204
|
+
|
198
205
|
txt += help.rules_file
|
199
206
|
end
|
200
|
-
|
201
207
|
return txt
|
202
208
|
end
|
203
209
|
end
|
@@ -1,11 +1,31 @@
|
|
1
1
|
class SlackSmartBot
|
2
2
|
|
3
3
|
def get_repls(channel = @channel_id)
|
4
|
-
|
4
|
+
require 'yaml'
|
5
|
+
repl_file = "#{config.path}/repl/repls_#{channel}.yaml"
|
6
|
+
|
7
|
+
if File.exist?("#{config.path}/repl/repls_#{channel}.rb") #backwards compatible
|
5
8
|
file_conf = IO.readlines("#{config.path}/repl/repls_#{channel}.rb").join
|
6
|
-
|
9
|
+
if file_conf.to_s() == ""
|
10
|
+
@repls = {}
|
11
|
+
else
|
7
12
|
@repls = eval(file_conf)
|
8
13
|
end
|
14
|
+
File.open(repl_file, 'w') {|file| file.write(@repls.to_yaml) }
|
15
|
+
File.delete("#{config.path}/repl/repls_#{channel}.rb")
|
16
|
+
end
|
17
|
+
|
18
|
+
if File.exist?(repl_file)
|
19
|
+
repls = @repls
|
20
|
+
10.times do
|
21
|
+
repls = YAML.load(File.read(repl_file))
|
22
|
+
if repls.is_a?(Hash)
|
23
|
+
break
|
24
|
+
else
|
25
|
+
sleep (0.1*(rand(2)+1))
|
26
|
+
end
|
27
|
+
end
|
28
|
+
@repls = repls unless repls.is_a?(FalseClass)
|
9
29
|
end
|
10
30
|
end
|
11
31
|
end
|