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.
Files changed (121) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +184 -16
  3. data/lib/slack/smart-bot/comm/ask.rb +55 -49
  4. data/lib/slack/smart-bot/comm/delete.rb +13 -0
  5. data/lib/slack/smart-bot/comm/dont_understand.rb +3 -3
  6. data/lib/slack/smart-bot/comm/event_hello.rb +8 -4
  7. data/lib/slack/smart-bot/comm/get_channel_members.rb +13 -4
  8. data/lib/slack/smart-bot/comm/get_channels.rb +31 -16
  9. data/lib/slack/smart-bot/comm/get_presence.rb +20 -0
  10. data/lib/slack/smart-bot/comm/get_user_info.rb +12 -8
  11. data/lib/slack/smart-bot/comm/get_users.rb +24 -0
  12. data/lib/slack/smart-bot/comm/react.rb +19 -2
  13. data/lib/slack/smart-bot/comm/respond.rb +224 -53
  14. data/lib/slack/smart-bot/comm/respond_direct.rb +2 -3
  15. data/lib/slack/smart-bot/comm/respond_thread.rb +5 -0
  16. data/lib/slack/smart-bot/comm/send_file.rb +38 -34
  17. data/lib/slack/smart-bot/comm/send_msg_channel.rb +27 -22
  18. data/lib/slack/smart-bot/comm/send_msg_user.rb +59 -33
  19. data/lib/slack/smart-bot/comm/unreact.rb +22 -18
  20. data/lib/slack/smart-bot/comm.rb +4 -0
  21. data/lib/slack/smart-bot/commands/general/add_admin.rb +51 -0
  22. data/lib/slack/smart-bot/commands/general/add_announcement.rb +32 -0
  23. data/lib/slack/smart-bot/commands/general/add_team.rb +80 -0
  24. data/lib/slack/smart-bot/commands/general/allow_access.rb +67 -0
  25. data/lib/slack/smart-bot/commands/general/bot_help.rb +69 -33
  26. data/lib/slack/smart-bot/commands/general/bye_bot.rb +0 -7
  27. data/lib/slack/smart-bot/commands/general/delete_announcement.rb +34 -0
  28. data/lib/slack/smart-bot/commands/general/delete_share.rb +34 -0
  29. data/lib/slack/smart-bot/commands/general/delete_team.rb +34 -0
  30. data/lib/slack/smart-bot/commands/general/deny_access.rb +36 -0
  31. data/lib/slack/smart-bot/commands/general/hi_bot.rb +16 -11
  32. data/lib/slack/smart-bot/commands/general/ping_team.rb +100 -0
  33. data/lib/slack/smart-bot/commands/general/poster.rb +116 -0
  34. data/lib/slack/smart-bot/commands/general/remove_admin.rb +58 -0
  35. data/lib/slack/smart-bot/commands/general/see_access.rb +24 -0
  36. data/lib/slack/smart-bot/commands/general/see_admins.rb +33 -0
  37. data/lib/slack/smart-bot/commands/general/see_announcements.rb +115 -0
  38. data/lib/slack/smart-bot/commands/general/see_command_ids.rb +29 -0
  39. data/lib/slack/smart-bot/commands/general/see_favorite_commands.rb +53 -0
  40. data/lib/slack/smart-bot/commands/general/see_shares.rb +41 -0
  41. data/lib/slack/smart-bot/commands/general/see_statuses.rb +91 -0
  42. data/lib/slack/smart-bot/commands/general/see_teams.rb +252 -0
  43. data/lib/slack/smart-bot/commands/general/share_messages.rb +58 -0
  44. data/lib/slack/smart-bot/commands/general/update_team.rb +109 -0
  45. data/lib/slack/smart-bot/commands/general_bot_commands.rb +504 -0
  46. data/lib/slack/smart-bot/commands/on_bot/add_shortcut.rb +4 -6
  47. data/lib/slack/smart-bot/commands/on_bot/admin/add_routine.rb +45 -14
  48. data/lib/slack/smart-bot/commands/on_bot/admin/extend_rules.rb +4 -1
  49. data/lib/slack/smart-bot/commands/on_bot/admin/pause_bot.rb +6 -3
  50. data/lib/slack/smart-bot/commands/on_bot/admin/pause_routine.rb +3 -1
  51. data/lib/slack/smart-bot/commands/on_bot/admin/remove_routine.rb +4 -4
  52. data/lib/slack/smart-bot/commands/on_bot/admin/run_routine.rb +8 -2
  53. data/lib/slack/smart-bot/commands/on_bot/admin/see_result_routine.rb +33 -0
  54. data/lib/slack/smart-bot/commands/on_bot/admin/see_routines.rb +13 -10
  55. data/lib/slack/smart-bot/commands/on_bot/admin/start_bot.rb +6 -3
  56. data/lib/slack/smart-bot/commands/on_bot/admin/start_routine.rb +3 -1
  57. data/lib/slack/smart-bot/commands/on_bot/admin/stop_using_rules_on.rb +4 -1
  58. data/lib/slack/smart-bot/commands/on_bot/admin_master/delete_message.rb +25 -0
  59. data/lib/slack/smart-bot/commands/on_bot/admin_master/get_bot_logs.rb +1 -0
  60. data/lib/slack/smart-bot/commands/on_bot/admin_master/react_to.rb +34 -0
  61. data/lib/slack/smart-bot/commands/on_bot/admin_master/send_message.rb +37 -0
  62. data/lib/slack/smart-bot/commands/on_bot/delete_repl.rb +4 -5
  63. data/lib/slack/smart-bot/commands/on_bot/delete_shortcut.rb +7 -8
  64. data/lib/slack/smart-bot/commands/on_bot/general/bot_stats.rb +400 -0
  65. data/lib/slack/smart-bot/commands/{general → on_bot/general}/bot_status.rb +3 -4
  66. data/lib/slack/smart-bot/commands/on_bot/general/leaderboard.rb +201 -0
  67. data/lib/slack/smart-bot/commands/{general → on_bot/general}/stop_using_rules.rb +12 -6
  68. data/lib/slack/smart-bot/commands/on_bot/general/suggest_command.rb +36 -0
  69. data/lib/slack/smart-bot/commands/{general → on_bot/general}/use_rules.rb +13 -11
  70. data/lib/slack/smart-bot/commands/{general → on_bot/general}/whats_new.rb +2 -1
  71. data/lib/slack/smart-bot/commands/on_bot/get_repl.rb +4 -5
  72. data/lib/slack/smart-bot/commands/on_bot/repl.rb +76 -21
  73. data/lib/slack/smart-bot/commands/on_bot/ruby_code.rb +3 -4
  74. data/lib/slack/smart-bot/commands/on_bot/run_repl.rb +15 -7
  75. data/lib/slack/smart-bot/commands/on_bot/see_repls.rb +5 -6
  76. data/lib/slack/smart-bot/commands/on_bot/see_shortcuts.rb +5 -6
  77. data/lib/slack/smart-bot/commands/on_extended/bot_rules.rb +45 -12
  78. data/lib/slack/smart-bot/commands/on_master/admin/kill_bot_on_channel.rb +7 -3
  79. data/lib/slack/smart-bot/commands/on_master/admin_master/exit_bot.rb +4 -1
  80. data/lib/slack/smart-bot/commands/on_master/admin_master/notify_message.rb +3 -1
  81. data/lib/slack/smart-bot/commands/on_master/admin_master/publish_announcements.rb +33 -0
  82. data/lib/slack/smart-bot/commands/on_master/admin_master/set_general_message.rb +39 -0
  83. data/lib/slack/smart-bot/commands/on_master/admin_master/set_maintenance.rb +10 -1
  84. data/lib/slack/smart-bot/commands/on_master/create_bot.rb +28 -14
  85. data/lib/slack/smart-bot/commands/on_master/where_smartbot.rb +41 -0
  86. data/lib/slack/smart-bot/commands.rb +36 -5
  87. data/lib/slack/smart-bot/listen.rb +31 -33
  88. data/lib/slack/smart-bot/process.rb +234 -73
  89. data/lib/slack/smart-bot/process_first.rb +119 -38
  90. data/lib/slack/smart-bot/treat_message.rb +310 -237
  91. data/lib/slack/smart-bot/utils/build_help.rb +2 -2
  92. data/lib/slack/smart-bot/utils/create_routine_thread.rb +81 -46
  93. data/lib/slack/smart-bot/utils/get_access_channels.rb +13 -0
  94. data/lib/slack/smart-bot/utils/get_admins_channels.rb +13 -0
  95. data/lib/slack/smart-bot/utils/get_bots_created.rb +28 -8
  96. data/lib/slack/smart-bot/utils/get_channels_name_and_id.rb +7 -2
  97. data/lib/slack/smart-bot/utils/get_command_ids.rb +84 -0
  98. data/lib/slack/smart-bot/utils/get_help.rb +79 -73
  99. data/lib/slack/smart-bot/utils/get_repls.rb +22 -2
  100. data/lib/slack/smart-bot/utils/get_routines.rb +22 -2
  101. data/lib/slack/smart-bot/utils/get_shares.rb +12 -0
  102. data/lib/slack/smart-bot/utils/get_teams.rb +22 -0
  103. data/lib/slack/smart-bot/utils/has_access.rb +28 -0
  104. data/lib/slack/smart-bot/utils/is_admin.rb +27 -0
  105. data/lib/slack/smart-bot/utils/save_stats.rb +46 -41
  106. data/lib/slack/smart-bot/utils/save_status.rb +67 -0
  107. data/lib/slack/smart-bot/utils/update_access_channels.rb +8 -0
  108. data/lib/slack/smart-bot/utils/update_admins_channels.rb +8 -0
  109. data/lib/slack/smart-bot/utils/update_bots_file.rb +28 -7
  110. data/lib/slack/smart-bot/utils/update_repls.rb +7 -4
  111. data/lib/slack/smart-bot/utils/update_routines.rb +9 -3
  112. data/lib/slack/smart-bot/utils/update_shortcuts_file.rb +13 -6
  113. data/lib/slack/smart-bot/utils/update_teams.rb +16 -0
  114. data/lib/slack/smart-bot/utils.rb +11 -0
  115. data/lib/slack-smart-bot.rb +72 -12
  116. data/lib/slack-smart-bot_general_commands.rb +61 -0
  117. data/lib/slack-smart-bot_general_rules.rb +5 -2
  118. data/lib/slack-smart-bot_rules.rb +43 -17
  119. data/whats_new.txt +20 -15
  120. metadata +76 -9
  121. data/lib/slack/smart-bot/commands/general/bot_stats.rb +0 -313
@@ -3,27 +3,31 @@ class SlackSmartBot
3
3
  # unreact(:thumbsup)
4
4
  # ts: can be true, false or a specific ts
5
5
  def unreact(emoji, ts=false)
6
- if ts.is_a?(TrueClass) or ts.is_a?(FalseClass)
7
- parent = ts
8
- ts = nil
9
- else
10
- parent = false
11
- end
12
- if ts.nil?
13
- if parent or Thread.current[:ts].to_s == ''
14
- ts = Thread.current[:thread_ts]
6
+ begin
7
+ if ts.is_a?(TrueClass) or ts.is_a?(FalseClass)
8
+ parent = ts
9
+ ts = nil
15
10
  else
16
- ts = Thread.current[:ts]
11
+ parent = false
17
12
  end
18
- end
19
- if ts.nil?
20
- @logger.warn 'unreact method no ts supplied'
21
- else
22
- begin
23
- client.web_client.reactions_remove(channel: Thread.current[:dest], name: emoji, timestamp: ts) unless config.simulate
24
- rescue Exception => stack
25
- @logger.warn stack
13
+ if ts.nil?
14
+ if parent or Thread.current[:ts].to_s == ''
15
+ ts = Thread.current[:thread_ts]
16
+ else
17
+ ts = Thread.current[:ts]
18
+ end
19
+ end
20
+ if ts.nil?
21
+ @logger.warn 'unreact method no ts supplied'
22
+ else
23
+ begin
24
+ client.web_client.reactions_remove(channel: Thread.current[:dest], name: emoji, timestamp: ts) unless config.simulate
25
+ rescue Exception => stack
26
+ @logger.warn stack
27
+ end
26
28
  end
29
+ rescue Exception => stack
30
+ @logger.warn stack
27
31
  end
28
32
  end
29
33
  end
@@ -1,5 +1,6 @@
1
1
  require_relative 'comm/ask'
2
2
  require_relative 'comm/dont_understand'
3
+ require_relative 'comm/respond_thread'
3
4
  require_relative 'comm/respond_direct'
4
5
  require_relative 'comm/respond'
5
6
  require_relative 'comm/send_file'
@@ -8,6 +9,9 @@ require_relative 'comm/send_msg_user'
8
9
  require_relative 'comm/react'
9
10
  require_relative 'comm/unreact'
10
11
  require_relative 'comm/get_user_info'
12
+ require_relative 'comm/get_users'
11
13
  require_relative 'comm/event_hello'
12
14
  require_relative 'comm/get_channel_members'
13
15
  require_relative 'comm/get_channels'
16
+ require_relative 'comm/delete'
17
+ require_relative 'comm/get_presence'
@@ -0,0 +1,51 @@
1
+ class SlackSmartBot
2
+ def add_admin(user, admin_user)
3
+ save_stats(__method__)
4
+ if Thread.current[:dest][0]=='D'
5
+ respond "This command cannot be called from a DM"
6
+ else
7
+ if Thread.current[:typem] == :on_call
8
+ channel = Thread.current[:dchannel]
9
+ elsif Thread.current[:using_channel].to_s==''
10
+ channel = Thread.current[:dest]
11
+ else
12
+ channel = Thread.current[:using_channel]
13
+ end
14
+ messages = []
15
+ admins = config.masters.dup
16
+ channels = get_channels()
17
+ channel_found = channels.detect { |c| c.id == channel }
18
+ if !channel_found.nil? and channel_found.creator.to_s != ''
19
+ creator_info = @users.select{|u| u.id == channel_found.creator or (u.key?(:enterprise_user) and u.enterprise_user.id == channel_found.creator)}[-1]
20
+ admins << creator_info.name
21
+ end
22
+ if Thread.current[:typem] == :on_bot or Thread.current[:typem] == :on_master
23
+ admins << config.admins.dup
24
+ end
25
+ if @admins_channels.key?(channel) and @admins_channels[channel].size > 0
26
+ admins << @admins_channels[channel]
27
+ end
28
+ admins.flatten!
29
+ admins.uniq!
30
+ admins.delete(nil)
31
+ if admins.include?(user.name)
32
+ admin_info = @users.select{|u| u.id == admin_user or (u.key?(:enterprise_user) and u.enterprise_user.id == admin_user)}[-1]
33
+ if admins.include?(admin_info.name)
34
+ messages << "This user is already an admin of this channel."
35
+ else
36
+ @admins_channels[channel] ||= []
37
+ @admins_channels[channel] << admin_info.name
38
+ update_admins_channels()
39
+ messages << "The user is an admin of this channel from now on."
40
+ admins << admin_info.name
41
+ end
42
+ messages << "*Admins*: <@#{admins.join('>, <@')}>"
43
+ else
44
+ messages << "Only the creator of the channel, Master admins or admins can add a new admin for this channel."
45
+ messages << "*Admins*: <@#{admins.join('>, <@')}>"
46
+ end
47
+
48
+ respond messages.join("\n")
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,32 @@
1
+ class SlackSmartBot
2
+
3
+ def add_announcement(user, type, message)
4
+ save_stats(__method__)
5
+ if has_access?(__method__, user)
6
+ if Thread.current[:typem] == :on_call
7
+ channel = Thread.current[:dchannel]
8
+ else
9
+ channel = Thread.current[:dest]
10
+ end
11
+ if File.exist?("#{config.path}/announcements/#{channel}.csv") and !@announcements.key?(channel)
12
+ t = CSV.table("#{config.path}/announcements/#{channel}.csv", headers: ['message_id', 'user_deleted', 'user_created', 'date', 'time', 'type', 'message'])
13
+ @announcements[channel] = t
14
+ num = t[:message_id].max + 1
15
+ elsif !@announcements.key?(channel)
16
+ File.open("#{config.path}/announcements/#{channel}.csv","w")
17
+ t = CSV.table("#{config.path}/announcements/#{channel}.csv", headers: ['message_id', 'user_deleted', 'user_created', 'date', 'time', 'type', 'message'])
18
+ num = 1
19
+ @announcements[channel] = t
20
+ else
21
+ num = @announcements[channel][:message_id].max + 1
22
+ end
23
+ values = [num, '', user.name, Time.now.strftime("%Y/%m/%d"), Time.now.strftime("%H:%M"), type, message]
24
+ @announcements[channel] << values
25
+ CSV.open("#{config.path}/announcements/#{channel}.csv", "a+") do |csv|
26
+ csv << values
27
+ end
28
+ respond "The announcement has been added. (id: #{num}).\nRelated commands `see announcements`, `delete announcement ID`"
29
+
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,80 @@
1
+ class SlackSmartBot
2
+ def add_team(user, name, options, info)
3
+ save_stats(__method__)
4
+
5
+ get_teams()
6
+ if @teams.key?(name.to_sym)
7
+ respond "It seems like the team *#{name}* already exists.\nRelated commands `update team TEAM_NAME PROPERTIES`, `delete team TEAM_NAME`, `see team TEAM_NAME`, `see teams`"
8
+ else
9
+ wrong = false
10
+ team = { members: {}, channels: {} }
11
+ last_type = nil
12
+ type_detected = false
13
+ options.split(/\s+/).each do |opt|
14
+ type_detected = false
15
+ if opt.match?(/^\s*$/)
16
+ #blank
17
+ elsif opt.match?(/^[\w\-]+$/i)
18
+ last_type = opt
19
+ type_detected = true
20
+ elsif opt.match(/<@(\w+)>/i)
21
+ team[:members][last_type] ||= []
22
+ if last_type.nil?
23
+ wrong = true
24
+ respond "You need to specify the TYPE for the member."
25
+ break
26
+ else
27
+ member_id = $1
28
+ member_info = @users.select { |u| u.id == member_id or (u.key?(:enterprise_user) and u.enterprise_user.id == member_id) }[-1]
29
+ if member_info.nil?
30
+ @users = get_users()
31
+ member_info = @users.select { |u| u.id == member_id or (u.key?(:enterprise_user) and u.enterprise_user.id == member_id) }[-1]
32
+ end
33
+ team[:members][last_type] << member_info.name
34
+ end
35
+ elsif opt.match(/<#(\w+)\|[^>]*>/i)
36
+ team[:channels][last_type] ||= []
37
+ if last_type.nil?
38
+ wrong = true
39
+ respond "You need to specify the TYPE for the channel."
40
+ break
41
+ else
42
+ channel_id = $1
43
+ get_channels_name_and_id() unless @channels_name.keys.include?(channel_id)
44
+ channel = @channels_name[channel_id]
45
+ channel_members = get_channel_members(channel_id) unless channel.nil?
46
+ if channel.nil? or !channel_members.include?(config.nick_id)
47
+ respond ":exclamation: Add the Smart Bot to *<##{channel_id}>* channel first."
48
+ wrong = true
49
+ break
50
+ else
51
+ team[:channels][last_type] << channel
52
+ end
53
+ end
54
+ else
55
+ respond "It seems like the members or channel list is not correct. Please double check."
56
+ wrong = true
57
+ break
58
+ end
59
+ end
60
+ if type_detected #type added but not added a channel or user
61
+ respond "It seems like the parameters supplied are not correct. Please double check."
62
+ wrong = true
63
+ end
64
+
65
+ unless wrong
66
+ get_teams()
67
+ team[:info] = info
68
+ team[:status] = :added
69
+ team[:user] = user.name
70
+ team[:creator] = user.name
71
+ team[:date] = Time.now.strftime("%Y-%m-%dT%H:%M:%S.000Z")[0..18]
72
+ new_team = {}
73
+ new_team[name.to_sym] = team
74
+ update_teams(new_team)
75
+ respond "The *#{name}* team has been added."
76
+ see_teams(user, name, add_stats: false)
77
+ end
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,67 @@
1
+ class SlackSmartBot
2
+ def allow_access(user, command_id, opt)
3
+ save_stats(__method__)
4
+ not_allowed = ["hi_bot", "bye_bot", "allow_access", "deny_access", "get_bot_logs", "add_routine", "pause_bot", "pause_routine", "remove_routine", "run_routine", "start_bot",
5
+ "start_routine", "delete_message", "send_message", "kill_bot_on_channel", "exit_bot", "notify_message", "publish_announcements", "set_general_message",
6
+ "set_maintenance", "bot_help", "bot_rules"]
7
+ if !is_admin?(user.name)
8
+ respond "Only admins of this channel can use this command. Take a look who is an admin of this channel by calling `see admins`"
9
+ elsif Thread.current[:dest][0] == "D"
10
+ respond "This command cannot be called from a DM"
11
+ elsif not_allowed.include?(command_id)
12
+ respond "Sorry but the access for `#{command_id}` cannot be changed."
13
+ else
14
+ if Thread.current[:typem] == :on_call
15
+ channel = Thread.current[:dchannel]
16
+ elsif Thread.current[:using_channel].to_s == ""
17
+ channel = Thread.current[:dest]
18
+ else
19
+ channel = Thread.current[:using_channel]
20
+ end
21
+ command_ids = get_command_ids()
22
+ if command_ids.values.flatten.include?(command_id)
23
+ wrong_user = false
24
+ access_users = []
25
+ opt.each do |o|
26
+ if o.match(/\A\s*<@([^>]+)>\s*\z/)
27
+ access_users << $1
28
+ else
29
+ respond "Hmm, I've done some research on this and it looks like #{o} is not a valid Slack user.\nMake sure you are writing @USER and it is recognized by *Slack*\n"
30
+ wrong_user = true
31
+ break
32
+ end
33
+ end
34
+ unless wrong_user
35
+ if !@access_channels.key?(channel)
36
+ @access_channels[channel] = {}
37
+ end
38
+
39
+ if access_users.empty? # all users will be able to access
40
+ @access_channels[channel].delete(command_id)
41
+ else
42
+ if @access_channels.key?(channel) and !@access_channels[channel].key?(command_id)
43
+ @access_channels[channel][command_id] = []
44
+ end
45
+ access_users_names = []
46
+ access_users.each do |us|
47
+ user_info = @users.select { |u| u.id == us or (u.key?(:enterprise_user) and u.enterprise_user.id == us) }[-1]
48
+ access_users_names << user_info.name unless user_info.nil?
49
+ end
50
+ @access_channels[channel][command_id] += access_users_names
51
+ @access_channels[channel][command_id].flatten!
52
+ @access_channels[channel][command_id].uniq!
53
+ @access_channels[channel][command_id].delete(nil)
54
+ end
55
+ update_access_channels()
56
+ if !@access_channels[channel].key?(command_id)
57
+ respond "All users will have access to this command on this channel."
58
+ else
59
+ respond "These users will have access to this command on this channel: <@#{@access_channels[channel][command_id].join(">, <@")}>"
60
+ end
61
+ end
62
+ else
63
+ respond "It seems like #{command_id} is not valid. Please be sure that exists by calling `see command ids`"
64
+ end
65
+ end
66
+ end
67
+ end
@@ -1,24 +1,9 @@
1
1
  class SlackSmartBot
2
2
 
3
- # help: ----------------------------------------------
4
- # help: `bot help`
5
- # help: `bot help COMMAND`
6
- # help: `bot rules`
7
- # help: `bot rules COMMAND`
8
- # help: `bot help expanded`
9
- # help: `bot rules expanded`
10
- # help: `bot what can I do?`
11
- # help: it will display this help. For a more detailed help call `bot help expanded` or `bot rules expanded`.
12
- # help: if COMMAND supplied just help for that command
13
- # help: you can use the option 'expanded' or the alias 'extended'
14
- # help: `bot rules` will show only the specific rules for this channel.
15
- # help:
16
- def bot_help(user, from, dest, dchannel, specific, help_command, rules_file)
17
- save_stats(__method__)
18
- if config[:allow_access].key?(__method__) and !config[:allow_access][__method__].include?(user.name) and !config[:allow_access][__method__].include?(user.id) and
19
- (!user.key?(:enterprise_user) or ( user.key?(:enterprise_user) and !config[:allow_access][__method__].include?(user[:enterprise_user].id)))
20
- respond "You don't have access to use this command, please contact an Admin to be able to use it: <@#{config.admins.join(">, <@")}>"
21
- else
3
+ def bot_help(user, from, dest, dchannel, specific, help_command, rules_file, savestats: true, strict: false)
4
+ save_stats(__method__) if savestats
5
+ output = []
6
+ if has_access?(__method__, user)
22
7
  help_found = false
23
8
 
24
9
  message = ""
@@ -28,36 +13,74 @@ class SlackSmartBot
28
13
  message_not_expanded = ''
29
14
  else
30
15
  expanded = false
31
- message_not_expanded = "*If you want to see the expanded version of `bot help` or `bot rules`, please call `bot help expanded` or `bot rules expanded`*\n"
32
- message_not_expanded += "*Also to get specific expanded help for a specific command or rule call `bot help COMMAND`*\n"
16
+ message_not_expanded = "If you want to see the *expanded* version of *`bot help`* or *`bot rules`*, please call *`bot help expanded`* or *`bot rules expanded`*\n"
17
+ message_not_expanded += "Also to get specific *expanded* help for a specific command or rule call *`bot help COMMAND`*\n"
33
18
  end
34
-
35
19
  help_message = get_help(rules_file, dest, from, specific, expanded)
36
-
20
+ commands = []
21
+ commands_search = []
37
22
  if help_command.to_s != ""
38
23
  help_message.gsub(/====+/,'-'*30).split(/^\s*-------*$/).each do |h|
39
- if h.match?(/[`_]#{help_command}/i)
40
- respond h, dest
41
- help_found = true
24
+ if strict
25
+ if h.match?(/`#{help_command}`/i) or h.match?(/^\s*command_id:\s+:#{help_command.gsub(' ', '_')}\s*$/)
26
+ output << h
27
+ help_found = true
28
+ commands << h
29
+ break
30
+ end
31
+ else
32
+ if h.match?(/[`_]#{help_command}/i) or h.match?(/^\s*command_id:\s+:#{help_command.gsub(' ', '_')}\s*$/)
33
+ output << h
34
+ help_found = true
35
+ commands << h
36
+ elsif !h.match?(/\A\s*\*/) and !h.match?(/\A\s*=+/) #to avoid general messages for bot help *General commands...*
37
+ all_found = true
38
+ help_command.to_s.split(' ') do |hc|
39
+ unless hc.match?(/^\s*\z/)
40
+ if !h.match?(/#{hc}/i)
41
+ all_found = false
42
+ end
43
+ end
44
+ end
45
+ end
46
+ commands_search << h if all_found
42
47
  end
43
48
  end
44
49
  else
45
50
  if Thread.current[:using_channel]!=''
46
51
  message += "*You are using rules from another channel: <##{Thread.current[:using_channel]}>. These are the specific commands for that channel:*"
47
52
  end
48
- respond message, dest
53
+ output << message
49
54
  end
50
55
 
51
56
  if (help_command.to_s == "")
52
57
  help_message.split(/^\s*=========*$/).each do |h|
53
- respond("#{"=" * 35}\n#{h}", dest) unless h.match?(/\A\s*\z/)
58
+ unless h.match?(/\A\s*\z/)
59
+ output << "#{"=" * 35}\n#{h}"
60
+ end
61
+ end
62
+ if Thread.current[:typem] == :on_pg or Thread.current[:typem] == :on_pub
63
+ if @bots_created.size>0
64
+ txt = "\nThese are the *SmartBots* running on this Slack workspace: *<##{@master_bot_id}>, <##{@bots_created.keys.join('>, <#')}>*\n"
65
+ txt += "Join one channel and call *`bot rules`* to see specific commands for that channel or *`bot help`* to see all commands for that channel.\n"
66
+ output << txt
67
+ end
54
68
  end
55
69
  else
70
+ if commands.size < 10 and help_command.to_s!='' and commands_search.size > 0
71
+ commands_search.shuffle!
72
+ (10-commands.size).times do |n|
73
+ unless commands_search[n].nil?
74
+ output << commands_search[n]
75
+ help_found = true
76
+ end
77
+ end
78
+ end
56
79
  unless help_found
57
80
  if specific
58
- respond("I didn't find any rule starting by `#{help_command}`", dest)
81
+ output << "I didn't find any rule with `#{help_command}`"
59
82
  else
60
- respond("I didn't find any command starting by `#{help_command}`", dest)
83
+ output << "I didn't find any command with `#{help_command}`"
61
84
  end
62
85
  end
63
86
  end
@@ -69,7 +92,7 @@ class SlackSmartBot
69
92
  end
70
93
  end
71
94
  if defined?(git_project) && (git_project.to_s != "") && (help_command.to_s == "")
72
- respond "Git project: #{git_project}", dest
95
+ output << "Git project: #{git_project}"
73
96
  else
74
97
  def git_project
75
98
  ""
@@ -80,9 +103,22 @@ class SlackSmartBot
80
103
  end
81
104
  end
82
105
  elsif help_command.to_s == ""
83
- respond "Slack Smart Bot Github project: https://github.com/MarioRuiz/slack-smart-bot", dest
106
+ output << "Slack Smart Bot Github project: https://github.com/MarioRuiz/slack-smart-bot"
107
+ end
108
+ unless expanded
109
+ output << message_not_expanded
110
+ end
111
+ end
112
+ if output.join("\n").lines.count > 50 and dest[0]!='D'
113
+ dest = :on_thread
114
+ output.unshift('Since there are many lines returned the results are returned on a thread by default.')
115
+ end
116
+ output.each do |h|
117
+ msg = h.gsub(/^\s*command_id:\s+:\w+\s*$/,'')
118
+ unless msg.match?(/\A\s*\z/)
119
+ respond msg, dest, unfurl_links: false, unfurl_media: false
84
120
  end
85
- respond message_not_expanded unless expanded
86
121
  end
122
+ return output.join("\n")
87
123
  end
88
124
  end
@@ -1,12 +1,5 @@
1
1
  class SlackSmartBot
2
2
 
3
- # help: ----------------------------------------------
4
- # help: `Bye Bot`
5
- # help: `Bye Smart`
6
- # help: `Bye NAME_OF_THE_BOT`
7
- # help: Bot stops listening to you
8
- # help: Also apart of Bye you can use _Bæ, Good Bye, Adiós, Ciao, Bless, Bless Bless, Adeu_
9
- # help:
10
3
  def bye_bot(dest, from, display_name)
11
4
  if @status == :on
12
5
  save_stats(__method__)
@@ -0,0 +1,34 @@
1
+ class SlackSmartBot
2
+
3
+ def delete_announcement(user, message_id)
4
+ save_stats(__method__)
5
+ if has_access?(__method__, user)
6
+ if Thread.current[:typem] == :on_call
7
+ channel = Thread.current[:dchannel]
8
+ else
9
+ channel = Thread.current[:dest]
10
+ end
11
+ if File.exist?("#{config.path}/announcements/#{channel}.csv") and !@announcements.key?(channel)
12
+ t = CSV.table("#{config.path}/announcements/#{channel}.csv", headers: ['message_id', 'user_deleted', 'user_created', 'date', 'time', 'type', 'message'])
13
+ @announcements[channel] = t
14
+ end
15
+ found = false
16
+ message = ''
17
+ if @announcements.key?(channel) and @announcements[channel][:message_id].include?(message_id.to_i)
18
+ CSV.open("#{config.path}/announcements/#{channel}.csv", "w") do |csv|
19
+ @announcements[channel].each do |row|
20
+ if row[:message_id].to_i == message_id.to_i
21
+ message = row[:message]
22
+ row[:user_deleted] = user.name
23
+ end
24
+ csv << row
25
+ end
26
+ end
27
+ respond "The announcement has been deleted: #{message}"
28
+ else
29
+ respond "Sorry but I didn't find the message id #{message_id}. Call `see announcements` to see the ids."
30
+ end
31
+
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,34 @@
1
+ class SlackSmartBot
2
+
3
+ def delete_share(user, share_id)
4
+ save_stats(__method__)
5
+ if has_access?(__method__, user)
6
+ if Thread.current[:typem] == :on_call
7
+ channel = Thread.current[:dchannel]
8
+ else
9
+ channel = Thread.current[:dest]
10
+ end
11
+ if File.exist?("#{config.path}/shares/#{@channels_name[channel]}.csv") and !@shares.key?(@channels_name[channel])
12
+ t = CSV.table("#{config.path}/shares/#{@channels_name[channel]}.csv", headers: ['share_id', 'user_deleted', 'user_created', 'date', 'time', 'type', 'to_channel', 'condition'])
13
+ @shares[@channels_name[channel]] = t
14
+ end
15
+ found = false
16
+ message = ''
17
+ if @shares[@channels_name[channel]][:share_id].include?(share_id.to_i)
18
+ CSV.open("#{config.path}/shares/#{@channels_name[channel]}.csv", "w") do |csv|
19
+ @shares[@channels_name[channel]].each do |row|
20
+ if row[:share_id].to_i == share_id.to_i
21
+ message = row[:condition]
22
+ row[:user_deleted] = user.name
23
+ end
24
+ csv << row
25
+ end
26
+ end
27
+ respond "The share has been deleted: #{message}"
28
+ else
29
+ respond "Sorry but I didn't find the share id #{share_id}. Call `see shares` to see all the ids."
30
+ end
31
+
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,34 @@
1
+ class SlackSmartBot
2
+ def delete_team(user, team_name)
3
+ save_stats(__method__) if answer.empty?
4
+
5
+ if Thread.current[:dest][0] == "D"
6
+ respond "This command cannot be called from a DM"
7
+ else
8
+ get_teams()
9
+ if !@teams.key?(team_name.to_sym)
10
+ respond "It seems like the team *#{team_name}* doesn't exist.\nRelated commands `add team TEAM_NAME PROPERTIES`, `see team TEAM_NAME`, `see teams`"
11
+ elsif !(@teams[team_name.to_sym].members.values + [@teams[team_name.to_sym].creator] + config.masters).flatten.include?(user.name)
12
+ respond "You have to be a member of the team, the creator or a Master admin to be able to delete this team."
13
+ else
14
+ if answer.empty?
15
+ ask "do you really want to delete the #{team_name} team? (yes/no)"
16
+ else
17
+ case answer
18
+ when /yes/i, /yep/i, /sure/i
19
+ answer_delete
20
+ @teams.delete(team_name.to_sym)
21
+ update_teams()
22
+ respond "The team #{team_name} has been deleted."
23
+ when /no/i, /nope/i, /cancel/i
24
+ answer_delete
25
+ respond "Ok, the team was not deleted"
26
+ else
27
+ respond "I don't understand"
28
+ ask "do you really want to delete the #{team_name} team? (yes/no)"
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,36 @@
1
+ class SlackSmartBot
2
+ def deny_access(user, command_id)
3
+ save_stats(__method__)
4
+ not_allowed = ['hi_bot', 'bye_bot', "allow_access", "deny_access", "get_bot_logs", "add_routine", "pause_bot", "pause_routine", "remove_routine", "run_routine", "start_bot",
5
+ "start_routine", "delete_message", "send_message", "kill_bot_on_channel", "exit_bot", "notify_message", "publish_announcements", "set_general_message",
6
+ "set_maintenance", 'bot_help', 'bot_rules']
7
+ if !is_admin?(user.name)
8
+ respond "Only admins of this channel can use this command. Take a look who is an admin of this channel by calling `see admins`"
9
+ elsif Thread.current[:dest][0] == "D"
10
+ respond "This command cannot be called from a DM"
11
+ elsif not_allowed.include?(command_id)
12
+ respond "Sorry but the access for `#{command_id}` cannot be changed."
13
+ else
14
+ if Thread.current[:typem] == :on_call
15
+ channel = Thread.current[:dchannel]
16
+ elsif Thread.current[:using_channel].to_s == ""
17
+ channel = Thread.current[:dest]
18
+ else
19
+ channel = Thread.current[:using_channel]
20
+ end
21
+ command_ids = get_command_ids()
22
+ if command_ids.values.flatten.include?(command_id)
23
+ if !@access_channels.key?(channel)
24
+ @access_channels[channel] = {}
25
+ end
26
+
27
+ @access_channels[channel][command_id] = []
28
+
29
+ update_access_channels()
30
+ respond "The command `#{command_id}` won't be available in this channel. Call `allow access #{command_id}` if you want it back."
31
+ else
32
+ respond "It seems like #{command_id} is not valid. Please be sure that exists by calling `see command ids`"
33
+ end
34
+ end
35
+ end
36
+ end
@@ -1,21 +1,26 @@
1
1
  class SlackSmartBot
2
2
 
3
- # help: ----------------------------------------------
4
- # help: `Hi Bot`
5
- # help: `Hi Smart`
6
- # help: `Hello Bot` `Hola Bot` `Hallo Bot` `What's up Bot` `Hey Bot` `Hæ Bot`
7
- # help: `Hello THE_NAME_OF_THE_BOT`
8
- # help: Bot starts listening to you
9
- # help: After that if you want to avoid a single message to be treated by the smart bot, start the message by -
10
- # help: Also apart of Hello you can use _Hallo, Hi, Hola, What's up, Hey, Hæ_
11
- # help:
12
- def hi_bot(user, dest, dchannel, from, display_name)
3
+ def hi_bot(user, dest, from, display_name)
13
4
  if @status == :on
14
5
  save_stats(__method__)
15
6
  greetings = ["Hello", "Hallo", "Hi", "Hola", "What's up", "Hey", "Hæ"].sample
16
7
  respond "#{greetings} #{display_name}", dest
8
+ if Thread.current[:typem] == :on_pub or Thread.current[:typem] == :on_pg
9
+ respond "You are on a channel where the SmartBot is just a member. You can only run general bot commands.\nCall `bot help` to see the commands you can use."
10
+ elsif Thread.current[:typem] == :on_extended
11
+ message = ["You are on an extended channel from <##{@channel_id}> so you can use all specific commands from that channel just adding !, !! or ^ before the command."]
12
+ message << "Call `bot help` to see the commands you can use."
13
+ respond message.join("\n")
14
+ elsif Thread.current[:typem] == :on_dm and Thread.current[:using_channel] == ''
15
+ respond "To start using the rules from a Bot channel call `use #CHANNEL`.\nAvailable SmartBots: <##{@bots_created.keys.join('>, <#')}>\nIf you want to call just one command from a specific channel: `#CHANNEL COMMAND`"
16
+ else
17
+ respond "You are on <##{@channel_id}> SmartBot channel. Call `bot help` to see all commands you can use or `bot rules` just to see the specific commands for this Bot channel."
18
+ end
17
19
  if Thread.current[:using_channel]!=''
18
- respond "You are using specific rules for channel: <##{Thread.current[:using_channel]}>", dest
20
+ message = ["You are using rules from <##{Thread.current[:using_channel]}>"]
21
+ message << "If you want to change bot channel rules call `use #CHANNEL` or `stop using rules from <##{Thread.current[:using_channel]}>` to stop using rules from this channel."
22
+ message << "You can call a command from any other channel by calling `#CHANNEL COMMAND`" if Thread.current[:typem] == :on_dm
23
+ respond message.join("\n")
19
24
  end
20
25
  @listening[from] = {} unless @listening.key?(from)
21
26
  if Thread.current[:on_thread]