Class: Wechat::Message
- Inherits:
-
Object
show all
- Defined in:
- lib/wechat/message.rb
Defined Under Namespace
Classes: ArticleBuilder, MpNewsArticleBuilder, NewsArticleBuilder
Constant Summary
collapse
- TEMPLATE_KEYS =
i[template_id form_id page color
emphasis_keyword topcolor url miniprogram data].freeze
- TO_JSON_KEY_MAP =
{
'ToUserName' => 'touser',
'ToWxName' => 'towxname',
'MediaId' => 'media_id',
'MpNews' => 'mpnews',
'ThumbMediaId' => 'thumb_media_id',
'TemplateId' => 'template_id',
'FormId' => 'form_id',
'ContentSourceUrl' => 'content_source_url',
'ShowCoverPic' => 'show_cover_pic'
}.freeze
- TO_JSON_ALLOWED =
%w[touser msgtype content image voice video file
music news articles template agentid filter
send_ignore_reprint mpnews towxname].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.
50
51
52
|
# File 'lib/wechat/message.rb', line 50
def initialize(message_hash)
@message_hash = message_hash || {}
end
|
Instance Attribute Details
#message_hash ⇒ Object
Returns the value of attribute message_hash.
48
49
50
|
# File 'lib/wechat/message.rb', line 48
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_users = '', towxname: nil, send_ignore_reprint: 0) ⇒ Object
8
9
10
11
12
13
14
15
16
|
# File 'lib/wechat/message.rb', line 8
def to(to_users = '', towxname: nil, send_ignore_reprint: 0)
if towxname.present?
new(ToWxName: towxname, CreateTime: Time.now.to_i)
elsif send_ignore_reprint == 1
new(ToUserName: to_users, CreateTime: Time.now.to_i, send_ignore_reprint: send_ignore_reprint)
else
new(ToUserName: to_users, CreateTime: Time.now.to_i)
end
end
|
.to_mass(tag_id: nil, send_ignore_reprint: 0) ⇒ Object
18
19
20
21
22
23
24
|
# File 'lib/wechat/message.rb', line 18
def to_mass(tag_id: nil, send_ignore_reprint: 0)
if tag_id
new(filter: { is_to_all: false, tag_id: tag_id }, send_ignore_reprint: send_ignore_reprint)
else
new(filter: { is_to_all: true }, send_ignore_reprint: send_ignore_reprint)
end
end
|
Instance Method Details
#[](key) ⇒ Object
54
55
56
|
# File 'lib/wechat/message.rb', line 54
def [](key)
message_hash[key]
end
|
#agent_id(agentid) ⇒ Object
99
100
101
|
# File 'lib/wechat/message.rb', line 99
def agent_id(agentid)
update(AgentId: agentid)
end
|
#as(type) ⇒ Object
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
# File 'lib/wechat/message.rb', line 78
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
132
133
134
|
# File 'lib/wechat/message.rb', line 132
def file(media_id)
update(MsgType: 'file', File: { MediaId: media_id })
end
|
#image(media_id) ⇒ Object
119
120
121
|
# File 'lib/wechat/message.rb', line 119
def image(media_id)
update(MsgType: 'image', Image: { MediaId: media_id })
end
|
#mpnews(collection, &_block) ⇒ Object
156
157
158
159
160
161
162
163
164
165
166
167
168
|
# File 'lib/wechat/message.rb', line 156
def mpnews(collection, &_block)
if block_given?
article = MpNewsArticleBuilder.new
collection.take(8).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(:thumb_media_id, :title, :content, :author, :content_source_url, :digest, :show_cover_pic).reject { |_k, v| v.nil? })
end
end
update(MsgType: 'mpnews', Articles: items.collect { |item| camelize_hash_keys(item) })
end
|
#music(thumb_media_id, music_url, opts = {}) ⇒ Object
136
137
138
139
|
# File 'lib/wechat/message.rb', line 136
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
# File 'lib/wechat/message.rb', line 141
def news(collection, &_block)
if block_given?
article = NewsArticleBuilder.new
collection.take(8).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
|
#ref_mpnews(media_id) ⇒ Object
170
171
172
|
# File 'lib/wechat/message.rb', line 170
def ref_mpnews(media_id)
update(MsgType: 'ref_mpnews', MpNews: { MediaId: media_id })
end
|
#reply ⇒ Object
58
59
60
61
62
63
64
65
|
# File 'lib/wechat/message.rb', line 58
def reply
Message.new(
ToUserName: message_hash[:FromUserName],
FromUserName: message_hash[:ToUserName],
CreateTime: Time.now.to_i,
WechatSession: session
)
end
|
#save_session ⇒ Object
72
73
74
75
76
|
# File 'lib/wechat/message.rb', line 72
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
229
230
231
232
233
|
# File 'lib/wechat/message.rb', line 229
def save_to!(model_class)
model = model_class.new(underscore_hash_keys(message_hash))
model.save!
self
end
|
#session ⇒ Object
67
68
69
70
|
# File 'lib/wechat/message.rb', line 67
def session
return nil unless Wechat.config.have_session_class
@message_hash[:WechatSession] ||= WechatSession.find_or_initialize_session(underscore_hash_keys(message_hash))
end
|
#success ⇒ Object
115
116
117
|
# File 'lib/wechat/message.rb', line 115
def success
update(MsgType: 'success')
end
|
#template(opts = {}) ⇒ Object
177
178
179
180
|
# File 'lib/wechat/message.rb', line 177
def template(opts = {})
template_fields = opts.symbolize_keys.slice(*TEMPLATE_KEYS)
update(MsgType: 'template', Template: template_fields)
end
|
#text(content) ⇒ Object
103
104
105
|
# File 'lib/wechat/message.rb', line 103
def text(content)
update(MsgType: 'text', Content: content)
end
|
#to(openid_or_userid) ⇒ Object
95
96
97
|
# File 'lib/wechat/message.rb', line 95
def to(openid_or_userid)
update(ToUserName: openid_or_userid)
end
|
#to_json ⇒ Object
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
|
# File 'lib/wechat/message.rb', line 205
def to_json
keep_camel_case_key = message_hash[:MsgType] == 'template'
json_hash = deep_recursive(message_hash) do |key, value|
key = key.to_s
[(TO_JSON_KEY_MAP[key] || (keep_camel_case_key ? key : key.downcase)), value]
end
json_hash = json_hash.transform_keys(&:downcase).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 'mpnews'
json_hash = { 'articles' => json_hash['articles'] }
when 'ref_mpnews'
json_hash['msgtype'] = 'mpnews'
json_hash.delete('articles')
when 'template'
json_hash = { 'touser' => json_hash['touser'] }.merge!(json_hash['template'])
end
JSON.generate(json_hash)
end
|
#to_xml ⇒ Object
182
183
184
185
186
187
|
# File 'lib/wechat/message.rb', line 182
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
107
108
109
110
111
112
113
|
# File 'lib/wechat/message.rb', line 107
def transfer_customer_service(kf_account = nil)
if kf_account
update(MsgType: 'transfer_customer_service', TransInfo: { KfAccount: kf_account })
else
update(MsgType: 'transfer_customer_service')
end
end
|
#video(media_id, opts = {}) ⇒ Object
127
128
129
130
|
# File 'lib/wechat/message.rb', line 127
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
123
124
125
|
# File 'lib/wechat/message.rb', line 123
def voice(media_id)
update(MsgType: 'voice', Voice: { MediaId: media_id })
end
|