ruboty-slack_rtm 0.0.2 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 11510a4a0e35da5630eff5dc6e94784225dff973
4
- data.tar.gz: c4d99a6dcbae908eb437f09387ddd60a4b9cce5c
3
+ metadata.gz: 5e4546f427a69aee99fc391dd6681301c7109962
4
+ data.tar.gz: da84efd1447b98ace8eb8cbe58d433e6d20ce8ce
5
5
  SHA512:
6
- metadata.gz: b6a2d177a7ee38661ebacd8a5a3ca2c1384dc951c040960242d29b4066c9630dfc36b5281060893e42132079ed0e65f310c78ac6a781558683a903f11858ee70
7
- data.tar.gz: 5778e53030dc8526bfcef607c4c55863f6999b395bc582496c2d84127da9ed997779263b27d05db29b9492a0cd799fc58d046e3c7e65cb7f70d204023721f635
6
+ metadata.gz: 18183e21f0cf9ed4d4b0606555ddd6e50951a8fb732172cd06fb4d1f8d024dd37f537c2c9fbb26bd682b4904f9a6aa6704a55dfd6eb23ea5ff3facad01d45229
7
+ data.tar.gz: df72d3706b7aec6e2c32c025ffdad945f189a1e6c6595569aa13bb444cd30dd66f4b89ac82412150d18a0122b91067472f81a57666ba44a6fb4c36975fd88b85
@@ -18,7 +18,7 @@ module Ruboty
18
18
  realtime.send(
19
19
  type: 'message',
20
20
  channel: message[:to],
21
- text: message[:code] ? "```\n#{message[:body]}\n```" : message[:body],
21
+ text: message[:code] ? "```\n#{message[:body]}\n```" : resolve_send_mention(message[:body]),
22
22
  mrkdwn: true
23
23
  )
24
24
  end
@@ -31,6 +31,9 @@ module Ruboty
31
31
  @channel_info_cahces = {}
32
32
 
33
33
  ENV['RUBOTY_NAME'] ||= response['user']
34
+
35
+ make_users_cache
36
+ make_channels_cache
34
37
  end
35
38
 
36
39
  def bind
@@ -86,9 +89,7 @@ module Ruboty
86
89
  end
87
90
 
88
91
  def on_channel_change(data)
89
- channel_id = data['channel']
90
- channel_id = channel_id['id'] if channel_id.is_a?(Hash)
91
- @channel_info_cahces[channel_id] = nil
92
+ make_channels_cache
92
93
  end
93
94
  alias_method :on_channel_deleted, :on_channel_change
94
95
  alias_method :on_channel_renamed, :on_channel_change
@@ -107,17 +108,96 @@ module Ruboty
107
108
 
108
109
  data['mention_to'] = []
109
110
 
110
- (data['text'] || '').gsub!(/\<\@(?<uid>[0-9A-Z]+)\>/) do |_|
111
- user = user_info(Regexp.last_match[:uid])
111
+ data['text'] = (data['text'] || '').gsub(/\<\@(?<uid>[0-9A-Z]+)(?:\|(?<name>[^>]+))?\>/) do |_|
112
+ name = Regexp.last_match[:name]
113
+
114
+ unless name
115
+ user = user_info(Regexp.last_match[:uid])
116
+
117
+ data['mention_to'] << user
118
+
119
+ name = user['name']
120
+ end
121
+
122
+ "@#{name}"
123
+ end
112
124
 
113
- data['mention_to'] << user
125
+ data['text'].gsub!(/\<!(?<special>[^>]+)\>/) do |_|
126
+ "@#{Regexp.last_match[:special]}"
127
+ end
114
128
 
115
- "@#{user['name']}"
129
+ data['text'].gsub!(/\<((?<link>[^>|]+)(?:\|(?<ref>[^>]*))?)\>/) do |_|
130
+ Regexp.last_match[:ref] || Regexp.last_match[:link]
131
+ end
132
+
133
+
134
+ data['text'].gsub!(/\#(?<room_id>[A-Z0-9]+)/) do |_|
135
+ room_id = Regexp.last_match[:room_id]
136
+ msg = "##{room_id}"
137
+
138
+ if channel = channel_info(room_id)
139
+ msg = "##{channel['name']}"
140
+ end
141
+
142
+ msg
116
143
  end
117
144
 
118
145
  data
119
146
  end
120
147
 
148
+ def resolve_send_mention(text)
149
+ text = text.to_s
150
+ text.gsub!(/@(?<mention>[0-9a-z_-]+)/) do |_|
151
+ mention = Regexp.last_match[:mention]
152
+ msg = "@#{mention}"
153
+
154
+ @user_info_caches.each_pair do |id, user|
155
+ if user['name'].downcase == mention.downcase
156
+ msg = "<@#{id}|#{mention}>"
157
+ end
158
+ end
159
+
160
+ msg
161
+ end
162
+
163
+ text.gsub!(/@(?<special>(?:everyone|group|channel))/) do |_|
164
+ "<!#{Regexp.last_match[:special]}>"
165
+ end
166
+
167
+ text.gsub!(/\#(?<room_id>[a-z0-9_-]+)/) do |_|
168
+ room_id = Regexp.last_match[:room_id]
169
+ msg = "##{room_id}"
170
+
171
+ @channel_info_cahces.each_pair do |id, channel|
172
+ if channel && channel['name'] == room_id
173
+ msg = "<##{id}|#{room_id}>"
174
+ end
175
+ end
176
+
177
+ msg
178
+ end
179
+
180
+ text
181
+ end
182
+
183
+ def make_users_cache
184
+ resp = client.users_list
185
+ if resp['ok']
186
+ resp['members'].each do |user|
187
+ @user_info_caches[user['id']] = user
188
+ end
189
+ end
190
+ end
191
+
192
+ def make_channels_cache
193
+ resp = client.channels_list
194
+ if resp['ok']
195
+ resp['channels'].each do |channel|
196
+ @channel_info_cahces[channel['id']] = channel
197
+ end
198
+ end
199
+ end
200
+
121
201
  def user_info(user_id)
122
202
  @user_info_caches[user_id] ||= begin
123
203
  resp = client.users_info(user: user_id)
@@ -1,5 +1,5 @@
1
1
  module Ruboty
2
2
  module SlackRTM
3
- VERSION = '0.0.2'
3
+ VERSION = '1.0.0'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruboty-slack_rtm
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sho Kusano
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-01-19 00:00:00.000000000 Z
11
+ date: 2015-02-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -131,7 +131,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
131
131
  version: '0'
132
132
  requirements: []
133
133
  rubyforge_project:
134
- rubygems_version: 2.2.2
134
+ rubygems_version: 2.4.5
135
135
  signing_key:
136
136
  specification_version: 4
137
137
  summary: Slack real time messaging adapter for Ruboty