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
消息发送者.
-
#from_user ⇒ Object
readonly
群组消息时为消息用户,其余为from.
-
#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_members ⇒ Object
- #at_message? ⇒ Boolean
-
#initialize(raw, bot) ⇒ Message
constructor
A new instance of Message.
-
#match(regexp, type) ⇒ MatchData
消息匹配.
-
#parse ⇒ void
解析微信消息.
-
#reply(text, **args) ⇒ Object
回复消息.
Constructor Details
#initialize(raw, bot) ⇒ Message
Returns a new instance of Message.
64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/wechat/bot/message.rb', line 64 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
#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 |
#from_user ⇒ Object (readonly)
群组消息时为消息用户,其余为from
54 55 56 |
# File 'lib/wechat/bot/message.rb', line 54 def from_user @from_user 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.
60 61 62 |
# File 'lib/wechat/bot/message.rb', line 60 def media_id @media_id end |
#message ⇒ String (readonly)
消息正文
58 59 60 |
# File 'lib/wechat/bot/message.rb', line 58 def @message end |
#meta_data ⇒ Object (readonly)
Returns the value of attribute meta_data.
62 63 64 |
# File 'lib/wechat/bot/message.rb', line 62 def @meta_data 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_members ⇒ Object
153 154 155 156 157 158 159 |
# File 'lib/wechat/bot/message.rb', line 153 def at_members if @at_message_names.map{|nickname| from.find_member(nickname: nickname) } else [] end end |
#at_message? ⇒ Boolean
149 150 151 |
# File 'lib/wechat/bot/message.rb', line 149 def !( @at_message_names.nil? || @at_message_names.empty? ) end |
#match(regexp, type) ⇒ MatchData
消息匹配
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/wechat/bot/message.rb', line 130 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 @message.match(regexp) end |
#parse ⇒ void
This method returns an undefined value.
解析微信消息
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/wechat/bot/message.rb', line 95 def parse parse_source parse_kind # TODO: 来自于特殊账户无法获取联系人信息,需要单独处理 @from = @bot.contact_list.find(username: @raw["FromUserName"]) = @raw["Content"].convert_emoji = CGI.unescape_html() if @kinde != Message::Kind::Text if match = () = match[1] @from_user = @from.find_member(username: match[0]) unless @from.nil? @at_message_names = match[2] else @from_user = @from end @message = case @kind when Message::Kind::Emoticon parse_emoticon when Message::Kind::ShareCard @meta_data = MessageData::ShareCard.parse(@message) end parse_events end |
#reply(text, **args) ⇒ Object
回复消息
78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/wechat/bot/message.rb', line 78 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 |