lib/ruboty/adapters/slack_rtm.rb in ruboty-slack_rtm-1.5.1 vs lib/ruboty/adapters/slack_rtm.rb in ruboty-slack_rtm-1.6.0

- old
+ new

@@ -38,15 +38,17 @@ def init response = client.auth_test @user_info_caches = {} @channel_info_caches = {} + @usergroup_info_caches = {} ENV['RUBOTY_NAME'] ||= response['user'] make_users_cache make_channels_cache + make_usergroups_cache end def bind realtime.on(:message) do |data| method_name = "on_#{data['type']}".to_sym @@ -149,10 +151,20 @@ end "@#{name}" end + data['text'].gsub!(/\<!subteam\^(?<usergroup_id>[0-9A-Z]+)(?:\|(?<handle>[^>]+))?\>/) do |_| + handle = Regexp.last_match[:handle] + + unless handle + handle = usergroup_info(Regexp.last_match[:usergroup_id]) + end + + "#{handle}" + end + data['text'].gsub!(/\<!(?<special>[^>|@]+)(\|\@[^>]+)?\>/) do |_| "@#{Regexp.last_match[:special]}" end data['text'].gsub!(/\<((?<link>[^>|]+)(?:\|(?<ref>[^>]*))?)\>/) do |_| @@ -191,10 +203,22 @@ text.gsub!(/@(?<special>(?:everyone|group|channel|here))/) do |_| "<!#{Regexp.last_match[:special]}>" end + text.gsub!(/@(?<subteam_name>[0-9a-z._-]+)/) do |_| + subteam_name = Regexp.last_match[:subteam_name] + msg = "@#{subteam_name}" + + @usergroup_info_caches.each_pair do |id, usergroup| + if usergroup && usergroup['handle'] == subteam_name + msg = "<!subteam^#{usergroup['id']}>" + end + end + msg + end + text.gsub!(/\#(?<room_id>[a-z0-9_-]+)/) do |_| room_id = Regexp.last_match[:room_id] msg = "##{room_id}" @channel_info_caches.each_pair do |id, channel| @@ -225,10 +249,19 @@ @channel_info_caches[channel['id']] = channel end end end + def make_usergroups_cache + resp = client.get("usergroups.list") + if resp['ok'] + resp['usergroups'].each do |usergroup| + @usergroup_info_caches[usergroup['id']] = usergroup + end + end + end + def user_info(user_id) return {} if user_id.to_s.empty? @user_info_caches[user_id] ||= begin resp = client.users_info(user: user_id) @@ -257,9 +290,16 @@ ret_id = id break end end return ret_id + end + + def usergroup_info(usergroup_id) + @usergroup_info_caches[usergroup_id] || begin + make_usergroups_cache + @usergroup_info_caches[usergroup_id] + end end end end end