Class: WeChat::Bot::Message
- Inherits:
-
Object
- Object
- WeChat::Bot::Message
- Defined in:
- lib/wechat/bot/message.rb
Overview
微信消息
Defined Under Namespace
Modules: Kind
Constant Summary collapse
- GROUP_MESSAGE_REGEX =
/^(@\w+):<br\/>(.*)$/- AT_MESSAGE_REGEX =
/@([^\s]+) (.*)/
Instance Attribute Summary collapse
- #bot ⇒ Core readonly
-
#events ⇒ Array<Symbol>
readonly
事件列表.
-
#from ⇒ Contact
readonly
消息发送者.
-
#kind ⇒ Message::Kind
readonly
消息类型.
-
#media_id ⇒ Object
readonly
Returns the value of attribute media_id.
-
#message ⇒ String
readonly
消息正文.
-
#meta_data ⇒ Object
readonly
Returns the value of attribute meta_data.
-
#raw ⇒ Hash<Object, Object>
readonly
原始消息.
-
#source ⇒ Contact::Kind
readonly
消息来源.
- #time ⇒ Time readonly
Instance Method Summary collapse
-
#at_message(message) ⇒ String
尝试解析群聊中的 @ 消息.
- #at_message? ⇒ Boolean
-
#group_message(message) ⇒ Array<Object>
解析用户的群消息.
-
#initialize(raw, bot) ⇒ Message
constructor
A new instance of Message.
-
#match(regexp, type) ⇒ MatchData
消息匹配.
-
#parse ⇒ void
解析微信消息.
-
#parse_emoticon ⇒ void
解析表情.
-
#parse_events ⇒ void
解析 Handler 的事件.
-
#parse_kind ⇒ void
解析消息类型.
- #parse_share ⇒ Object
-
#parse_source ⇒ void
解析消息来源.
-
#reply(text, **args) ⇒ Object
回复消息.
Constructor Details
#initialize(raw, bot) ⇒ Message
62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/wechat/bot/message.rb', line 62 def initialize(raw, bot) @raw = raw @bot = bot @events = [] @time = Time.now @statusmsg_mode = nil parse @bot.logger.verbose "Message Raw: #{@raw}" end |
Instance Attribute Details
#bot ⇒ Core (readonly)
35 36 37 |
# File 'lib/wechat/bot/message.rb', line 35 def bot @bot end |
#events ⇒ Array<Symbol> (readonly)
事件列表
32 33 34 |
# File 'lib/wechat/bot/message.rb', line 32 def events @events end |
#from ⇒ Contact (readonly)
消息发送者
用户或者群组
52 53 54 |
# File 'lib/wechat/bot/message.rb', line 52 def from @from end |
#kind ⇒ Message::Kind (readonly)
消息类型
42 43 44 |
# File 'lib/wechat/bot/message.rb', line 42 def kind @kind end |
#media_id ⇒ Object (readonly)
Returns the value of attribute media_id.
58 59 60 |
# File 'lib/wechat/bot/message.rb', line 58 def media_id @media_id end |
#meta_data ⇒ Object (readonly)
Returns the value of attribute meta_data.
60 61 62 |
# File 'lib/wechat/bot/message.rb', line 60 def end |
#raw ⇒ Hash<Object, Object> (readonly)
原始消息
28 29 30 |
# File 'lib/wechat/bot/message.rb', line 28 def raw @raw end |
#source ⇒ Contact::Kind (readonly)
消息来源
46 47 48 |
# File 'lib/wechat/bot/message.rb', line 46 def source @source end |
#time ⇒ Time (readonly)
38 39 40 |
# File 'lib/wechat/bot/message.rb', line 38 def time @time end |
Instance Method Details
#at_message(message) ⇒ String
尝试解析群聊中的 @ 消息
群消息格式:
@ToNickNameUserName Message
275 276 277 278 279 280 281 |
# File 'lib/wechat/bot/message.rb', line 275 def () if match = AT_MESSAGE_REGEX.match() return match[2].strip end end |
#at_message? ⇒ Boolean
141 142 143 |
# File 'lib/wechat/bot/message.rb', line 141 def @at_mesage == true end |
#group_message(message) ⇒ Array<Object>
解析用户的群消息
群消息格式:
@FromUserName:<br>Message
260 261 262 263 264 265 266 |
# File 'lib/wechat/bot/message.rb', line 260 def () if match = GROUP_MESSAGE_REGEX.match() return [match[1], (match[2])] end false end |
#match(regexp, type) ⇒ MatchData
消息匹配
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/wechat/bot/message.rb', line 122 def match(regexp, type) # text = "" # case type # when :ctcp # text = ctcp_message # when :action # text = action_message # else # text = message.to_s # type = :other # end # if strip_colors # text = Cinch::Formatting.unformat(text) # end .match(regexp) end |
#parse ⇒ void
This method returns an undefined value.
解析微信消息
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/wechat/bot/message.rb', line 93 def parse parse_source parse_kind = @raw["Content"].convert_emoji = CGI.unescape_html() if @kinde != Message::Kind::Text if match = () # from_username = match[0] = match[1] end = # TODO: 来自于特殊账户无法获取联系人信息,需要单独处理 @from = @bot.contact_list.find(username: @raw["FromUserName"]) parse_emoticon if @kind == Message::Kind::Emoticon case @kind when Message::Kind::ShareCard = MessageData::ShareCard.parse() end parse_events end |
#parse_emoticon ⇒ void
This method returns an undefined value.
解析表情
表情分为两种:
1.
235 236 237 238 239 240 241 242 243 244 |
# File 'lib/wechat/bot/message.rb', line 235 def parse_emoticon if .empty? @media_id = @raw["MediaId"] # TODO: 解决微信商店表情 # file = @bot.client.download_image(@raw["NewMsgId"]) else data = MultiXml.parse() @media_id = data["msg"]["emoji"]["md5"] end end |
#parse_events ⇒ void
This method returns an undefined value.
解析 Handler 的事件
- `:message`
216 217 218 219 220 221 222 223 224 225 226 |
# File 'lib/wechat/bot/message.rb', line 216 def parse_events @events << :message @events << @kind @events << @source @at_mesage = false if @source == :group && @raw["Content"] =~ /@([^\s]+)\s+(.*)/ @events << :at_message @at_mesage = true end end |
#parse_kind ⇒ void
This method returns an undefined value.
解析消息类型
- 1: Text
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/wechat/bot/message.rb', line 180 def parse_kind @kind = case @raw["MsgType"] when 1 Message::Kind::Text when 3 Message::Kind::Image when 34 Message::Kind::Voice when 37 Message::Kind::Verify when 42 Message::Kind::BusinessCard when 62 Message::Kind::ShortVideo when 47 Message::Kind::Emoticon when 49 Message::Kind::ShareCard when 10000 Message::Kind::System else Message::Kind::Unkown end end |
#parse_share ⇒ Object
246 247 248 249 |
# File 'lib/wechat/bot/message.rb', line 246 def parse_share # TODO: 完成解析 data = MultiXml.parse() end |
#parse_source ⇒ void
This method returns an undefined value.
解析消息来源
特殊账户/群聊/公众号/用户
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/wechat/bot/message.rb', line 150 def parse_source @source = if @bot.config.special_users.include?(@raw["FromUserName"]) # 特殊账户 Contact::Kind::Special elsif @raw["FromUserName"].include?("@@") # 群聊 Contact::Kind::Group elsif (@raw["RecommendInfo"]["VerifyFlag"] & 8) != 0 # 公众号 Contact::Kind::MP else # 普通用户 Contact::Kind::User end end |
#reply(text, **args) ⇒ Object
回复消息
76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/wechat/bot/message.rb', line 76 def reply(text, **args) to_user = args[:username] || @from.username to_user = @bot.contact_list.find(nickname: args[:nickname]) if args[:nickname] = args[:type] || :text # if @bot.config.special_users.include?(to_user) && to_user != 'filehelper' # @bot.logger.error "特殊账户无法回复: #{to_user}" # raise NoReplyException, "特殊账户无法回复: #{to_user}" # end @bot.client.send(, to_user, text) end |