wechat-bot2 0.1.2 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 45b229da1d868e19135b5c098047080fa8c4c5021e59a0fe01fb39472c70df17
4
- data.tar.gz: 427a0a11dafe69a2a4a6fc739da898713a57ca799590f7bcd85259dc38ed3f38
3
+ metadata.gz: 2d129e298145340b212f0f23de82dbf6c571580d29984553c68e77339fa8ce4d
4
+ data.tar.gz: 15d8e87b1bebc29ff8b8727f64dbe62b3dad74d5b61bd2920050e7d9b320b50e
5
5
  SHA512:
6
- metadata.gz: ce306583f3407ff60a1a04912b47a48b4a6de2c7c4d4a662894221092d4d027ac87fcbf70c3fa3d7ae4a4eba1f7138003b7f3975e0416295051fcf22fd91f33a
7
- data.tar.gz: 4dc39e55e76176775666753b7b39a61829ec55b6854eb47d05335b5afc8d6a383b44649bf2f2bc5247f12d2ced0693db78386990e953aa961db42d34d63aaea5
6
+ metadata.gz: 2fb047ab3e2829968a859c16e38ba8ad5cb51f74d21f6395903ecc94a363727dd4b11d4e54bf8e408db75ba3112a6a3b921f6f0ffc1ec431df2b0c91ed59e0f8
7
+ data.tar.gz: 5de0e2c9a843309fc2ecf67aeff30ad71377193d5b0e72c2160a740b9470c25a8d78fd141d5adc6c6c4f702bc156b6ea0af8cd8cfc475421196bd8d0c9d47442
@@ -90,6 +90,14 @@ module WeChat::Bot
90
90
  attr(:members)
91
91
  end
92
92
 
93
+ def find_member(args)
94
+ if args[:username]
95
+ members.find{|contact| args[:username] == contact.username }
96
+ elsif args[:nickname]
97
+ members.find{|contact| args[:nickname] == contact.nickname }
98
+ end
99
+ end
100
+
93
101
  # 联系人解析
94
102
  #
95
103
  # @param [Hash<Object, Object>] raw
@@ -117,7 +125,7 @@ module WeChat::Bot
117
125
  end
118
126
 
119
127
  def to_s
120
- "#<#{self.class}:#{object_id.to_s(16)} username='#{username}' nickname='#{nickname}' kind='#{kind}'>"
128
+ "#<#{self.class}:#{object_id.to_s(16)} username='#{username}' nickname='#{nickname}' kind='#{kind}' members=#{members.size}>"
121
129
  end
122
130
 
123
131
  private
@@ -21,7 +21,7 @@ module WeChat::Bot
21
21
  end
22
22
 
23
23
  GROUP_MESSAGE_REGEX = /^(@\w+):<br\/>(.*)$/
24
- AT_MESSAGE_REGEX = /@([^\s]+) (.*)/
24
+ AT_MESSAGE_REGEX = /@([^\s]+?) /
25
25
 
26
26
  # 原始消息
27
27
  # @return [Hash<Object, Object>]
@@ -50,6 +50,8 @@ module WeChat::Bot
50
50
  # 用户或者群组
51
51
  # @return [Contact]
52
52
  attr_reader :from
53
+ # 群组消息时为消息用户,其余为from
54
+ attr_reader :from_user
53
55
 
54
56
  # 消息正文
55
57
  # @return [String]
@@ -94,16 +96,22 @@ module WeChat::Bot
94
96
  parse_source
95
97
  parse_kind
96
98
 
99
+ # TODO: 来自于特殊账户无法获取联系人信息,需要单独处理
100
+ @from = @bot.contact_list.find(username: @raw["FromUserName"])
101
+
97
102
  message = @raw["Content"].convert_emoji
98
103
  message = CGI.unescape_html(message) if @kinde != Message::Kind::Text
104
+
99
105
  if match = group_message(message)
100
- # from_username = match[0]
101
106
  message = match[1]
107
+ @from_user = @from.find_member(username: match[0])
108
+ @at_message_names = match[2]
109
+ else
110
+ @from_user = @from
102
111
  end
103
112
 
104
113
  @message = message
105
- # TODO: 来自于特殊账户无法获取联系人信息,需要单独处理
106
- @from = @bot.contact_list.find(username: @raw["FromUserName"])
114
+
107
115
  parse_emoticon if @kind == Message::Kind::Emoticon
108
116
 
109
117
  case @kind
@@ -139,9 +147,24 @@ module WeChat::Bot
139
147
  end
140
148
 
141
149
  def at_message?
142
- @at_mesage == true
150
+ !( @at_message_names.nil? || @at_message_names.empty? )
143
151
  end
144
152
 
153
+ def at_members
154
+ if at_message?
155
+ @at_message_names.map{|nickname| from.find_member(nickname: nickname) }
156
+ else
157
+ []
158
+ end
159
+ end
160
+
161
+ # 私聊或者群聊被@
162
+ def talked_to?
163
+ (source == Contact::Kind::User) || at_members.any?{|member| !!member && (member.username == @raw['ToUserName']) }
164
+ end
165
+
166
+ private
167
+
145
168
  # 解析消息来源
146
169
  #
147
170
  # 特殊账户/群聊/公众号/用户
@@ -218,11 +241,7 @@ module WeChat::Bot
218
241
  @events << @kind
219
242
  @events << @source
220
243
 
221
- @at_mesage = false
222
- if @source == :group && @raw["Content"] =~ /@([^\s]+)\s+(.*)/
223
- @events << :at_message
224
- @at_mesage = true
225
- end
244
+ @events << :at_message if at_message?
226
245
  end
227
246
 
228
247
  # 解析表情
@@ -259,10 +278,10 @@ module WeChat::Bot
259
278
  # - 1 message
260
279
  def group_message(message)
261
280
  if match = GROUP_MESSAGE_REGEX.match(message)
262
- return [match[1], at_message(match[2])]
281
+ at_message(match[2]).unshift(match[1])
282
+ else
283
+ false
263
284
  end
264
-
265
- false
266
285
  end
267
286
 
268
287
  # 尝试解析群聊中的 @ 消息
@@ -273,11 +292,11 @@ module WeChat::Bot
273
292
  # @param [String] message 原始消息
274
293
  # @return [String] 文本消息,如果不是 @ 消息返回原始消息
275
294
  def at_message(message)
276
- if match = AT_MESSAGE_REGEX.match(message)
277
- return match[2].strip
278
- end
279
-
280
- message
295
+ at_names = []
296
+ [
297
+ message.gsub(AT_MESSAGE_REGEX){ at_names << $1; "" },
298
+ at_names
299
+ ]
281
300
  end
282
301
  end
283
302
  end
@@ -1,5 +1,5 @@
1
1
  module WeChat
2
2
  module Bot
3
- VERSION = "0.1.2"
3
+ VERSION = "0.1.3"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wechat-bot2
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - icyleaf