Commit f8c5a52f by lanrion

完善群发信息API接口

parent 5eafcfbc
...@@ -3,7 +3,7 @@ module WeixinAuthorize ...@@ -3,7 +3,7 @@ module WeixinAuthorize
module Api module Api
module Mass module Mass
MSG_TYPE = ["mpnews", "image", "text", "voice", "mpvideo"].freeze MSG_TYPE = ["mpnews", "image", "text", "voice", "mpvideo"]
# media_info= {"media_id" media_id} # media_info= {"media_id" media_id}
# https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token=ACCESS_TOKEN # https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token=ACCESS_TOKEN
...@@ -21,7 +21,6 @@ module WeixinAuthorize ...@@ -21,7 +21,6 @@ module WeixinAuthorize
def mass_with_openids(openids, media_info, msgtype="mpnews") def mass_with_openids(openids, media_info, msgtype="mpnews")
openid_option = {"touser" => openids} openid_option = {"touser" => openids}
media = generate_media(msgtype, media_info, openid_option) media = generate_media(msgtype, media_info, openid_option)
mass_url = "#{mass_base_url}/send" mass_url = "#{mass_base_url}/send"
http_post(mass_url, media) http_post(mass_url, media)
end end
...@@ -30,7 +29,21 @@ module WeixinAuthorize ...@@ -30,7 +29,21 @@ module WeixinAuthorize
# 另外,删除群发消息只能删除图文消息和视频消息,其他类型的消息一经发送,无法删除。 # 另外,删除群发消息只能删除图文消息和视频消息,其他类型的消息一经发送,无法删除。
def mass_delete_with_msgid(msg_id) def mass_delete_with_msgid(msg_id)
mass_url = "#{mass_base_url}/delete" mass_url = "#{mass_base_url}/delete"
http_post(mass_url, {"msgid" => msg_id}) http_post(mass_url, {"msg_id" => msg_id})
end
# 预览接口【订阅号与服务号认证后均可用】
def mass_preview(openid, media_info, msgtype="mpnews")
openid_option = {"touser" => openid}
media = generate_media(msgtype, media_info, openid_option)
mass_url = "#{mass_base_url}/preview"
http_post(mass_url, media)
end
# 查询群发消息发送状态【订阅号与服务号认证后均可用】
def mass_get_status(msg_id)
mass_url = "#{mass_base_url}/get"
http_post(mass_url, {"msg_id" => msg_id})
end end
private private
...@@ -42,7 +55,6 @@ module WeixinAuthorize ...@@ -42,7 +55,6 @@ module WeixinAuthorize
def generate_media(msgtype, media_info, option) def generate_media(msgtype, media_info, option)
msgtype = msgtype.to_s msgtype = msgtype.to_s
raise "#{msgtype} is a valid msgtype" if not MSG_TYPE.include?(msgtype) raise "#{msgtype} is a valid msgtype" if not MSG_TYPE.include?(msgtype)
{ {
msgtype => convert_media_info(msgtype, media_info), msgtype => convert_media_info(msgtype, media_info),
"msgtype" => msgtype "msgtype" => msgtype
......
...@@ -22,7 +22,7 @@ module WeixinAuthorize ...@@ -22,7 +22,7 @@ module WeixinAuthorize
download_media_url download_media_url
end end
# 上传图文消息素材 # 上传图文消息素材, 主要用于群发消息接口
# { # {
# "articles": [ # "articles": [
# { # {
...@@ -70,6 +70,10 @@ module WeixinAuthorize ...@@ -70,6 +70,10 @@ module WeixinAuthorize
private private
def media_base_url
"/media"
end
def process_file(media) def process_file(media)
return media if media.is_a?(File) && jpep?(media) return media if media.is_a?(File) && jpep?(media)
...@@ -87,10 +91,6 @@ module WeixinAuthorize ...@@ -87,10 +91,6 @@ module WeixinAuthorize
file file
end end
def media_base_url
"/media"
end
def process_media(uploader) def process_media(uploader)
uploader = covert(uploader) uploader = covert(uploader)
uploader.file.to_file uploader.file.to_file
......
...@@ -45,17 +45,21 @@ describe WeixinAuthorize::Api::Mass do ...@@ -45,17 +45,21 @@ describe WeixinAuthorize::Api::Mass do
end end
it "#mass_with_group with mpnews" do it "#mass_with_group with mpnews" do
response = $client.mass_with_group("1", mass_media_id) response = $client.mass_with_group("1", "mass_group_text", "text")
expect(response.code).to eq(WeixinAuthorize::OK_CODE) expect(response.code).to eq(WeixinAuthorize::OK_CODE)
expect(response.result["type"]).to eq("mpnews")
expect(response.result.keys).to eq(["type", "msg_id", "errmsg", "errcode"])
end end
it "#mass_with_openids with mpnews" do it "#mass_with_openids with mpnews and can delete message" do
response = $client.mass_with_openids([ENV["OPENID"]], mass_media_id) response = $client.mass_with_openids([ENV["OPENID"]], mass_media_id)
expect(response.code).to eq(WeixinAuthorize::OK_CODE) expect(response.code).to eq(WeixinAuthorize::OK_CODE)
expect(response.result["type"]).to eq("mpnews") expect(response.result.keys).to eq(["msg_id"])
expect(response.result.keys).to eq(["type", "msg_id", "errmsg", "errcode"]) delete_res = $client.mass_delete_with_msgid(response.result["msg_id"])
expect(delete_res.code).to eq(WeixinAuthorize::OK_CODE)
end
it "#mass_preview can preview by openid" do
response = $client.mass_preview(ENV["OPENID"], "mass_text", "text")
expect(response.code).to eq(WeixinAuthorize::OK_CODE)
end end
end end
...@@ -55,7 +55,7 @@ redis_with_ns = Redis::Namespace.new("#{namespace}", :redis => redis) ...@@ -55,7 +55,7 @@ redis_with_ns = Redis::Namespace.new("#{namespace}", :redis => redis)
WeixinAuthorize.configure do |config| WeixinAuthorize.configure do |config|
config.redis = redis_with_ns config.redis = redis_with_ns
config.rest_client_options = {timeout: 1, open_timeout: 1, verify_ssl: true} config.rest_client_options = {timeout: 10, open_timeout: 10, verify_ssl: true}
end end
$client = WeixinAuthorize::Client.new(ENV["APPID"], ENV["APPSECRET"]) $client = WeixinAuthorize::Client.new(ENV["APPID"], ENV["APPSECRET"])
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment