微信公眾號開發之群發消息預覽接口(十五)


預覽接口【訂閱號與服務號認證后均可用】

開發者可通過該接口發送消息給指定用戶,在手機端查看消息的樣式和排版。為了滿足第三方平台開發者的需求,在保留對openID預覽能力的同時,增加了對指定微信號發送預覽的能力,但該能力每日調用次數有限制(100次),請勿濫用。

接口調用請求說明

http請求方式: POST https://api.weixin.qq.com/cgi-bin/message/mass/preview?access_token=ACCESS_TOKEN

POST數據說明

POST數據示例如下:

圖文消息(其中media_id與根據分組群發中的media_id相同):

{
   "touser":"OPENID", 
   "mpnews":{              
     "media_id":"123dsdajkasd231jhksad"               
    },
   "msgtype":"mpnews" 
}

文本:

{     
    "touser":"OPENID",
    "text":{           
      "content":"CONTENT"            
    },     
    "msgtype":"text"
}

語音(其中media_id與根據分組群發中的media_id相同):

{
    "touser":"OPENID",
    "voice":{              
     "media_id":"123dsdajkasd231jhksad"
    },
    "msgtype":"voice" 
}

圖片(其中media_id與根據分組群發中的media_id相同):

{
    "touser":"OPENID",
    "image":{      
     "media_id":"123dsdajkasd231jhksad"
    },
    "msgtype":"image" 
}

視頻(其中media_id與根據分組群發中的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" 
}

請注意,上述JSON數據中的touser字段都可以改為towxname,這樣就可以針對微信號進行預覽(而非openID),towxname和touser同時賦值時,以towxname優先。修改后JSON數據如下(以圖文消息為例): 圖文消息:

{
   "towxname":"示例的微信號", 
   "mpnews":{              
            "media_id":"123dsdajkasd231jhksad"               
             },
   "msgtype":"mpnews" 
}
參數 說明
touser 接收消息用戶對應該公眾號的openid,該字段也可以改為towxname,以實現對微信號的預覽
msgtype 群發的消息類型,圖文消息為mpnews,文本消息為text,語音為voice,音樂為music,圖片為image,視頻為video,卡券為wxcard
media_id 用於群發的消息的media_id
content 發送文本消息時文本的內容

返回說明

返回數據示例(正確時的JSON返回結果):

{
   "errcode":0,
   "errmsg":"preview success",
   "msg_id":34182
}
參數 說明
errcode 錯誤碼
errmsg 錯誤信息
msg_id 消息ID

 一、我們測試一下圖文素材預覽群發

【重要說明!!!】1、這里發現一個文檔bug,預覽群發圖文素材時候touser至少需要兩個openid以上

2、圖文素材預覽接口測試賬號暫時沒有權限

現在我們看代碼演示發送圖文消息預覽

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;

/**
 * 預覽接口【訂閱號與服務號認證后均可用】
 */
@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(); } /** *預覽接口 */ 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); //將菜單對象轉換成JSON字符串 String dataString = this.createPreviewString(openIdList, mediaId, content, msgtype); log.info("dataString:{}",dataString); //發起POST請求創建菜單 String jsonObject = restTemplate.postForObject(url, dataString,String.class); return jsonObject; } return null; } } 

在swagger中添加測試方法

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
 * 類描述: 預覽API
 */
@Slf4j
@RestController
@Api(tags = "預覽接口")
@RequestMapping(value = "/preview")
public class PreviewController {

    @Autowired
    private PreviewUtil previewUtil;

    /**
     * 根據標簽進行預覽
     */
    @ApiOperation(value = "預覽群發接口")
    @RequestMapping(value = "/preview", method = RequestMethod.POST)
    @ApiImplicitParams({
            //@ApiImplicitParam(name="openId",value="接收消息用戶對應該公眾號的openid,該字段也可以改為towxname,以實現對微信號的預覽", paramType="query",dataType="String"),
            @ApiImplicitParam(name="mediaId",value="用於群發的消息的media_id"),
            @ApiImplicitParam(name="content",value="發送文本消息時文本的內容"),
            @ApiImplicitParam(name="msgtype",value="群發的消息類型,圖文消息為mpnews,文本消息為text,語音為voice,音樂為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測試文本消息預覽,填寫正確的參數

發送成功,返回了一個msg_id

重要說明:當我們短時間發送重復內容會發生什么呢?

我們看看官方開發文檔怎么說的

使用 clientmsgid 參數,避免重復推送

一、群發接口新增 clientmsgid 參數,開發者調用群發接口時可以主動設置 clientmsgid 參數,避免重復推送。

群發時,微信后台將對 24 小時內的群發記錄進行檢查,如果該 clientmsgid 已經存在一條群發記錄,則會拒絕本次群發請求,返回已存在的群發msgid,開發者可以調用“查詢群發消息發送狀態”接口查看該條群發的狀態。

二、新增返回碼

返回碼 結果
45065 相同 clientmsgid 已存在群發記錄,返回數據中帶有已存在的群發任務的 msgid
45066 相同 clientmsgid 重試速度過快,請間隔1分鍾重試
45067 clientmsgid 長度超過限制

 微信公眾號還提供了設置和查詢群發速度接口,更多細節請讀者自行仔細閱讀微信公眾號開發文檔更多詳細內容,謝謝觀看,我們下回再見!

===============================================================================

如果您覺得此文有幫助,可以小小打賞一下,持續更新更有動力喲!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM