Skip to content

Commit

Permalink
Merge pull request #300 from zlei1/master
Browse files Browse the repository at this point in the history
New material_add_news API
  • Loading branch information
Eric-Guo committed Apr 21, 2021
2 parents 8195bb0 + 888c816 commit f31241d
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 1 deletion.
1 change: 1 addition & 0 deletions README-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,7 @@ Wechat Public Account commands:
wechat groups # 查询所有分组
wechat material_get [MEDIA_ID, PATH] # 永久媒体下载
wechat material_add [MEDIA_TYPE, PATH] # 永久媒体上传
wechat material_add_news [MPNEWS_YAML_PATH] # 永久图文素材上传
wechat material_count # 获取永久素材总数
wechat material_delete [MEDIA_ID] # 删除永久素材
wechat material_list [TYPE, OFFSET, COUNT] # 获取永久素材列表
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -395,8 +395,9 @@ Wechat Public Account commands:
wechat group_delete [GROUP_ID] # 删除分组
wechat group_update [GROUP_ID, NEW_GROUP_NAME] # 修改分组名
wechat groups # 查询所有分组
wechat material_get [MEDIA_ID, PATH] # 永久媒体下载
wechat material_get [MEDIA_ID, PATH] # 永久媒体下载
wechat material_add [MEDIA_TYPE, PATH] # 永久媒体上传
wechat material_add_news [MPNEWS_YAML_PATH] # 永久图文素材上传
wechat material_count # 获取永久素材总数
wechat material_delete [MEDIA_ID] # 删除永久素材
wechat material_list [TYPE, OFFSET, COUNT] # 获取永久素材列表
Expand Down
6 changes: 6 additions & 0 deletions bin/wechat
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,12 @@ class App < Thor
puts wechat_api.material_add(type, path)
end

desc 'material_add_news [MPNEWS_YAML_PATH]', '永久图文素材上传'
def material_add_news(mpnews_yaml_path)
new = YAML.load(File.read(mpnews_yaml_path))
puts wechat_api.material_add_news(Wechat::Message.new(MsgType: 'mpnews').mpnews(new['articles']))
end

desc 'material_delete [MEDIA_ID]', '删除永久素材'
def material_delete(media_id)
puts wechat_api.material_delete(media_id)
Expand Down
4 changes: 4 additions & 0 deletions lib/wechat/concern/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ def material_add(type, file)
post_file 'material/add_material', file, params: { type: type }
end

def material_add_news(mpnews_message)
post 'material/add_news', mpnews_message.to_json
end

def material_delete(media_id)
post 'material/del_material', JSON.generate(media_id: media_id)
end
Expand Down
40 changes: 40 additions & 0 deletions spec/lib/wechat/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,46 @@
end
end

describe '#material_add_news' do
let(:items) do
[
{ thumb_media_id: 'qI6_Ze_6PtV7svjolgs-rN6stStuHIjs9_DidOHaj0Q-mwvBelOXCFZiq2OsIU-p',
author: 'xxx', title: 'Happy Day', content_source_url: 'www.qq.com',
content: 'content', digest: 'digest', show_cover_pic: 1 },
{ thumb_media_id: 'qI6_Ze_6PtV7svjolgs-rN6stStuHIjs9_DidOHaj0Q-mwvBelOXCFZiq2OsIU-p',
author: 'xxx', title: 'Happy Day', content_source_url: 'www.qq.com',
content: 'content', digest: 'digest', show_cover_pic: 0 }
]
end
specify 'will post material/media_add_news with access_token and mpnews in json' do
mpnews = {
articles: [
{
thumb_media_id: 'qI6_Ze_6PtV7svjolgs-rN6stStuHIjs9_DidOHaj0Q-mwvBelOXCFZiq2OsIU-p',
title: 'Happy Day',
content: 'content',
author: 'xxx',
content_source_url: 'www.qq.com',
digest: 'digest',
show_cover_pic: 1
},
{
thumb_media_id: 'qI6_Ze_6PtV7svjolgs-rN6stStuHIjs9_DidOHaj0Q-mwvBelOXCFZiq2OsIU-p',
title: 'Happy Day',
content: 'content',
author: 'xxx',
content_source_url: 'www.qq.com',
digest: 'digest',
show_cover_pic: 0
}
]
}
result = { type: 'news', media_id: 'CsEf3ldqkAYJAU6EJeIkStVDSvffUJ54vqbThMgplD-VJXXof6ctX5fI6-aYyUiQ', created_at: 1391857799 }
expect(subject.client).to receive(:post).with('material/add_news', mpnews.to_json, params: { access_token: 'access_token' }).and_return(result)
expect(subject.material_add_news(Wechat::Message.new(MsgType: 'mpnews').mpnews(items))).to eq(result)
end
end

describe '#material_delete' do
specify 'will post material/del_material with access_token and media_id in payload' do
media_id = 'media_id'
Expand Down

0 comments on commit f31241d

Please sign in to comment.