Class: WeChat::Bot::Message

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#botCore (readonly)

Returns:



35
36
37
# File 'lib/wechat/bot/message.rb', line 35

def bot
  @bot
end

#eventsArray<Symbol> (readonly)

事件列表

Returns:

  • (Array<Symbol>)


32
33
34
# File 'lib/wechat/bot/message.rb', line 32

def events
  @events
end

#fromContact (readonly)

消息发送者

用户或者群组

Returns:



52
53
54
# File 'lib/wechat/bot/message.rb', line 52

def from
  @from
end

#from_userObject (readonly)

群组消息时为消息用户,其余为from



54
55
56
# File 'lib/wechat/bot/message.rb', line 54

def from_user
  @from_user
end

#kindMessage::Kind (readonly)

消息类型

Returns:



42
43
44
# File 'lib/wechat/bot/message.rb', line 42

def kind
  @kind
end

#media_idObject (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

#messageString (readonly)

消息正文

Returns:



58
59
60
# File 'lib/wechat/bot/message.rb', line 58

def message
  @message
end

#meta_dataObject (readonly)

Returns the value of attribute meta_data.



62
63
64
# File 'lib/wechat/bot/message.rb', line 62

def 
  @meta_data
end

#rawHash<Object, Object> (readonly)

原始消息

Returns:

  • (Hash<Object, Object>)


28
29
30
# File 'lib/wechat/bot/message.rb', line 28

def raw
  @raw
end

#sourceContact::Kind (readonly)

消息来源

Returns:



46
47
48
# File 'lib/wechat/bot/message.rb', line 46

def source
  @source
end

#timeTime (readonly)

Returns:

  • (Time)


38
39
40
# File 'lib/wechat/bot/message.rb', line 38

def time
  @time
end

Instance Method Details

#at_membersObject



153
154
155
156
157
158
159
# File 'lib/wechat/bot/message.rb', line 153

def at_members
  if at_message?
    @at_message_names.map{|nickname| from.find_member(nickname: nickname) }
  else
    []
  end
end

#at_message?Boolean

Returns:

  • (Boolean)


149
150
151
# File 'lib/wechat/bot/message.rb', line 149

def at_message?
  !( @at_message_names.nil? || @at_message_names.empty? )
end

#match(regexp, type) ⇒ MatchData

消息匹配

Parameters:

Returns:

  • (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

#parsevoid

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"])

  message = @raw["Content"].convert_emoji
  message = CGI.unescape_html(message) if @kinde != Message::Kind::Text

  if match = group_message(message)
    message = match[1]
    @from_user = @from.find_member(username: match[0]) unless @from.nil?
    @at_message_names = match[2]
  else
    @from_user = @from
  end

  @message = 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]

  message_type = 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(message_type, to_user, text)
end