lib/ruboty/adapters/slack_rtm.rb in ruboty-slack_rtm-2.4.2 vs lib/ruboty/adapters/slack_rtm.rb in ruboty-slack_rtm-2.4.3

- old
+ new

@@ -97,11 +97,10 @@ end # event handlers def on_message(data) - data = resolve_mention!(data) user = user_info(data['user']) || {} channel = channel_info(data['channel']) if data['subtype'] == 'bot_message' && ENV['SLACK_IGNORE_BOT_MESSAGE'] == '1' @@ -120,20 +119,22 @@ from: data['channel'], from_name: user['name'], to: channel_to, channel: channel, user: user, - mention_to: data['mention_to'], time: Time.at(data['ts'].to_f) } - robot.receive(message_info.merge(body: CGI.unescapeHTML(data['text']))) + text, mention_to = extract_mention(data['text']) + robot.receive(message_info.merge(body: text, mention_to: mention_to)) (data['attachments'] || []).each do |attachment| - body = attachment['fallback'] || "#{attachment['text']} #{attachment['pretext']}" + body, body_mention_to = extract_mention(attachment['fallback'] || "#{attachment['text']} #{attachment['pretext']}".strip) - robot.receive(message_info.merge(body: CGI.unescapeHTML(body))) unless body.empty? + unless body.empty? + robot.receive(message_info.merge(body: body, mention_to: body_mention_to)) + end end end def on_channel_change(data) make_channels_cache @@ -148,59 +149,57 @@ @user_info_caches[user['id']] = user end alias_method :on_bot_added, :on_user_change alias_method :on_bot_changed, :on_user_change - def resolve_mention!(data) - data = data.dup + def extract_mention(text) + mention_to = [] - data['mention_to'] = [] - - data['text'] = (data['text'] || '').gsub(/\<\@(?<uid>[0-9A-Z]+)(?:\|(?<name>[^>]+))?\>/) do |_| + text = (text || '').gsub(/\<\@(?<uid>[0-9A-Z]+)(?:\|(?<name>[^>]+))?\>/) do |_| name = Regexp.last_match[:name] unless name user = user_info(Regexp.last_match[:uid]) - data['mention_to'] << user + mention_to << user name = user['name'] end "@#{name}" end - data['text'].gsub!(/\<!subteam\^(?<usergroup_id>[0-9A-Z]+)(?:\|(?<handle>[^>]+))?\>/) do |_| + 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 |_| + text.gsub!(/\<!(?<special>[^>|@]+)(\|\@[^>]+)?\>/) do |_| "@#{Regexp.last_match[:special]}" end - data['text'].gsub!(/\<((?<link>[^>|]+)(?:\|(?<ref>[^>]*))?)\>/) do |_| + text.gsub!(/\<((?<link>[^>|]+)(?:\|(?<ref>[^>]*))?)\>/) do |_| Regexp.last_match[:ref] || Regexp.last_match[:link] end - data['text'].gsub!(/\#(?<room_id>[A-Z0-9]+)/) do |_| + text.gsub!(/\#(?<room_id>[A-Z0-9]+)/) do |_| room_id = Regexp.last_match[:room_id] msg = "##{room_id}" if channel = channel_info(room_id) msg = "##{channel['name']}" end msg end - data + [CGI.unescapeHTML(text), mention_to] end def resolve_send_mention(text) text = text.to_s text.gsub!(/@(?<mention>[0-9a-z._-]+)/) do |_|