Skip to content

Commit

Permalink
New message_mass_preview command line.
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric-Guo committed Mar 13, 2017
1 parent d2fb08c commit 2a1c6bb
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 5 deletions.
1 change: 1 addition & 0 deletions README-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ Wechat Public Account commands:
wechat menu_delete # 删除菜单
wechat menu_trymatch [USER_ID] # 测试个性化菜单匹配结果
wechat message_mass_delete [MSG_ID] # 删除群发消息
wechat message_mass_preview [WX_NAME, MPNEWS_MEDIA_ID] # 预览图文消息素材
wechat qrcode_create_limit_scene [SCENE_ID_OR_STR] # 请求永久二维码
wechat qrcode_create_scene [SCENE_ID, EXPIRE_SECONDS] # 请求临时二维码
wechat qrcode_download [TICKET, QR_CODE_PIC_PATH] # 通过ticket下载二维码
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ Wechat Public Account commands:
wechat menu_delete # 删除菜单
wechat menu_trymatch [USER_ID] # 测试个性化菜单匹配结果
wechat message_mass_delete [MSG_ID] # 删除群发消息
wechat message_mass_preview [WX_NAME, MPNEWS_MEDIA_ID] # 预览图文消息素材
wechat qrcode_create_limit_scene [SCENE_ID_OR_STR] # 请求永久二维码
wechat qrcode_create_scene [SCENE_ID, EXPIRE_SECONDS] # 请求临时二维码
wechat qrcode_download [TICKET, QR_CODE_PIC_PATH] # 通过ticket下载二维码
Expand Down
5 changes: 5 additions & 0 deletions bin/wechat
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,11 @@ class App < Thor
puts wechat_api.message_mass_delete(msg_id)
end

desc 'message_mass_preview [WX_NAME, MPNEWS_MEDIA_ID]', '预览图文消息素材'
def message_mass_preview(wx_name, mpnews_media_id)
puts wechat_api.message_mass_preview(Wechat::Message.to(towxname: wx_name).ref_mpnews(mpnews_media_id))
end

desc 'group_create [GROUP_NAME]', '创建分组'
def group_create(group_name)
puts wechat_api.group_create(group_name)
Expand Down
13 changes: 8 additions & 5 deletions lib/wechat/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ def from_hash(message_hash)
new(message_hash)
end

def to(to_users, send_ignore_reprint: 0)
if send_ignore_reprint == 0
new(ToUserName: to_users, CreateTime: Time.now.to_i)
else
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

Expand Down Expand Up @@ -183,6 +185,7 @@ def to_xml

TO_JSON_KEY_MAP = {
'ToUserName' => 'touser',
'ToWxName' => 'towxname',
'MediaId' => 'media_id',
'MpNews' => 'mpnews',
'ThumbMediaId' => 'thumb_media_id',
Expand All @@ -191,7 +194,7 @@ def to_xml
'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).freeze
TO_JSON_ALLOWED = %w(touser msgtype content image voice video file music news articles template agentid filter send_ignore_reprint mpnews towxname).freeze

def to_json
keep_camel_case_key = message_hash[:MsgType] == 'template'
Expand Down
8 changes: 8 additions & 0 deletions spec/lib/wechat/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,14 @@
expect(subject.client).to receive(:post).with('message/mass/preview', ref_mpnews_to_openid.to_json, params: { access_token: 'access_token' }).and_return(result)
expect(subject.message_mass_preview(Wechat::Message.to('OPENID').ref_mpnews('123dsdajkasd231jhksad'))).to eq(result)
end
specify 'will post message/mass/preview with access_token, towxname and mpnews media id in json' do
ref_mpnews_to_openid = { towxname: '示例的微信号',
msgtype: 'mpnews',
mpnews: { media_id: '123dsdajkasd231jhksad' } }
result = { errcode: 0, errmsg: 'preview success', msg_id: 34182 }
expect(subject.client).to receive(:post).with('message/mass/preview', ref_mpnews_to_openid.to_json, params: { access_token: 'access_token' }).and_return(result)
expect(subject.message_mass_preview(Wechat::Message.to(towxname: '示例的微信号').ref_mpnews('123dsdajkasd231jhksad'))).to eq(result)
end
end

describe '#wxa_create_qrcode' do
Expand Down

0 comments on commit 2a1c6bb

Please sign in to comment.