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
@@ -0,0 +1,24 @@
1
+ class SlackSmartBot
2
+
3
+ def get_users()
4
+ begin
5
+ users = []
6
+ cursor = nil
7
+ if config.simulate
8
+ users = client.web_client.users_list
9
+ else
10
+ begin
11
+ resp = client.web_client.users_list(limit: 1000, cursor: cursor)
12
+ if resp.key?(:members) and resp[:members].is_a?(Array) and resp[:members].size > 0
13
+ users << resp[:members]
14
+ end
15
+ cursor = resp.get_values(:next_cursor).values[-1]
16
+ end until cursor.empty?
17
+ users.flatten!
18
+ end
19
+ return users
20
+ rescue Exception => stack
21
+ @logger.warn stack
22
+ end
23
+ end
24
+ end
@@ -2,7 +2,9 @@ class SlackSmartBot
2
2
  # list of available emojis: https://www.webfx.com/tools/emoji-cheat-sheet/
3
3
  # react(:thumbsup)
4
4
  # ts: can be true, false or a specific ts
5
- def react(emoji, ts=false)
5
+ def react(emoji, ts=false, channel='')
6
+ result = true
7
+ channel = Thread.current[:dest] if channel == ''
6
8
  if ts.is_a?(TrueClass) or ts.is_a?(FalseClass)
7
9
  parent = ts
8
10
  ts = nil
@@ -15,15 +17,30 @@ class SlackSmartBot
15
17
  else
16
18
  ts = Thread.current[:ts]
17
19
  end
20
+ else
21
+ if ts.to_s.match?(/^\d+\.\d+$/)
22
+ #thread id
23
+ elsif ts.to_s.match?(/^p\d\d\d\d\d+$/)
24
+ #a thread id taken from url fex: p1622549264010700
25
+ ts = ts.scan(/(\d+)/).join
26
+ ts = "#{ts[0..9]}.#{ts[10..-1]}"
27
+ else
28
+ ts = Thread.current[:thread_ts] if ts == ''
29
+ end
30
+
18
31
  end
19
32
  if ts.nil?
20
33
  @logger.warn 'react method no ts supplied'
34
+ result = false
21
35
  else
36
+ emoji.gsub!(':','') if emoji.is_a?(String)
22
37
  begin
23
- client.web_client.reactions_add(channel: Thread.current[:dest], name: emoji, timestamp: ts) unless config.simulate
38
+ client.web_client.reactions_add(channel: channel, name: emoji.to_sym, timestamp: ts) unless config.simulate
24
39
  rescue Exception => stack
25
40
  @logger.warn stack
41
+ result = false
26
42
  end
27
43
  end
44
+ return result
28
45
  end
29
46
  end
@@ -1,64 +1,235 @@
1
1
  class SlackSmartBot
2
- def respond(msg, dest = nil)
3
- if dest.nil? and Thread.current.key?(:dest)
4
- dest = Thread.current[:dest]
5
- end
6
- dest = @channels_id[dest] if @channels_id.key?(dest) #it is a name of channel
7
- if !config.simulate #https://api.slack.com/docs/rate-limits
8
- msg.to_s.size > 500 ? wait = 0.5 : wait = 0.1
9
- sleep wait if Time.now <= (@last_respond+wait)
10
- end
11
- if dest.nil?
12
- if config[:simulate]
13
- open("#{config.path}/buffer_complete.log", "a") { |f|
14
- f.puts "|#{@channel_id}|#{config[:nick_id]}|#{config[:nick]}|#{msg}~~~"
15
- }
16
- else
17
- if Thread.current[:on_thread]
18
- client.message(channel: @channel_id, text: msg, as_user: true, thread_ts: Thread.current[:thread_ts])
2
+ def respond(msg = "", dest = nil, unfurl_links: true, unfurl_media: true, thread_ts: "", web_client: true, blocks: [], dont_share: false, return_message: false, max_chars_per_message: 4000)
3
+ result = true
4
+ resp = nil
5
+ if (msg.to_s != "" or !msg.to_s.match?(/^A\s*\z/) or !blocks.empty?) and Thread.current[:routine_type].to_s != "bgroutine"
6
+ if !web_client.is_a?(TrueClass) and !web_client.is_a?(FalseClass)
7
+ (!unfurl_links or !unfurl_media) ? web_client = true : web_client = false
8
+ end
9
+ begin
10
+ msg = msg.to_s
11
+ web_client = true if !blocks.empty?
12
+
13
+ on_thread = Thread.current[:on_thread]
14
+ if dest.nil? and Thread.current.key?(:dest)
15
+ dest = Thread.current[:dest]
16
+ elsif dest.is_a?(Symbol) and dest == :on_thread
17
+ on_thread = true
18
+ dest = Thread.current[:dest]
19
+ elsif dest.is_a?(Symbol) and dest == :direct
20
+ dest = Thread.current[:user].id
21
+ end
22
+ if thread_ts.to_s.match?(/^\d+\.\d+$/)
23
+ on_thread = true
24
+ #thread id
25
+ elsif thread_ts.to_s.match?(/^p\d\d\d\d\d+$/)
26
+ on_thread = true
27
+ #a thread id taken from url fex: p1622549264010700
28
+ thread_ts = thread_ts.scan(/(\d+)/).join
29
+ thread_ts = "#{thread_ts[0..9]}.#{thread_ts[10..-1]}"
19
30
  else
20
- client.message(channel: @channel_id, text: msg, as_user: true)
31
+ thread_ts = Thread.current[:thread_ts] if thread_ts == ""
21
32
  end
22
- end
23
- if config[:testing] and config.on_master_bot
24
- open("#{config.path}/buffer.log", "a") { |f|
25
- f.puts "|#{@channel_id}|#{config[:nick_id]}|#{config[:nick]}|#{msg}"
26
- }
27
- end
28
- elsif dest[0] == "C" or dest[0] == "G" # channel
29
- if config[:simulate]
30
- open("#{config.path}/buffer_complete.log", "a") { |f|
31
- f.puts "|#{dest}|#{config[:nick_id]}|#{config[:nick]}|#{msg}~~~"
32
- }
33
- else
34
- if Thread.current[:on_thread]
35
- client.message(channel: dest, text: msg, as_user: true, thread_ts: Thread.current[:thread_ts])
33
+
34
+ dest = @channels_id[dest] if @channels_id.key?(dest) #it is a name of channel
35
+
36
+ on_thread ? txt_on_thread=':on_thread:' : txt_on_thread=''
37
+
38
+ if blocks.empty?
39
+ if !config.simulate #https://api.slack.com/docs/rate-limits
40
+ msg.size > 500 ? wait = 0.5 : wait = 0.1
41
+ sleep wait if Time.now <= (@last_respond + wait)
42
+ else
43
+ wait = 0
44
+ end
45
+ msgs = [] # max of max_chars_per_message characters per message
46
+ if max_chars_per_message.nil?
47
+ txt = msg
48
+ else
49
+ txt = ""
50
+ msg.split("\n").each do |m|
51
+ if (m + txt).size > max_chars_per_message
52
+ msgs << txt.chars.each_slice(max_chars_per_message).map(&:join) unless txt == ""
53
+ txt = ""
54
+ end
55
+ txt += (m + "\n")
56
+ end
57
+ end
58
+ msgs << txt
59
+ msgs.flatten!
60
+ if dest.nil?
61
+ if config[:simulate]
62
+ open("#{config.path}/buffer_complete.log", "a") { |f|
63
+ f.puts "|#{@channel_id}|#{config[:nick_id]}|#{config[:nick]}|#{txt_on_thread}#{msg}~~~"
64
+ }
65
+ else
66
+ if on_thread
67
+ msgs.each do |msg|
68
+ if web_client
69
+ resp = client.web_client.chat_postMessage(channel: @channel_id, text: msg, as_user: true, unfurl_links: unfurl_links, unfurl_media: unfurl_media, thread_ts: thread_ts)
70
+ else
71
+ resp = client.message(channel: @channel_id, text: msg, as_user: true, thread_ts: thread_ts, unfurl_links: unfurl_links, unfurl_media: unfurl_media)
72
+ end
73
+ sleep wait
74
+ end
75
+ else
76
+ msgs.each do |msg|
77
+ if web_client
78
+ resp = client.web_client.chat_postMessage(channel: @channel_id, text: msg, as_user: true, unfurl_links: unfurl_links, unfurl_media: unfurl_media)
79
+ else
80
+ resp = client.message(channel: @channel_id, text: msg, as_user: true, unfurl_links: unfurl_links, unfurl_media: unfurl_media)
81
+ end
82
+ sleep wait
83
+ end
84
+ end
85
+ end
86
+ if config[:testing] and config.on_master_bot and !@buffered
87
+ @buffered = true
88
+ open("#{config.path}/buffer.log", "a") { |f|
89
+ f.puts "|#{@channel_id}|#{config[:nick_id]}|#{config[:nick]}|#{msg}"
90
+ }
91
+ end
92
+ elsif dest[0] == "C" or dest[0] == "G" # channel
93
+ if config[:simulate]
94
+ open("#{config.path}/buffer_complete.log", "a") { |f|
95
+ f.puts "|#{dest}|#{config[:nick_id]}|#{config[:nick]}|#{txt_on_thread}#{msg}~~~"
96
+ }
97
+ else
98
+ if on_thread
99
+ msgs.each do |msg|
100
+ if web_client
101
+ resp = client.web_client.chat_postMessage(channel: dest, text: msg, as_user: true, unfurl_links: unfurl_links, unfurl_media: unfurl_media, thread_ts: thread_ts)
102
+ else
103
+ resp = client.message(channel: dest, text: msg, as_user: true, thread_ts: thread_ts, unfurl_links: unfurl_links, unfurl_media: unfurl_media)
104
+ end
105
+ sleep wait
106
+ end
107
+ else
108
+ msgs.each do |msg|
109
+ if web_client
110
+ resp = client.web_client.chat_postMessage(channel: dest, text: msg, as_user: true, unfurl_links: unfurl_links, unfurl_media: unfurl_media)
111
+ else
112
+ resp = client.message(channel: dest, text: msg, as_user: true, unfurl_links: unfurl_links, unfurl_media: unfurl_media)
113
+ end
114
+ sleep wait
115
+ end
116
+ end
117
+ end
118
+ if config[:testing] and config.on_master_bot and !@buffered
119
+ @buffered = true
120
+ open("#{config.path}/buffer.log", "a") { |f|
121
+ f.puts "|#{dest}|#{config[:nick_id]}|#{config[:nick]}|#{msg}"
122
+ }
123
+ end
124
+ elsif dest[0] == "D" or dest[0] == "U" or dest[0] == "W" # Direct message
125
+ msgs.each do |msg|
126
+ resp = send_msg_user(dest, msg, on_thread, unfurl_links: unfurl_links, unfurl_media: unfurl_media)
127
+ sleep wait
128
+ end
129
+ elsif dest[0] == "@"
130
+ begin
131
+ user_info = @users.select { |u| u.id == dest[1..-1] or u.name == dest[1..-1] or (u.key?(:enterprise_user) and u.enterprise_user.id == dest[1..-1]) }[-1]
132
+ msgs.each do |msg|
133
+ resp = send_msg_user(user_info.id, msg, on_thread, unfurl_links: unfurl_links, unfurl_media: unfurl_media)
134
+ sleep wait
135
+ end
136
+ rescue Exception => stack
137
+ @logger.warn("user #{dest} not found.")
138
+ @logger.warn stack
139
+ if Thread.current.key?(:dest)
140
+ respond("User #{dest} not found.")
141
+ end
142
+ result = false
143
+ end
144
+ else
145
+ @logger.warn("method respond not treated correctly: msg:#{msg} dest:#{dest}")
146
+ result = false
147
+ end
36
148
  else
37
- client.message(channel: dest, text: msg, as_user: true)
149
+ wait = 0.1
150
+ if dest.nil?
151
+ if config[:simulate]
152
+ open("#{config.path}/buffer_complete.log", "a") { |f|
153
+ f.puts "|#{@channel_id}|#{config[:nick_id]}|#{config[:nick]}|#{txt_on_thread}#{blocks.join}~~~"
154
+ }
155
+ else
156
+ if on_thread
157
+ blocks.each_slice(40).to_a.each do |blockstmp|
158
+ resp = client.web_client.chat_postMessage(channel: @channel_id, blocks: blockstmp, as_user: true, thread_ts: thread_ts)
159
+ sleep wait
160
+ end
161
+ else
162
+ blocks.each_slice(40).to_a.each do |blockstmp|
163
+ resp = client.web_client.chat_postMessage(channel: @channel_id, blocks: blockstmp, as_user: true)
164
+ sleep wait
165
+ end
166
+ end
167
+ end
168
+ if config[:testing] and config.on_master_bot and !@buffered
169
+ @buffered = true
170
+ open("#{config.path}/buffer.log", "a") { |f|
171
+ f.puts "|#{@channel_id}|#{config[:nick_id]}|#{config[:nick]}|#{blocks.join}"
172
+ }
173
+ end
174
+ elsif dest[0] == "C" or dest[0] == "G" # channel
175
+ if config[:simulate]
176
+ open("#{config.path}/buffer_complete.log", "a") { |f|
177
+ f.puts "|#{dest}|#{config[:nick_id]}|#{config[:nick]}|#{txt_on_thread}#{blocks.join}~~~"
178
+ }
179
+ else
180
+ if on_thread
181
+ blocks.each_slice(40).to_a.each do |blockstmp|
182
+ resp = client.web_client.chat_postMessage(channel: dest, blocks: blockstmp, as_user: true, thread_ts: thread_ts)
183
+ sleep wait
184
+ end
185
+ else
186
+ blocks.each_slice(40).to_a.each do |blockstmp|
187
+ resp = client.web_client.chat_postMessage(channel: dest, blocks: blockstmp, as_user: true)
188
+ sleep wait
189
+ end
190
+ end
191
+ end
192
+ if config[:testing] and config.on_master_bot and !@buffered
193
+ @buffered = true
194
+ open("#{config.path}/buffer.log", "a") { |f|
195
+ f.puts "|#{dest}|#{config[:nick_id]}|#{config[:nick]}|#{blocks.join}"
196
+ }
197
+ end
198
+ elsif dest[0] == "D" or dest[0] == "U" or dest[0] == "W" # Direct message
199
+ blocks.each_slice(40).to_a.each do |blockstmp|
200
+ resp = send_msg_user(dest, msg, on_thread, unfurl_links: unfurl_links, unfurl_media: unfurl_media, blocks: blockstmp)
201
+ sleep wait
202
+ end
203
+ elsif dest[0] == "@"
204
+ begin
205
+ user_info = @users.select { |u| u.id == dest[1..-1] or (u.key?(:enterprise_user) and u.enterprise_user.id == dest[1..-1]) }[-1]
206
+ blocks.each_slice(40).to_a.each do |blockstmp|
207
+ resp = send_msg_user(user_info.id, msg, on_thread, unfurl_links: unfurl_links, unfurl_media: unfurl_media, blocks: blockstmp)
208
+ sleep wait
209
+ end
210
+ rescue Exception => stack
211
+ @logger.warn("user #{dest} not found.")
212
+ @logger.warn stack
213
+ if Thread.current.key?(:dest)
214
+ respond("User #{dest} not found.")
215
+ end
216
+ result = false
217
+ end
218
+ else
219
+ @logger.warn("method respond not treated correctly: msg:#{msg} dest:#{dest}")
220
+ result = false
221
+ end
38
222
  end
39
- end
40
- if config[:testing] and config.on_master_bot
41
- open("#{config.path}/buffer.log", "a") { |f|
42
- f.puts "|#{dest}|#{config[:nick_id]}|#{config[:nick]}|#{msg}"
43
- }
44
- end
45
- elsif dest[0] == "D" or dest[0] == "U" or dest[0] == "W" # Direct message
46
- send_msg_user(dest, msg)
47
- elsif dest[0] == "@"
48
- begin
49
- user_info = get_user_info(dest)
50
- send_msg_user(user_info.user.id, msg)
223
+ @last_respond = Time.now
51
224
  rescue Exception => stack
52
- @logger.warn("user #{dest} not found.")
53
225
  @logger.warn stack
54
- if Thread.current.key?(:dest)
55
- respond("User #{dest} not found.")
56
- end
226
+ result = false
57
227
  end
58
- else
59
- @logger.warn("method respond not treated correctly: msg:#{msg} dest:#{dest}")
60
228
  end
61
- @last_respond = Time.now
229
+ if Thread.current.key?(:routine) and Thread.current[:routine]
230
+ File.write("#{config.path}/routines/#{@channel_id}/#{Thread.current[:routine_name]}_output.txt", msg, mode: "a+")
231
+ end
232
+ result = resp if return_message
233
+ return result
62
234
  end
63
-
64
235
  end
@@ -1,6 +1,5 @@
1
1
  class SlackSmartBot
2
- def respond_direct(msg)
3
- dest = Thread.current[:user].id
4
- respond(msg, dest)
2
+ def respond_direct(msg, unfurl_links: true, unfurl_media: true)
3
+ respond(msg, :direct, unfurl_links: unfurl_links, unfurl_media: unfurl_media)
5
4
  end
6
5
  end
@@ -0,0 +1,5 @@
1
+ class SlackSmartBot
2
+ def respond_thread(msg, unfurl_links: true, unfurl_media: true)
3
+ respond(msg, :on_thread, unfurl_links: unfurl_links, unfurl_media: unfurl_media)
4
+ end
5
+ end
@@ -7,42 +7,46 @@ class SlackSmartBot
7
7
  #send_file(dest, 'the message', "myfile.rb", 'message to be sent', 'text/plain', "ruby", content: "the content to be sent when no file supplied")
8
8
  def send_file(to, msg, file, title, format, type = "text", content: '')
9
9
  unless config[:simulate]
10
- file = 'myfile' if file.to_s == '' and content!=''
11
- if to[0] == "U" or to[0] == "W" #user
12
- im = client.web_client.im_open(user: to)
13
- channel = im["channel"]["id"]
14
- else
15
- channel = to
16
- end
10
+ begin
11
+ file = 'myfile' if file.to_s == '' and content!=''
12
+ if to[0] == "U" or to[0] == "W" #user
13
+ im = client.web_client.conversations_open(users: id_user)
14
+ channel = im["channel"]["id"]
15
+ else
16
+ channel = to
17
+ end
17
18
 
18
- if Thread.current[:on_thread]
19
- ts = Thread.current[:thread_ts]
20
- else
21
- ts = nil
22
- end
19
+ if Thread.current[:on_thread]
20
+ ts = Thread.current[:thread_ts]
21
+ else
22
+ ts = nil
23
+ end
23
24
 
24
- if content.to_s == ''
25
- client.web_client.files_upload(
26
- channels: channel,
27
- as_user: true,
28
- file: Faraday::UploadIO.new(file, format),
29
- title: title,
30
- filename: file,
31
- filetype: type,
32
- initial_comment: msg,
33
- thread_ts: ts
34
- )
35
- else
36
- client.web_client.files_upload(
37
- channels: channel,
38
- as_user: true,
39
- content: content,
40
- title: title,
41
- filename: file,
42
- filetype: type,
43
- initial_comment: msg,
44
- thread_ts: ts
45
- )
25
+ if content.to_s == ''
26
+ client.web_client.files_upload(
27
+ channels: channel,
28
+ as_user: true,
29
+ file: Faraday::UploadIO.new(file, format),
30
+ title: title,
31
+ filename: file,
32
+ filetype: type,
33
+ initial_comment: msg,
34
+ thread_ts: ts
35
+ )
36
+ else
37
+ client.web_client.files_upload(
38
+ channels: channel,
39
+ as_user: true,
40
+ content: content,
41
+ title: title,
42
+ filename: file,
43
+ filetype: type,
44
+ initial_comment: msg,
45
+ thread_ts: ts
46
+ )
47
+ end
48
+ rescue Exception => stack
49
+ @logger.warn stack
46
50
  end
47
51
  end
48
52
  end
@@ -2,31 +2,36 @@ class SlackSmartBot
2
2
 
3
3
  # to: (String) Channel name or id
4
4
  # msg: (String) message to send
5
- def send_msg_channel(to, msg)
5
+ def send_msg_channel(to, msg, unfurl_links: true, unfurl_media: true)
6
6
  unless msg == ""
7
- get_channels_name_and_id() unless @channels_name.key?(to) or @channels_id.key?(to)
8
- if @channels_name.key?(to) #it is an id
9
- channel_id = to
10
- elsif @channels_id.key?(to) #it is a channel name
11
- channel_id = @channels_id[to]
12
- else
13
- @logger.fatal "Channel: #{to} not found. Message: #{msg}"
14
- end
15
- if config[:simulate]
16
- open("#{config.path}/buffer_complete.log", "a") { |f|
17
- f.puts "|#{channel_id}|#{config[:nick_id]}|#{config[:nick]}|#{msg}~~~"
18
- }
19
- else
20
- if Thread.current[:on_thread]
21
- client.message(channel: channel_id, text: msg, as_user: true, thread_ts: Thread.current[:thread_ts])
7
+ begin
8
+ get_channels_name_and_id() unless @channels_name.key?(to) or @channels_id.key?(to)
9
+ if @channels_name.key?(to) #it is an id
10
+ channel_id = to
11
+ elsif @channels_id.key?(to) #it is a channel name
12
+ channel_id = @channels_id[to]
22
13
  else
23
- client.message(channel: channel_id, text: msg, as_user: true)
14
+ @logger.fatal "Channel: #{to} not found. Message: #{msg}"
24
15
  end
25
- end
26
- if config[:testing] and config.on_master_bot
27
- open("#{config.path}/buffer.log", "a") { |f|
28
- f.puts "|#{channel_id}|#{config[:nick_id]}|#{config[:nick]}|#{msg}"
29
- }
16
+ if config[:simulate]
17
+ open("#{config.path}/buffer_complete.log", "a") { |f|
18
+ f.puts "|#{channel_id}|#{config[:nick_id]}|#{config[:nick]}|#{msg}~~~"
19
+ }
20
+ else
21
+ if Thread.current[:on_thread]
22
+ client.message(channel: channel_id, text: msg, as_user: true, thread_ts: Thread.current[:thread_ts], unfurl_links: unfurl_links, unfurl_media: unfurl_media)
23
+ else
24
+ client.message(channel: channel_id, text: msg, as_user: true, unfurl_links: unfurl_links, unfurl_media: unfurl_media)
25
+ end
26
+ end
27
+ if config[:testing] and config.on_master_bot and !@buffered
28
+ @buffered = true
29
+ open("#{config.path}/buffer.log", "a") { |f|
30
+ f.puts "|#{channel_id}|#{config[:nick_id]}|#{config[:nick]}|#{msg}"
31
+ }
32
+ end
33
+ rescue Exception => stack
34
+ @logger.warn stack
30
35
  end
31
36
  end
32
37
  end
@@ -1,45 +1,71 @@
1
1
  class SlackSmartBot
2
2
 
3
3
  #to send messages without listening for a response to users
4
- def send_msg_user(id_user, msg)
5
- unless msg == ""
6
- if id_user[0] == "D"
7
- if config[:simulate]
8
- open("#{config.path}/buffer_complete.log", "a") { |f|
9
- f.puts "|#{id_user}|#{config[:nick_id]}|#{config[:nick]}|#{msg}~~~"
10
- }
11
- else
12
- if Thread.current[:on_thread]
13
- client.message(channel: id_user, as_user: true, text: msg, thread_ts: Thread.current[:thread_ts])
4
+ def send_msg_user(id_user, msg='', on_thread=nil, unfurl_links: true, unfurl_media: true, blocks: [], web_client: true)
5
+ resp = nil
6
+ unless msg == "" and blocks.empty?
7
+ begin
8
+ on_thread = Thread.current[:on_thread] if on_thread.nil?
9
+ web_client = true if !blocks.empty? or !unfurl_links or !unfurl_media
10
+ if id_user[0] == "D"
11
+ if config[:simulate]
12
+ open("#{config.path}/buffer_complete.log", "a") { |f|
13
+ f.puts "|#{id_user}|#{config[:nick_id]}|#{config[:nick]}|#{msg}~~~"
14
+ }
14
15
  else
15
- client.message(channel: id_user, as_user: true, text: msg)
16
+ if web_client
17
+ if on_thread
18
+ resp = client.web_client.chat_postMessage(channel: id_user, text: msg, blocks: blocks, as_user: true, unfurl_links: unfurl_links, unfurl_media: unfurl_media, thread_ts: Thread.current[:thread_ts])
19
+ else
20
+ resp = client.web_client.chat_postMessage(channel: id_user, text: msg, blocks: blocks, as_user: true, unfurl_links: unfurl_links, unfurl_media: unfurl_media)
21
+ end
22
+ else
23
+ if on_thread
24
+ resp = client.message(channel: id_user, as_user: true, text: msg, thread_ts: Thread.current[:thread_ts], unfurl_links: unfurl_links, unfurl_media: unfurl_media)
25
+ else
26
+ resp = client.message(channel: id_user, as_user: true, text: msg, unfurl_links: unfurl_links, unfurl_media: unfurl_media)
27
+ end
28
+ end
16
29
  end
17
- end
18
- if config[:testing] and config.on_master_bot
19
- open("#{config.path}/buffer.log", "a") { |f|
20
- f.puts "|#{id_user}|#{config[:nick_id]}|#{config[:nick]}|#{msg}"
21
- }
22
- end
23
- else
24
- im = client.web_client.im_open(user: id_user)
25
- if config[:simulate]
26
- open("#{config.path}/buffer_complete.log", "a") { |f|
27
- f.puts "|#{im["channel"]["id"]}|#{config[:nick_id]}|#{config[:nick]}|#{msg}~~~"
28
- }
29
- else
30
- if Thread.current[:on_thread]
31
- client.message(channel: im["channel"]["id"], as_user: true, text: msg, thread_ts: Thread.current[:thread_ts])
32
- else
33
- client.message(channel: im["channel"]["id"], as_user: true, text: msg)
30
+ if config[:testing] and config.on_master_bot and !@buffered
31
+ @buffered = true
32
+ open("#{config.path}/buffer.log", "a") { |f|
33
+ f.puts "|#{id_user}|#{config[:nick_id]}|#{config[:nick]}|#{msg}#{blocks.join}"
34
+ }
35
+ end
36
+ else
37
+ if config[:simulate]
38
+ open("#{config.path}/buffer_complete.log", "a") { |f|
39
+ f.puts "|#{DIRECT[id_user.to_sym].ubot}|#{config[:nick_id]}|#{config[:nick]}|#{msg}#{blocks.join}~~~"
40
+ }
41
+ else
42
+ im = client.web_client.conversations_open(users: id_user)
43
+ if web_client
44
+ if on_thread
45
+ resp = client.web_client.chat_postMessage(channel: im["channel"]["id"], text: msg, blocks: blocks, as_user: true, unfurl_links: unfurl_links, unfurl_media: unfurl_media, thread_ts: Thread.current[:thread_ts])
46
+ else
47
+ resp = client.web_client.chat_postMessage(channel: im["channel"]["id"], text: msg, blocks: blocks, as_user: true, unfurl_links: unfurl_links, unfurl_media: unfurl_media)
48
+ end
49
+ else
50
+ if on_thread
51
+ resp = client.message(channel: im["channel"]["id"], as_user: true, text: msg, thread_ts: Thread.current[:thread_ts], unfurl_links: unfurl_links, unfurl_media: unfurl_media)
52
+ else
53
+ resp = client.message(channel: im["channel"]["id"], as_user: true, text: msg, unfurl_links: unfurl_links, unfurl_media: unfurl_media)
54
+ end
55
+ end
56
+ end
57
+ if config[:testing] and config.on_master_bot and !@buffered
58
+ @buffered = true
59
+ open("#{config.path}/buffer.log", "a") { |f|
60
+ f.puts "|#{im["channel"]["id"]}|#{config[:nick_id]}|#{config[:nick]}|#{msg}#{blocks.join}"
61
+ }
34
62
  end
35
63
  end
36
- if config[:testing] and config.on_master_bot
37
- open("#{config.path}/buffer.log", "a") { |f|
38
- f.puts "|#{im["channel"]["id"]}|#{config[:nick_id]}|#{config[:nick]}|#{msg}"
39
- }
40
- end
64
+ rescue Exception => stack
65
+ @logger.warn stack
41
66
  end
42
67
  end
68
+ return resp
43
69
  end
44
70
 
45
71
  end