微信公众号开发之群发消息预览接口(十五)
預(yù)覽接口【訂閱號(hào)與服務(wù)號(hào)認(rèn)證后均可用】
開(kāi)發(fā)者可通過(guò)該接口發(fā)送消息給指定用戶,在手機(jī)端查看消息的樣式和排版。為了滿足第三方平臺(tái)開(kāi)發(fā)者的需求,在保留對(duì)openID預(yù)覽能力的同時(shí),增加了對(duì)指定微信號(hào)發(fā)送預(yù)覽的能力,但該能力每日調(diào)用次數(shù)有限制(100次),請(qǐng)勿濫用。
接口調(diào)用請(qǐng)求說(shuō)明
http請(qǐng)求方式: POST https://api.weixin.qq.com/cgi-bin/message/mass/preview?access_token=ACCESS_TOKEN
POST數(shù)據(jù)說(shuō)明
POST數(shù)據(jù)示例如下:
圖文消息(其中media_id與根據(jù)分組群發(fā)中的media_id相同):
{
"touser":"OPENID",
"mpnews":{
"media_id":"123dsdajkasd231jhksad"
},
"msgtype":"mpnews"
}
文本:
{
"touser":"OPENID",
"text":{
"content":"CONTENT"
},
"msgtype":"text"
}
語(yǔ)音(其中media_id與根據(jù)分組群發(fā)中的media_id相同):
{
"touser":"OPENID",
"voice":{
"media_id":"123dsdajkasd231jhksad"
},
"msgtype":"voice"
}
圖片(其中media_id與根據(jù)分組群發(fā)中的media_id相同):
{
"touser":"OPENID",
"image":{
"media_id":"123dsdajkasd231jhksad"
},
"msgtype":"image"
}
視頻(其中media_id與根據(jù)分組群發(fā)中的media_id相同):
{
"touser":"OPENID",
"mpvideo":{ "media_id":"IhdaAQXuvJtGzwwc0abfXnzeezfO0NgPK6AQYShD8RQYMTtfzbLdBIQkQziv2XJc",
},
"msgtype":"mpvideo"
}
卡券:
{ "touser":"OPENID",
"wxcard":{
"card_id":"123dsdajkasd231jhksad",
"card_ext": "{"code":"","openid":"","timestamp":"1402057159","signature":"017bb17407c8e0058a66d72dcc61632b70f511ad"}"
},
"msgtype":"wxcard"
}
請(qǐng)注意,上述JSON數(shù)據(jù)中的touser字段都可以改為towxname,這樣就可以針對(duì)微信號(hào)進(jìn)行預(yù)覽(而非openID),towxname和touser同時(shí)賦值時(shí),以towxname優(yōu)先。修改后JSON數(shù)據(jù)如下(以圖文消息為例): 圖文消息:
{
"towxname":"示例的微信號(hào)",
"mpnews":{
"media_id":"123dsdajkasd231jhksad"
},
"msgtype":"mpnews"
}
| 參數(shù) | 說(shuō)明 |
|---|---|
| touser | 接收消息用戶對(duì)應(yīng)該公眾號(hào)的openid,該字段也可以改為towxname,以實(shí)現(xiàn)對(duì)微信號(hào)的預(yù)覽 |
| msgtype | 群發(fā)的消息類型,圖文消息為mpnews,文本消息為text,語(yǔ)音為voice,音樂(lè)為music,圖片為image,視頻為video,卡券為wxcard |
| media_id | 用于群發(fā)的消息的media_id |
| content | 發(fā)送文本消息時(shí)文本的內(nèi)容 |
返回說(shuō)明
返回?cái)?shù)據(jù)示例(正確時(shí)的JSON返回結(jié)果):
{
"errcode":0,
"errmsg":"preview success",
"msg_id":34182
}
| 參數(shù) | 說(shuō)明 |
|---|---|
| errcode | 錯(cuò)誤碼 |
| errmsg | 錯(cuò)誤信息 |
| msg_id | 消息ID |
一、我們測(cè)試一下圖文素材預(yù)覽群發(fā)
【重要說(shuō)明!!!】1、這里發(fā)現(xiàn)一個(gè)文檔bug,預(yù)覽群發(fā)圖文素材時(shí)候touser至少需要兩個(gè)openid以上
?
2、圖文素材預(yù)覽接口測(cè)試賬號(hào)暫時(shí)沒(méi)有權(quán)限
?
現(xiàn)在我們看代碼演示發(fā)送圖文消息預(yù)覽
?
package com.xu.wemall.components.weixin;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xu.wemall.commons.constants.URIConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import java.util.List;
/**
* 預(yù)覽接口【訂閱號(hào)與服務(wù)號(hào)認(rèn)證后均可用】
*/
@Slf4j
@Component
public class PreviewUtil {
@Autowired
private RestTemplate restTemplate;
@Autowired
private AccessTokenUtil accessTokenUtil;
private String createPreviewString(List<String> openIdList, String mediaId, String content, String msgtype){
JSONObject data = new JSONObject();
JSONArray touser = new JSONArray();
touser.addAll(openIdList);
data.put("touser",touser);
JSONObject type = new JSONObject();
if(mediaId != null){
type.put("media_id",mediaId);
}
//圖文消息
if(msgtype.equalsIgnoreCase("mpnews")){
data.put("mpnews",type);
}else if(msgtype.equalsIgnoreCase("text")){
data.put("text",type); //文本
type.put("content",content);
}else if(msgtype.equalsIgnoreCase("voice")){
data.put("voice",type); //聲音
}else if(msgtype.equalsIgnoreCase("image")){
data.put("image",type); //圖片
}else if(msgtype.equalsIgnoreCase("mpvideo")){
data.put("mpvideo",type); //聲音
}else if(msgtype.equalsIgnoreCase("wxcard")) {
data.put("wxcard", type); //卡券
}
if(msgtype !=null){
data.put("msgtype",msgtype);
}
return data.toJSONString();
}
/**
*預(yù)覽接口
*/
public String preview(List<String> openIdList, String mediaId, String content, String msgtype) {
String accessToken = accessTokenUtil.getAccessToken();
if (accessToken != null) {
log.info("URL{}", URIConstant.PREVIEW_URL);
String url = URIConstant.PREVIEW_URL.replace("ACCESS_TOKEN", accessToken);
log.info("PREVIEW_URL:{}", url);
//將菜單對(duì)象轉(zhuǎn)換成JSON字符串
String dataString = this.createPreviewString(openIdList, mediaId, content, msgtype);
log.info("dataString:{}",dataString);
//發(fā)起POST請(qǐng)求創(chuàng)建菜單
String jsonObject = restTemplate.postForObject(url, dataString,String.class);
return jsonObject;
}
return null;
}
}
在swagger中添加測(cè)試方法
?
package com.xu.wemall.controller.weixin;
import com.xu.wemall.components.weixin.PreviewUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* 類名稱: previewController
* 類描述: 預(yù)覽API
*/
@Slf4j
@RestController
@Api(tags = "預(yù)覽接口")
@RequestMapping(value = "/preview")
public class PreviewController {
@Autowired
private PreviewUtil previewUtil;
/**
* 根據(jù)標(biāo)簽進(jìn)行預(yù)覽
*/
@ApiOperation(value = "預(yù)覽群發(fā)接口")
@RequestMapping(value = "/preview", method = RequestMethod.POST)
@ApiImplicitParams({
//@ApiImplicitParam(name="openId",value="接收消息用戶對(duì)應(yīng)該公眾號(hào)的openid,該字段也可以改為towxname,以實(shí)現(xiàn)對(duì)微信號(hào)的預(yù)覽", paramType="query",dataType="String"),
@ApiImplicitParam(name="mediaId",value="用于群發(fā)的消息的media_id"),
@ApiImplicitParam(name="content",value="發(fā)送文本消息時(shí)文本的內(nèi)容"),
@ApiImplicitParam(name="msgtype",value="群發(fā)的消息類型,圖文消息為mpnews,文本消息為text,語(yǔ)音為voice,音樂(lè)為music,圖片為image,視頻為video,卡券為wxcard", paramType="query",dataType="Integer")
})
public Object preview(@RequestParam(value = "touser") List<String> touser, String mediaId, String content, String msgtype) {
String tempString = previewUtil.preview(touser, mediaId,content, msgtype);
return tempString;
}
}
swagger測(cè)試文本消息預(yù)覽,填寫(xiě)正確的參數(shù)
?
發(fā)送成功,返回了一個(gè)msg_id
?
?
重要說(shuō)明:當(dāng)我們短時(shí)間發(fā)送重復(fù)內(nèi)容會(huì)發(fā)生什么呢?
?
我們看看官方開(kāi)發(fā)文檔怎么說(shuō)的
使用 clientmsgid 參數(shù),避免重復(fù)推送
一、群發(fā)接口新增 clientmsgid 參數(shù),開(kāi)發(fā)者調(diào)用群發(fā)接口時(shí)可以主動(dòng)設(shè)置 clientmsgid 參數(shù),避免重復(fù)推送。
群發(fā)時(shí),微信后臺(tái)將對(duì) 24 小時(shí)內(nèi)的群發(fā)記錄進(jìn)行檢查,如果該 clientmsgid 已經(jīng)存在一條群發(fā)記錄,則會(huì)拒絕本次群發(fā)請(qǐng)求,返回已存在的群發(fā)msgid,開(kāi)發(fā)者可以調(diào)用“查詢?nèi)喊l(fā)消息發(fā)送狀態(tài)”接口查看該條群發(fā)的狀態(tài)。
二、新增返回碼
返回碼 結(jié)果 45065 相同 clientmsgid 已存在群發(fā)記錄,返回?cái)?shù)據(jù)中帶有已存在的群發(fā)任務(wù)的 msgid 45066 相同 clientmsgid 重試速度過(guò)快,請(qǐng)間隔1分鐘重試 45067 clientmsgid 長(zhǎng)度超過(guò)限制
微信公眾號(hào)還提供了設(shè)置和查詢?nèi)喊l(fā)速度接口,更多細(xì)節(jié)請(qǐng)讀者自行仔細(xì)閱讀微信公眾號(hào)開(kāi)發(fā)文檔更多詳細(xì)內(nèi)容,謝謝觀看,我們下回再見(jiàn)!
===============================================================================
如果您覺(jué)得此文有幫助,可以小小打賞一下,持續(xù)更新更有動(dòng)力喲!
總結(jié)
以上是生活随笔為你收集整理的微信公众号开发之群发消息预览接口(十五)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 『数据库』数据库笔记
- 下一篇: 『设计模式』HR:不会设计模式,你好意思