lib/ruboty/adapters/slack_rtm.rb in ruboty-slack_rtm-0.0.2 vs lib/ruboty/adapters/slack_rtm.rb in ruboty-slack_rtm-1.0.0
- old
+ new
@@ -16,11 +16,11 @@
def say(message)
realtime.send(
type: 'message',
channel: message[:to],
- text: message[:code] ? "```\n#{message[:body]}\n```" : message[:body],
+ text: message[:code] ? "```\n#{message[:body]}\n```" : resolve_send_mention(message[:body]),
mrkdwn: true
)
end
private
@@ -29,10 +29,13 @@
response = client.auth_test
@user_info_caches = {}
@channel_info_cahces = {}
ENV['RUBOTY_NAME'] ||= response['user']
+
+ make_users_cache
+ make_channels_cache
end
def bind
realtime.on(:message) do |data|
method_name = "on_#{data['type']}".to_sym
@@ -84,13 +87,11 @@
time: Time.at(data['ts'].to_f)
)
end
def on_channel_change(data)
- channel_id = data['channel']
- channel_id = channel_id['id'] if channel_id.is_a?(Hash)
- @channel_info_cahces[channel_id] = nil
+ make_channels_cache
end
alias_method :on_channel_deleted, :on_channel_change
alias_method :on_channel_renamed, :on_channel_change
alias_method :on_channel_archived, :on_channel_change
alias_method :on_channel_unarchived, :on_channel_change
@@ -105,18 +106,97 @@
def resolve_mention!(data)
data = data.dup
data['mention_to'] = []
- (data['text'] || '').gsub!(/\<\@(?<uid>[0-9A-Z]+)\>/) do |_|
- user = user_info(Regexp.last_match[:uid])
+ data['text'] = (data['text'] || '').gsub(/\<\@(?<uid>[0-9A-Z]+)(?:\|(?<name>[^>]+))?\>/) do |_|
+ name = Regexp.last_match[:name]
- data['mention_to'] << user
+ unless name
+ user = user_info(Regexp.last_match[:uid])
- "@#{user['name']}"
+ data['mention_to'] << user
+
+ name = user['name']
+ end
+
+ "@#{name}"
end
+ data['text'].gsub!(/\<!(?<special>[^>]+)\>/) do |_|
+ "@#{Regexp.last_match[:special]}"
+ end
+
+ data['text'].gsub!(/\<((?<link>[^>|]+)(?:\|(?<ref>[^>]*))?)\>/) do |_|
+ Regexp.last_match[:ref] || Regexp.last_match[:link]
+ end
+
+
+ data['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
+ end
+
+ def resolve_send_mention(text)
+ text = text.to_s
+ text.gsub!(/@(?<mention>[0-9a-z_-]+)/) do |_|
+ mention = Regexp.last_match[:mention]
+ msg = "@#{mention}"
+
+ @user_info_caches.each_pair do |id, user|
+ if user['name'].downcase == mention.downcase
+ msg = "<@#{id}|#{mention}>"
+ end
+ end
+
+ msg
+ end
+
+ text.gsub!(/@(?<special>(?:everyone|group|channel))/) do |_|
+ "<!#{Regexp.last_match[:special]}>"
+ end
+
+ text.gsub!(/\#(?<room_id>[a-z0-9_-]+)/) do |_|
+ room_id = Regexp.last_match[:room_id]
+ msg = "##{room_id}"
+
+ @channel_info_cahces.each_pair do |id, channel|
+ if channel && channel['name'] == room_id
+ msg = "<##{id}|#{room_id}>"
+ end
+ end
+
+ msg
+ end
+
+ text
+ end
+
+ def make_users_cache
+ resp = client.users_list
+ if resp['ok']
+ resp['members'].each do |user|
+ @user_info_caches[user['id']] = user
+ end
+ end
+ end
+
+ def make_channels_cache
+ resp = client.channels_list
+ if resp['ok']
+ resp['channels'].each do |channel|
+ @channel_info_cahces[channel['id']] = channel
+ end
+ end
end
def user_info(user_id)
@user_info_caches[user_id] ||= begin
resp = client.users_info(user: user_id)