Commit aea6f101c91615fa8b2a6dd6f78f764588f6e10d

Authored by lanrion
1 parent b2e4a7e0
Exists in master

更新管理媒体接口API

README.md
... ... @@ -146,6 +146,35 @@ group_client.media.upload(image_jpg_file, "image")
146 146 # 获取下载链接
147 147 # 返回一个URL,请开发者自行使用此url下载
148 148 group_client.media.get_media_by_id(media_id)
  149 +
  150 +# 上传永久图文素材
  151 +# articles 为图文列表:
  152 +{
  153 + "title": "Title01",
  154 + "thumb_media_id": "2-G6nrLmr5EC3MMb_-zK1dDdzmd0p7cNliYu9V5w7o8K0",
  155 + "author": "zs",
  156 + "content_source_url": "",
  157 + "content": "Content001",
  158 + "digest": "airticle01",
  159 + "show_cover_pic": "0"
  160 +}
  161 +group_client.material.add_mpnews(agent_id, articles)
  162 +# 更新图文素材
  163 +group_client.material.update_mpnews(agent_id, media_id, articles=[])
  164 +
  165 +# 上传其他类型永久素材
  166 +# type: "image", "voice", "video", "file"
  167 +# file: File
  168 +group_client.material.add_material(agent_id, type, file)
  169 +
  170 +# 删除永久素材
  171 +group_client.material.del(agent_id, media_id)
  172 +
  173 +# 获取素材总数
  174 +group_client.material.get_count(agent_id)
  175 +
  176 +# 获取素材列表
  177 +group_client.material.list(agent_id, type, offset, count=20)
149 178 ```
150 179  
151 180 ## 第三方应用
... ...
lib/qy_wechat_api.rb
... ... @@ -19,10 +19,10 @@ module QyWechatApi
19 19 autoload(:ObjectStorage, "qy_wechat_api/storage/object_storage")
20 20 autoload(:RedisStorage, "qy_wechat_api/storage/redis_storage")
21 21  
22   - ENDPOINT_URL = "https://qyapi.weixin.qq.com/cgi-bin".freeze
  22 + ENDPOINT_URL = "https://qyapi.weixin.qq.com/cgi-bin".freeze
23 23 SUITE_ENDPOINT = "https://qy.weixin.qq.com/cgi-bin".freeze
24   - OK_MSG = "ok".freeze
25   - OK_CODE = 0.freeze
  24 + OK_MSG = "ok".freeze
  25 + OK_CODE = 0.freeze
26 26  
27 27 class << self
28 28  
... ...
lib/qy_wechat_api/api/material.rb 0 → 100644
... ... @@ -0,0 +1,64 @@
  1 +# encoding: utf-8
  2 +
  3 +module QyWechatApi
  4 + module Api
  5 + class Material < Base
  6 +
  7 + material_types
  8 + MATERIAL_TYPES = ["image", "voice", "video", "file"]
  9 +
  10 + # 上传永久图文素材
  11 + # https://qyapi.weixin.qq.com/cgi-bin/material/add_mpnews?access_token=ACCESS_TOKEN
  12 + def add_mpnews(agent_id, articles=[])
  13 + payload = {agentid: agent_id, mpnews: {articles: articles}}
  14 + http_post("add_mpnews", payload)
  15 + end
  16 +
  17 + # 修改永久图文素材
  18 + def update_mpnews(agent_id, media_id, articles=[])
  19 + payload = {agentid: agent_id, media_id: media_id, mpnews: {articles: articles}}
  20 + http_post("update_mpnews", payload)
  21 + end
  22 +
  23 +
  24 + # 上传其他类型永久素材
  25 + # https://qyapi.weixin.qq.com/cgi-bin/material/add_material?agentid=AGENTID&type=TYPE&access_token=ACCESS_TOKEN
  26 + def add_material(agent_id, type, file)
  27 + check_masterial_type(type)
  28 + http_post("upload", {media: file}, {type: type, agentid: agent_id})
  29 + end
  30 +
  31 + # 删除永久素材
  32 + # https://qyapi.weixin.qq.com/cgi-bin/material/del?access_token=ACCESS_TOKEN&agentid=AGENTID&media_id=MEDIA_ID
  33 + def del(agent_id, media_id)
  34 + http_get("del", {agent_id: agent_id, media_id: media_id})
  35 + end
  36 +
  37 + # 获取素材总数
  38 + # https://qyapi.weixin.qq.com/cgi-bin/material/get_count?access_token=ACCESS_TOKEN&agentid=AGENTID
  39 + def get_count(agent_id)
  40 + http_get("get_count", agentid: agent_id)
  41 + end
  42 +
  43 + # 获取素材列表
  44 + # https://qyapi.weixin.qq.com/cgi-bin/material/batchget?access_token=ACCESS_TOKEN
  45 + def list(agent_id, type, offset, count=20)
  46 + check_masterial_type(type)
  47 + http_post("batchget", {agentid: agent_id, type: type, offset: offset, count: count})
  48 + end
  49 +
  50 + private
  51 +
  52 + def base_url
  53 + "/material"
  54 + end
  55 +
  56 + def check_masterial_type(type)
  57 + if !type.to_s.in?(MATERIAL_TYPES)
  58 + raise "#{type} must one of #{MATERIAL_TYPES.join(',')}"
  59 + end
  60 + end
  61 +
  62 + end
  63 + end
  64 +end
... ...