国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > Java > 正文

Java微信公眾平臺之群發接口(高級群發)

2024-07-14 08:40:41
字體:
來源:轉載
供稿:網友

再次吐槽下,微信素材管理和群發這塊文檔對Java很不友好,此文需要結合我前文和官方文檔。

測試號調試群發只需看是否群發消息是否能組裝成功,不需要看結果如何(反正不會發送成功的),因為微信還沒開放這個功能(估計也不會開放的)。

一、群發說明

在公眾平臺網站上,為訂閱號提供了每天一條的群發權限,為服務號提供每月(自然月)4條的群發權限。

1、對于認證訂閱號,群發接口每天可成功調用1次,此次群發可選擇發送給全部用戶或某個標簽;

2、對于認證服務號雖然開發者使用高級群發接口的每日調用限制為100次,但是用戶每月只能接收4條,無論在公眾平臺網站上,還是使用接口群發,用戶每月只能接收4條群發消息,多于4條的群發將對該用戶發送失敗;

3、開發者可以主動設置 clientmsgid 來避免重復推送。

4、群發接口每分鐘限制請求60次,超過限制的請求會被拒絕。

5、圖文消息正文中插入自己帳號和其他公眾號已群發文章鏈接的能力。

二、群發圖文消息過程

1、首先,預先將圖文消息中需要用到的圖片,使用上傳圖文消息內圖片接口,上傳成功并獲得圖片 URL;參考前文的上傳圖文消息內的圖片獲取URL方法
2、上傳圖文消息素材,需要用到圖片時,請使用上一步獲取的圖片 URL;
3、使用對用戶標簽的群發,或對 OpenID 列表的群發,將圖文消息群發出去,群發時微信會進行原創校驗,并返回群發操作結果;
4、在上述過程中,如果需要,還可以預覽圖文消息、查詢群發狀態,或刪除已群發的消息等。

三、群發圖片、文本等其他消息類型的過程

1、如果是群發文本消息,則直接根據下面的接口說明進行群發即可;
2、如果是群發圖片、視頻等消息,則需要預先通過素材管理接口準備好 mediaID。

四、is_to_all說明

用于設定是否向全部用戶發送,值為true或false,選擇true該消息群發給所有用戶,選擇false可根據tag_id發送給指定群組的用戶。

五、兩種群發方式

根據標簽進行群發,訂閱號與服務號必須通過認證

接口:https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token=ACCESS_TOKEN

根據OpenID列表群發,只適用于認證后的服務號

接口:https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token=ACCESS_TOKEN

post數據可以是圖文消息、文本、語音/音頻、圖片、視頻、卡券消息(所有使用到media_id的地方,現在都可以使用素材管理中的永久素材media_id了)

1、圖文消息post數據中的media_id需要通過上傳圖文消息素材接口獲取(https://api.weixin.qq.com/cgi-bin/media/uploadnews?access_token=ACCESS_TOKEN)
這個跟素材管理里的新增永久圖文素材接口post數據一樣,只是接口不一樣、返回的json多了一個type和created_at,參考我前文的新增永久圖文素材方法
2、語音/音頻、圖片post數據中的media_id需要通過上傳下載多媒體文件接口獲得,參考我前文的新增臨時/永久素材方法

3、視頻post數據中的media_id最麻煩,先得通過上傳下載多媒體文件接口獲取到media_id(經測試,永久的下一步報錯提示無效media_id),然后再通過特別接口再獲取到一個media_id,這才是群發所需要的media_id

六、群發示例Controller

@RequestMapping("/sendByOpenid") public MassMsgResult sendByOpenid() throws Exception {   // 根據OpenID列表群發圖文消息   String mediaPath1 = "C:/Users/phil/Pictures/image/8538572f61d7a94cf0b9fe0f290cdb28.jpg";   UploadMediasResult result1 = HttpReqUtil.uploadTempMedia("phil_token", "image", mediaPath1);   String mediaPath2 = "C:/Users/phil/Pictures/image/685977abgw1f8xqp46dgyj20qo0zktfi.jpg";   UploadMediasResult result2 = HttpReqUtil.uploadTempMedia("phil_token", "image", mediaPath2);   List<UploadNewsMedia> array = new ArrayList<>();   UploadNewsMedia entity1 = new UploadNewsMedia();   entity1.setAuthor("phil");   entity1.setContent("人生只有經歷才會懂得,只有懂得才知道珍惜,一生中,總會有一個人讓你笑得最甜,也總會有一個人讓你痛得最深,忘記,是善待自己");   entity1.setContent_source_url("http://blog.csdn.net/phil_jing");   // entity1.setDigest("digest");   entity1.setShow_conver_pic(1);   entity1.setThumb_media_id(result1.getMedia_id());   entity1.setTitle("心靈雞湯");   array.add(entity1);   UploadNewsMedia entity2 = new UploadNewsMedia();   entity2.setAuthor("phil");   entity2.setContent("什么是幸福,幸福就是自己的一種愉快的心理狀態和感受。時時、事事都能使自己快樂的人才是最幸福的人。最快樂的人,就是最幸福的人。笑口常開的人,是最幸福的。");   entity2.setContent_source_url("http://www.qq.com");   // entity2.setDigest("digest");   entity2.setShow_conver_pic(0);   entity2.setThumb_media_id(result2.getMedia_id());   entity2.setTitle(" 經典語錄");   array.add(entity2);   UploadMediasResult ur = HttpReqUtil.uploadNewsMedia("phil_token", array);   List<String> openids = new ArrayList<>();   openids.add("ovHQ5v9-ZsHUcax_nTCQwiP-sBcg");   openids.add("ovHQ5v6CW3INkWUsCl3olODif0cc");   MassMsgResult result_news = wechatMsgService.sendMpnewsToOpenid("phil_token", openids,   ur.getMedia_id());   logger.debug(" send by openid msg {} " ,result_news.getErrmsg());   // 根據OpenID列表群發文字消息   MassMsgResult result_text = wechatMsgService.sendTextToOpenid("phil_token", openids,   "什么是幸福,幸福就是自己的一種愉快的心理狀態和感受。時時、事事都能使自己快樂的人才是最幸福的人。最快樂的人,就是最幸福的人。笑口常開的人,是最幸福的");   logger.debug(" send by openid msg {} " ,result_text.getErrmsg());   return null; } 

七、部分用到的類和方法

群發方法

/**  * 根據標簽進行群發文本消息  * @param accessToken 授權token  * @param entity 圖文消息對象  * @return  */ public MassMsgResult sendTextToTag(String accessToken, int tagId, String content){   MassMsgResult result = null;   TreeMap<String, String> params = new TreeMap<>();   params.put("access_token", accessToken);   // post 提交的參數   Map<String, Object> filterParams = new HashMap<>();   filterParams.put("is_to_all", false);   filterParams.put("tag_id", tagId);   Map<String, Object> textParams = new HashMap<>();   textParams.put("content", content);   TreeMap<String,Object> dataParams = new TreeMap<>();   dataParams.put("filter", filterParams);   dataParams.put("text", textParams);   dataParams.put("msgtype", "text");   String data = JsonUtil.toJsonString(dataParams);   String json = HttpReqUtil.HttpsDefaultExecute(SystemConfig.POST_METHOD, WechatConfig.SEND_ALL_MASS_MESSAGE_URL, params, data);   try {     result = JsonUtil.fromJsonString(json, MassMsgResult.class);   } catch (Exception e) {     e.printStackTrace();   }   return result; }  /**  * 根據標簽進行群發圖文消息  * @param accessToken 授權token  * @param tagId 標簽  * @param mediaId uploadMedia方法獲得  * @return  */ public MassMsgResult sendMpnewsToTag(String accessToken, int tagId, String mediaId){   MassMsgResult result = null;   TreeMap<String, String> params = new TreeMap<>();   params.put("access_token", accessToken);   // post 提交的參數   Map<String, Object> filterParams = new HashMap<>();   filterParams.put("is_to_all", false);   filterParams.put("tag_id", tagId);   Map<String, Object> mpnewsParams = new HashMap<>();   mpnewsParams.put("media_id", mediaId);   TreeMap<String,Object> dataParams = new TreeMap<>();   dataParams.put("filter", filterParams);   dataParams.put("mpnews", mpnewsParams);   dataParams.put("msgtype", "mpnews");   dataParams.put("send_ignore_reprint", 0);//不能省略   String data = JsonUtil.toJsonString(dataParams);   String json = HttpReqUtil.HttpsDefaultExecute(SystemConfig.POST_METHOD, WechatConfig.SEND_ALL_MASS_MESSAGE_URL, params, data);   try {     result = JsonUtil.fromJsonString(json, MassMsgResult.class);   } catch (Exception e) {     e.printStackTrace();   }   return result; }  /**  * 根據標簽進行群發圖片  * @param accessToken 授權token  * @param tagId 標簽  * @param mediaId uploadMedia方法獲得  * @return  */ public MassMsgResult sendImageToTag(String accessToken, int tagId, String mediaId){   MassMsgResult result = null;   TreeMap<String, String> params = new TreeMap<>();   params.put("access_token", accessToken);   // post 提交的參數   Map<String, Object> filterParams = new HashMap<>();   filterParams.put("is_to_all", false);   filterParams.put("tag_id", tagId);   Map<String, Object> imageParams = new HashMap<>();   imageParams.put("media_id", mediaId);   TreeMap<String,Object> dataParams = new TreeMap<>();   dataParams.put("filter", filterParams);   dataParams.put("image", imageParams);   dataParams.put("msgtype", "image");   String data = JsonUtil.toJsonString(dataParams);   String json = HttpReqUtil.HttpsDefaultExecute(SystemConfig.POST_METHOD, WechatConfig.SEND_ALL_MASS_MESSAGE_URL, params, data);   try {     result = JsonUtil.fromJsonString(json, MassMsgResult.class);   } catch (Exception e) {     e.printStackTrace();   }   return result; }  /**  * 根據標簽進行群發語音/音頻  * @param accessToken 授權token  * @param tagId 標簽  * @param mediaId uploadMedia方法獲得  * @return  */ public MassMsgResult sendVoiceToTag(String accessToken, int tagId, String mediaId){   MassMsgResult result = null;   TreeMap<String, String> params = new TreeMap<>();   params.put("access_token", accessToken);   // post 提交的參數   Map<String, Object> filterParams = new HashMap<>();   filterParams.put("is_to_all", false);   filterParams.put("tag_id", tagId);   Map<String, Object> voiceParams = new HashMap<>();   voiceParams.put("media_id", mediaId);   TreeMap<String,Object> dataParams = new TreeMap<>();   dataParams.put("filter", filterParams);   dataParams.put("voice", voiceParams);   dataParams.put("msgtype", "voice");   String data = JsonUtil.toJsonString(dataParams);   String json = HttpReqUtil.HttpsDefaultExecute(SystemConfig.POST_METHOD, WechatConfig.SEND_ALL_MASS_MESSAGE_URL, params, data);   try {     result = JsonUtil.fromJsonString(json, MassMsgResult.class);   } catch (Exception e) {     e.printStackTrace();   }   return result; }  /**  * 根據標簽進行群發視頻  * @param accessToken 授權token  * @param tagId 標簽  * @param mediaId uploadMedia方法獲得  * @return  */ public MassMsgResult sendVideoToTag(String accessToken, int tagId, String mediaId){   MassMsgResult result = null;   TreeMap<String, String> params = new TreeMap<>();   params.put("access_token", accessToken);   // post 提交的參數   Map<String, Object> filterParams = new HashMap<>();   filterParams.put("is_to_all", false);   filterParams.put("tag_id", tagId);   Map<String, Object> mpvideoParams = new HashMap<>();   mpvideoParams.put("media_id", mediaId);   TreeMap<String,Object> dataParams = new TreeMap<>();   dataParams.put("filter", filterParams);   dataParams.put("mpvideo", mpvideoParams);   dataParams.put("msgtype", "mpvideo");   String data = JsonUtil.toJsonString(dataParams);   String json = HttpReqUtil.HttpsDefaultExecute(SystemConfig.POST_METHOD, WechatConfig.SEND_ALL_MASS_MESSAGE_URL, params, data);   try {     result = JsonUtil.fromJsonString(json, MassMsgResult.class);   } catch (Exception e) {     e.printStackTrace();   }   return result; }  /**  * 根據標簽進行群發卡券  * @param accessToken 授權token  * @param tagId 標簽  * @param card_id  * @return  */ public MassMsgResult sendWxCardToTag(String accessToken, int tagId, String cardId){   MassMsgResult result = null;   TreeMap<String, String> params = new TreeMap<>();   params.put("access_token", accessToken);   // post 提交的參數   Map<String, Object> filterParams = new HashMap<>();   filterParams.put("is_to_all", false);   filterParams.put("tag_id", tagId);   Map<String, Object> wxcardParams = new HashMap<>();   wxcardParams.put("card_id", cardId);   TreeMap<String,Object> dataParams = new TreeMap<>();   dataParams.put("filter", filterParams);   dataParams.put("wxcard", wxcardParams);   dataParams.put("msgtype", "wxcard");   String data = JsonUtil.toJsonString(dataParams);   String json = HttpReqUtil.HttpsDefaultExecute(SystemConfig.POST_METHOD, WechatConfig.SEND_ALL_MASS_MESSAGE_URL, params, data);   try {     result = JsonUtil.fromJsonString(json, MassMsgResult.class);   } catch (Exception e) {     e.printStackTrace();   }   return result; }  /**  * 根據OpenId進行群發圖文消息  * @param accessToken 授權token  * @param tagId 標簽  * @param mediaId uploadMedia方法獲得  * @return  */ public MassMsgResult sendMpnewsToOpenid(String accessToken, List<String> openids, String mediaId){   MassMsgResult result = null;   TreeMap<String, String> params = new TreeMap<>();   params.put("access_token", accessToken);   // post 提交的參數   Map<String, Object> mpnewsParams = new HashMap<>();   mpnewsParams.put("media_id", mediaId);   TreeMap<String,Object> dataParams = new TreeMap<>();   dataParams.put("touser", openids);   dataParams.put("mpnews", mpnewsParams);   dataParams.put("msgtype", "mpnews");   dataParams.put("send_ignore_reprint", 0);   String data = JsonUtil.toJsonString(dataParams);   String json = HttpReqUtil.HttpsDefaultExecute(SystemConfig.POST_METHOD, WechatConfig.SEND_MASS_MESSAGE_URL, params, data);   try {     result = JsonUtil.fromJsonString(json, MassMsgResult.class);   } catch (Exception e) {     e.printStackTrace();   }   return result; }  /**  * 根據OpenId進行群發文本消息  * @param accessToken 授權token  * @param openids openid  * @param content  * @return  */ public MassMsgResult sendTextToOpenid(String accessToken, List<String> openids, String content){   MassMsgResult result = null;   TreeMap<String, String> params = new TreeMap<>();   params.put("access_token", accessToken);   // post 提交的參數   Map<String, Object> textParams = new HashMap<>();   textParams.put("content", content);   TreeMap<String,Object> dataParams = new TreeMap<>();   dataParams.put("touser", openids);   dataParams.put("text", textParams);   dataParams.put("msgtype", "text");   String data = JsonUtil.toJsonString(dataParams);   System.out.println(data);   String json = HttpReqUtil.HttpsDefaultExecute(SystemConfig.POST_METHOD, WechatConfig.SEND_MASS_MESSAGE_URL, params, data);   try {     result = JsonUtil.fromJsonString(json, MassMsgResult.class);   } catch (Exception e) {     e.printStackTrace();   }   return result; }  /**  * 根據OpenId進行群發語音消息  * @param accessToken 授權token  * @param openids openid  * @param mediaId  * @return  */ public MassMsgResult sendVocieToOpenid(String accessToken, List<String> openids, String mediaId){   MassMsgResult result = null;   TreeMap<String, String> params = new TreeMap<>();   params.put("access_token", accessToken);   // post 提交的參數   Map<String, Object> voiceParams = new HashMap<>();   voiceParams.put("media_id", mediaId);   TreeMap<String,Object> dataParams = new TreeMap<>();   dataParams.put("touser", openids);   dataParams.put("voice", voiceParams);   dataParams.put("msgtype", "voice");   String data = JsonUtil.toJsonString(dataParams);   System.out.println(data);   String json = HttpReqUtil.HttpsDefaultExecute(SystemConfig.POST_METHOD, WechatConfig.SEND_MASS_MESSAGE_URL, params, data);   try {     result = JsonUtil.fromJsonString(json, MassMsgResult.class);   } catch (Exception e) {     e.printStackTrace();   }   return result; }   /**  * 根據OpenId進行群發圖片消息  * @param accessToken 授權token  * @param openids openid  * @param mediaId  * @return  */ public MassMsgResult sendImageToOpenid(String accessToken, List<String> openids, String mediaId){   MassMsgResult result = null;   TreeMap<String, String> params = new TreeMap<>();   params.put("access_token", accessToken);   // post 提交的參數   Map<String, Object> imageParams = new HashMap<>();   imageParams.put("media_id", mediaId);   TreeMap<String,Object> dataParams = new TreeMap<>();   dataParams.put("touser", openids);   dataParams.put("image", imageParams);   dataParams.put("msgtype", "image");   String data = JsonUtil.toJsonString(dataParams);   System.out.println(data);   String json = HttpReqUtil.HttpsDefaultExecute(SystemConfig.POST_METHOD, WechatConfig.SEND_MASS_MESSAGE_URL, params, data);   try {     result = JsonUtil.fromJsonString(json, MassMsgResult.class);   } catch (Exception e) {     e.printStackTrace();   }   return result; }  /**  * 根據OpenId進行群發視頻消息  * @param accessToken 授權token  * @param openids openid  * @param mpVideoMedia uploadMediaVideo方法獲得media  * @return  */ public MassMsgResult sendVideoToOpenid(String accessToken, List<String> openids, MpVideoMedia mpVideoMedia){   MassMsgResult result = null;   TreeMap<String, String> params = new TreeMap<>();   params.put("access_token", accessToken);   // post 提交的參數   TreeMap<String,Object> dataParams = new TreeMap<>();   dataParams.put("touser", openids);   dataParams.put("mpvideo", mpVideoMedia);   dataParams.put("msgtype", "mpvideo");   String data = JsonUtil.toJsonString(dataParams);   System.out.println(data);   String json = HttpReqUtil.HttpsDefaultExecute(SystemConfig.POST_METHOD, WechatConfig.SEND_MASS_MESSAGE_URL, params, data);   try {     result = JsonUtil.fromJsonString(json, MassMsgResult.class);   } catch (Exception e) {     e.printStackTrace();   }   return result; }  /**  * 根據OpenId進行群發卡券消息  * @param accessToken 授權token  * @param openids openid  * @param mediaId  * @return  */ public MassMsgResult sendWxcardToOpenid(String accessToken, List<String> openids, String cardId){   MassMsgResult result = null;   TreeMap<String, String> params = new TreeMap<>();   params.put("access_token", accessToken);   // post 提交的參數   Map<String, Object> wxcardParams = new HashMap<>();   wxcardParams.put("card_id", cardId);   TreeMap<String,Object> dataParams = new TreeMap<>();   dataParams.put("touser", openids);   dataParams.put("wxcard", wxcardParams);   dataParams.put("msgtype", "wxcard");   String data = JsonUtil.toJsonString(dataParams);   String json = HttpReqUtil.HttpsDefaultExecute(SystemConfig.POST_METHOD, WechatConfig.SEND_MASS_MESSAGE_URL, params, data);   try {     result = JsonUtil.fromJsonString(json, MassMsgResult.class);   } catch (Exception e) {     e.printStackTrace();   }   return result; } 

視頻post數據bean

/**  * 視頻post數據bean  * @author phil  * @date 2017年9月20日  *  */ public class MpVideoMedia {    private String media_id;     private String title;   private String description; } 

獲取群發視頻post中的media_id

/**  * 獲取群發視頻post中的media_id  *  * @param accessToken  * @param uploadVideo  * @return  */ public static UploadMediasResult uploadMediaVideo(String accessToken, MpVideoMedia mpVideoMedia) {   UploadMediasResult result = null;   TreeMap<String, String> params = new TreeMap<String, String>();   params.put("access_token", accessToken);   // post 提交的參數   String data = JsonUtil.toJsonString(mpVideoMedia);   String json = HttpReqUtil.HttpsDefaultExecute(SystemConfig.POST_METHOD, WechatConfig.UPLOAD_VIDEO_MEDIA_URL,       params, data);   result = JsonUtil.fromJsonString(json, UploadMediasResult.class);   return result; } 

群發返回的結果

package com.phil.wechat.msg.model.resp;  import com.phil.wechat.base.result.ResultState;  /**  * 群發消息返回的結果  * 根據OpenID列表群發  * @author phil  * @date 2017年7月2日  *  */  public class MassMsgResult extends ResultState{       private String type; //媒體文件類型,分別有圖片(image)、語音(voice)、視頻(video)和縮略圖(thumb),次數為news,即圖文消息    private String msg_id;    private String msg_data_id;  }  

配置類

// 按分組進行群發 public static final String SEND_ALL_MASS_MESSAGE_URL = "https://api.weixin.qq.com/cgi-bin/message/mass/sendall"; // 按照openid進行群發消息(OpenID最少2個,最多10000個 10000個) public static final String SEND_MASS_MESSAGE_URL = "https://api.weixin.qq.com/cgi-bin/message/mass/send"; //上傳圖文消息素材 public static final String UPLOAD_NEWS_MEDIA_URL = "https://api.weixin.qq.com/cgi-bin/media/uploadnews"; //獲取群發視頻post中的media_id public static final String UPLOAD_VIDEO_MEDIA_URL = " https://api.weixin.qq.com/cgi-bin/media/uploadvideo"; 

關于源碼,希望幫到了你

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VeVb武林網。


注:相關教程知識閱讀請移步到JAVA教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 延津县| 涟水县| 庄河市| 年辖:市辖区| 广平县| 贵德县| 泸溪县| 沙田区| 上犹县| 昭平县| 南木林县| 辽阳市| 东至县| 巴林左旗| 湖北省| 长岛县| 于田县| 天门市| 利津县| 申扎县| 郴州市| 广昌县| 师宗县| 龙海市| 宜兰县| 东光县| 梁山县| 庆阳市| 共和县| 杭锦后旗| 内黄县| 东台市| 五台县| 塘沽区| 托克逊县| 江孜县| 进贤县| 随州市| 隆昌县| 云霄县| 浏阳市|