Class: Wechat::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/wechat/message.rb

Defined Under Namespace

Classes: ArticleBuilder

Constant Summary collapse

TO_JSON_KEY_MAP =
{
  'ToUserName' => 'touser',
  'MediaId' => 'media_id',
  'ThumbMediaId' => 'thumb_media_id',
  'TemplateId' => 'template_id'
}.freeze
TO_JSON_ALLOWED =
%w(touser msgtype content image voice video file music news articles template agentid).freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message_hash) ⇒ Message

Returns a new instance of Message.



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

def initialize(message_hash)
  @message_hash = message_hash || {}
end

Instance Attribute Details

#message_hashObject (readonly)

Returns the value of attribute message_hash.



25
26
27
# File 'lib/wechat/message.rb', line 25

def message_hash
  @message_hash
end

Class Method Details

.from_hash(message_hash) ⇒ Object



4
5
6
# File 'lib/wechat/message.rb', line 4

def from_hash(message_hash)
  new(message_hash)
end

.to(to_user) ⇒ Object



8
9
10
# File 'lib/wechat/message.rb', line 8

def to(to_user)
  new(ToUserName: to_user, CreateTime: Time.now.to_i)
end

Instance Method Details

#[](key) ⇒ Object



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

def [](key)
  message_hash[key]
end

#agent_id(agentid) ⇒ Object



76
77
78
# File 'lib/wechat/message.rb', line 76

def agent_id(agentid)
  update(AgentId: agentid)
end

#as(type) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/wechat/message.rb', line 55

def as(type)
  case type
  when :text
    message_hash[:Content]

  when :image, :voice, :video
    Wechat.api.media(message_hash[:MediaId])

  when :location
    message_hash.slice(:Location_X, :Location_Y, :Scale, :Label).each_with_object({}) do |value, results|
      results[value[0].to_s.underscore.to_sym] = value[1]
    end
  else
    raise "Don't know how to parse message as #{type}"
  end
end

#file(media_id) ⇒ Object



109
110
111
# File 'lib/wechat/message.rb', line 109

def file(media_id)
  update(MsgType: 'file', File: { MediaId: media_id })
end

#image(media_id) ⇒ Object



96
97
98
# File 'lib/wechat/message.rb', line 96

def image(media_id)
  update(MsgType: 'image', Image: { MediaId: media_id })
end

#music(thumb_media_id, music_url, opts = {}) ⇒ Object



113
114
115
116
# File 'lib/wechat/message.rb', line 113

def music(thumb_media_id, music_url, opts = {})
  music_fields = camelize_hash_keys(opts.slice(:title, :description, :HQ_music_url).merge(music_url: music_url, thumb_media_id: thumb_media_id))
  update(MsgType: 'music', Music: music_fields)
end

#news(collection, &_block) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/wechat/message.rb', line 118

def news(collection, &_block)
  if block_given?
    article = ArticleBuilder.new
    collection.take(10).each_with_index { |item, index| yield(article, item, index) }
    items = article.items
  else
    items = collection.collect do |item|
      camelize_hash_keys(item.symbolize_keys.slice(:title, :description, :pic_url, :url).reject { |_k, v| v.nil? })
    end
  end

  update(MsgType: 'news', ArticleCount: items.count,
         Articles: items.collect { |item| camelize_hash_keys(item) })
end

#replyObject



35
36
37
38
39
40
41
42
# File 'lib/wechat/message.rb', line 35

def reply
  Message.new(
    ToUserName: message_hash[:FromUserName],
    FromUserName: message_hash[:ToUserName],
    CreateTime: Time.now.to_i,
    WechatSession: session
  )
end

#save_sessionObject



49
50
51
52
53
# File 'lib/wechat/message.rb', line 49

def save_session
  ws = message_hash.delete(:WechatSession)
  ws.try(:save_session, underscore_hash_keys(message_hash))
  @message_hash[:WechatSession] = ws
end

#save_to!(model_class) ⇒ Object



172
173
174
175
176
# File 'lib/wechat/message.rb', line 172

def save_to!(model_class)
  model = model_class.new(underscore_hash_keys(message_hash))
  model.save!
  self
end

#sessionObject



44
45
46
47
# File 'lib/wechat/message.rb', line 44

def session
  return nil unless Wechat.config.have_session_class
  @message_hash[:WechatSession] ||= WechatSession.find_or_initialize_session(underscore_hash_keys(message_hash))
end

#successObject



92
93
94
# File 'lib/wechat/message.rb', line 92

def success
  update(MsgType: 'success')
end

#template(opts = {}) ⇒ Object



133
134
135
136
# File 'lib/wechat/message.rb', line 133

def template(opts = {})
  template_fields = camelize_hash_keys(opts.symbolize_keys.slice(:template_id, :topcolor, :url, :data))
  update(MsgType: 'template', Template: template_fields)
end

#text(content) ⇒ Object



80
81
82
# File 'lib/wechat/message.rb', line 80

def text(content)
  update(MsgType: 'text', Content: content)
end

#to(openid_or_userid) ⇒ Object



72
73
74
# File 'lib/wechat/message.rb', line 72

def to(openid_or_userid)
  update(ToUserName: openid_or_userid)
end

#to_jsonObject



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/wechat/message.rb', line 154

def to_json
  json_hash = deep_recursive(message_hash) do |key, value|
    key = key.to_s
    [(TO_JSON_KEY_MAP[key] || key.downcase), value]
  end

  json_hash = json_hash.select { |k, _v| TO_JSON_ALLOWED.include? k }
  case json_hash['msgtype']
  when 'text'
    json_hash['text'] = { 'content' => json_hash.delete('content') }
  when 'news'
    json_hash['news'] = { 'articles' => json_hash.delete('articles') }
  when 'template'
    json_hash = { 'touser' => json_hash['touser'] }.merge!(json_hash['template'])
  end
  JSON.generate(json_hash)
end

#to_xmlObject



138
139
140
141
142
143
# File 'lib/wechat/message.rb', line 138

def to_xml
  ws = message_hash.delete(:WechatSession)
  xml = message_hash.to_xml(root: 'xml', children: 'item', skip_instruct: true, skip_types: true)
  @message_hash[:WechatSession] = ws
  xml
end

#transfer_customer_service(kf_account = nil) ⇒ Object



84
85
86
87
88
89
90
# File 'lib/wechat/message.rb', line 84

def transfer_customer_service( = nil)
  if 
    update(MsgType: 'transfer_customer_service', TransInfo: { KfAccount:  })
  else
    update(MsgType: 'transfer_customer_service')
  end
end

#video(media_id, opts = {}) ⇒ Object



104
105
106
107
# File 'lib/wechat/message.rb', line 104

def video(media_id, opts = {})
  video_fields = camelize_hash_keys({ media_id: media_id }.merge(opts.slice(:title, :description)))
  update(MsgType: 'video', Video: video_fields)
end

#voice(media_id) ⇒ Object



100
101
102
# File 'lib/wechat/message.rb', line 100

def voice(media_id)
  update(MsgType: 'voice', Voice: { MediaId: media_id })
end