class WeChat::Bot::Contact
微信联系人
可以是用户、公众号、群组等
Public Class Methods
new(bot)
click to toggle source
# File lib/wechat/bot/contact.rb, line 18 def initialize(bot) @bot = bot @data = {} end
parse(obj, bot)
click to toggle source
# File lib/wechat/bot/contact.rb, line 14 def self.parse(obj, bot) self.new(bot).parse(obj) end
Public Instance Methods
city()
click to toggle source
城市
# File lib/wechat/bot/contact.rb, line 64 def city attr(:city) end
displayname()
click to toggle source
群聊显示名
# File lib/wechat/bot/contact.rb, line 39 def displayname attr(:displayname) end
group?()
click to toggle source
是否群聊
# File lib/wechat/bot/contact.rb, line 74 def group? kind == Kind::Group end
kind()
click to toggle source
用户类型
# File lib/wechat/bot/contact.rb, line 54 def kind attr(:kind) end
members()
click to toggle source
群组成员列表
只有群组才有内容,根据 {#kind} 或 {#group?} 来判断 不是群组类型的返回空数组
@return [Hash]
# File lib/wechat/bot/contact.rb, line 89 def members attr(:members) end
mp?()
click to toggle source
是否公众号
# File lib/wechat/bot/contact.rb, line 79 def mp? kind == Kind::MP end
nickname()
click to toggle source
用户昵称
# File lib/wechat/bot/contact.rb, line 29 def nickname attr(:nickname) end
parse(raw, update = false)
click to toggle source
联系人解析
@param [Hash<Object, Object>] raw @return [Contact]
# File lib/wechat/bot/contact.rb, line 97 def parse(raw, update = false) @raw = raw parse_kind parse_members @raw.each do |key, value| if attribute = mapping[key] next if value.to_s.empty? && update sync(attribute, value) end end self end
province()
click to toggle source
省份
# File lib/wechat/bot/contact.rb, line 59 def province attr(:province) end
remarkname()
click to toggle source
备注名
# File lib/wechat/bot/contact.rb, line 34 def remarkname attr(:remarkname) end
sex()
click to toggle source
性别
# File lib/wechat/bot/contact.rb, line 44 def sex attr(:sex) end
signature()
click to toggle source
个人签名
# File lib/wechat/bot/contact.rb, line 49 def signature attr(:signature) end
special?()
click to toggle source
是否特殊账户
# File lib/wechat/bot/contact.rb, line 69 def special? kind == Kind::Special end
to_s()
click to toggle source
# File lib/wechat/bot/contact.rb, line 119 def to_s "#<#{self.class}:#{object_id.to_s(16)} username='#{username}' nickname='#{nickname}' kind='#{kind}'>" end
update(raw)
click to toggle source
# File lib/wechat/bot/contact.rb, line 114 def update(raw) @raw = raw parse(@raw, true) end
username()
click to toggle source
用户唯一 ID
# File lib/wechat/bot/contact.rb, line 24 def username attr(:username) end
Private Instance Methods
attr(attribute, data = false)
click to toggle source
获取属性
@param [Symbol] attribute @param [Boolean] data 默认 false @return [String, Integer, Hash]
# File lib/wechat/bot/contact.rb, line 151 def attr(attribute, data = false) if data @data[attribute.to_sym] else instance_variable_get("@#{attribute}") end end
mapping()
click to toggle source
字段映射
@return [Hash<String, String>]
# File lib/wechat/bot/contact.rb, line 201 def mapping { "NickName" => "nickname", "UserName" => "username", "RemarkName" => "remarkname", "DisplayName" => "displayname", "Signature" => "signature", "Sex" => "sex", "Province" => "province", "City" => 'city' } end
parse_kind()
click to toggle source
解析联系人类型
详见 {Contact::Kind} 成员变量 @return [void]
# File lib/wechat/bot/contact.rb, line 163 def parse_kind kind = if @bot.config.special_users.include?(@raw["UserName"]) # 特殊账户 Kind::Special elsif @raw["UserName"].include?("@@") # 群聊 Kind::Group elsif @raw["VerifyFlag"] && (@raw["VerifyFlag"] & 8) != 0 # 公众号 Kind::MP else # 普通用户 Kind::User end sync(:kind, kind) end
parse_members()
click to toggle source
解析群组成员列表
只有群组才有内容,根据 {#kind} 或 {#group?} 来判断
@return [void]
# File lib/wechat/bot/contact.rb, line 186 def parse_members members = [] if @raw["MemberList"] @raw["MemberList"].each do |m| members.push(Contact.parse(m, @bot)) end end sync(:members, members) end
sync(attribute, value, data = false)
click to toggle source
更新或新写入变量值
@param [Symbol] attribute @param [String, Integer, Hash] value @param [Boolean] data @return [void]
# File lib/wechat/bot/contact.rb, line 131 def sync(attribute, value, data = false) value = value.convert_emoji if attribute.to_sym == :nickname # 满足群组类型且 nickname 为空时补充一个默认的群组名(参考微信 App 设计) if attribute.to_sym == :nickname && value.to_s.empty? && @kind == Kind::Group value = members.map { |m| m.nickname }.join("、") end if data @data[attribute.to_sym] = value else instance_variable_set("@#{attribute}", value) end end