I would like to complain again that WeChat material management and mass posting are not friendly to Java, and this article needs to be combined with my previous article and official documents.
When debugging mass sending test numbers, you only need to see if the mass sending message can be assembled successfully, and you don’t need to see the result (it won’t be sent anyway), because WeChat has not opened this function yet (it is probably not open either).
1. Mass statement
On the public platform website, subscription accounts are provided with one per day mass sending permission, and service accounts are provided with four mass sending permissions per month (natural month).
1. For authenticated subscription accounts, the mass sending interface can be successfully called once a day. This mass sending can be selected to send to all users or a certain tag;
2. Although the daily calls of the developer using the advanced mass sending interface are limited to 100 times for authentication service accounts, users can only receive 4 messages per month. Whether on public platform websites or using mass sending interfaces, users can only receive 4 messages per month. More than 4 mass sendings will fail to send to the user;
3. Developers can actively set clientmsgid to avoid repeated pushes.
4. The mass interface limits 60 requests per minute, and requests that exceed the limit will be denied.
5. The ability to insert your own account and other public accounts into the text and text message links that have been posted in a mass article.
2. The process of mass sending text messages
1. First, use the image interface in the upload image and text message to upload it successfully and obtain the image URL; refer to the previous text to upload the image and text message to obtain the URL method
2. When uploading graphic and text message materials, please use the image URL obtained in the previous step;
3. Use mass sending to user tags or mass sending of OpenID lists to send out the text and text messages. During mass sending, WeChat will perform original verification and return the results of the mass sending operation;
4. In the above process, if necessary, you can also preview the graphic and text messages, query the mass sending status, or delete the mass sending messages, etc.
3. The process of sending other message types such as mass pictures and texts
1. If it is a mass text message, just send it directly according to the following interface instructions;
2. If you are sending messages such as mass pictures, videos, etc., you need to prepare mediaID in advance through the material management interface.
IV. Is_to_all description
Used to set whether to send to all users, the value is true or false, select true, the message is sent to all users in the group, and select false to send to users in the specified group according to tag_id.
5. Two types of mass sending methods
Mass sending according to the tag, the subscription number and service number must be authenticated
Interface: https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token=ACCESS_TOKEN
Mass sending according to OpenID list is only applicable to certified service numbers
Interface: https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token=ACCESS_TOKEN
Post data can be graphic messages, text, voice/audio, picture, video, and card coupon messages (all places where media_id are used, you can now use the permanent material media_id in material management)
1. The media_id in the post data of the text message needs to be obtained through the upload text message material interface (https://api.weixin.qq.com/cgi-bin/media/uploadnews?access_token=ACCESS_TOKEN)
This is the same as the post data of the new permanent graphic material interface in material management, but the interface is different, and the returned json has an additional type and created_at. Refer to the method of adding permanent graphic material in my previous article
2. Media_id in voice/audio and picture post data needs to be obtained through uploading and downloading multimedia file interface. Refer to the new temporary/permanent material method in my previous article
3. Media_id in video post data is the most troublesome. You must first obtain media_id through the upload and download multimedia file interface (after testing, the permanent next step error prompt is invalid media_id), and then obtain a media_id through the special interface. This is the media_id required for mass sending.
6. Bulk sample Controller
@RequestMapping("/sendByOpenid") public MassMsgResult sendByOpenid() throws Exception { // Send a mass text message according to the OpenID list 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("Only experience can you understand life, only know how to cherish it, only know how to understand it, in your life, there will always be someone who makes you laugh the sweetest, and there will always be someone who makes you feel the deepest, forgetting, treating yourself kindly"); 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("Soul Chicken Soup"); array.add(entity1); UploadNewsMedia entity2 = new UploadNewsMedia(); entity2.setAuthor("phil"); entity2.setContent("What is happiness? Happiness is a happy psychological state and feeling of oneself. Only those who can make oneself happy at all times and in everything are the happiest people. The happiest people are the happiest people. People who smile often are the happiest people."); 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(" Classic Quotes"); 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()); // Send a text message based on the OpenID list MassMsgResult result_text = wechatMsgService.sendTextToOpenid("phil_token", openids, "What is happiness? Happiness is a happy psychological state and feeling of oneself. Those who can make oneself happy at all times and in everything are the happiest people. The happiest people are the happiest people. Those who smile often are the happiest people"); logger.debug(" send by openid msg {} " ,result_text.getErrmsg()); return null; }7. Some of the classes and methods used
Mass sending method
/** * Send text messages based on the tag* @param accessToken Authorized token * @param entity graphic message object* @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 Submitted parameters 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; } /** * Mass text messages based on tags* @param accessToken Authorize token * @param tagId tag* @param mediaId uploadMedia method gets * @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 Submitted parameters 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; } /** * Bulk images based on tags* @param accessToken Authorized token * @param tagId Tag* @param mediaId uploadMedia method gets * @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 Submitted parameters 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; } /** * Bulk voice/audio based on the tag* @param accessToken Authorized token * @param tagId Tag* @param mediaId uploadMedia method gets * @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 Submitted parameters 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; } /** * Bulk videos based on tags* @param accessToken Authorized token * @param tagId Tag* @param mediaId uploadMedia method gets * @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 Submitted parameters 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; } /** * Bulk card coupons based on tags* @param accessToken Authorized token * @param tagId Tag* @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 Submitted parameters 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; } /** * Send mass text messages based on OpenId* @param accessToken Authorized token * @param tagId tag* @param mediaId uploadMedia method gets * @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 Submitted parameters 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; } /** * Bulk text messages based on OpenId* @param accessToken Authorization 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 Submitted Parameters 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; } /** * Send mass voice messages based on OpenId* @param accessToken Authorization 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 Submitted parameters 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; } /** * Bulk image messages based on OpenId* @param accessToken Authorized 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 Submitted Parameters 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; } /** * Bulk video messages based on OpenId* @param accessToken Authorized token * @param openids openid * @param mpVideoMedia uploadMediaVideo method obtains 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 Submitted Parameters 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; } /** * Mass card coupon message based on OpenId* @param accessToken Authorization 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 Submitted Parameters 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; }Video post data bean
/** * Video post data bean * @author phil * @date September 20, 2017* */ public class MpVideoMedia { private String media_id; private String title; private String description; }Get media_id in post of mass video
/** * Get media_id in the post of the mass video * * @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 Submitted Parameters 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; }Results returned by mass
package com.phil.wechat.msg.model.resp; import com.phil.wechat.base.result.ResultState; /** * Result returned by mass messages* Bulk sending according to OpenID list* @author phil * @date July 2, 2017* */ public class MassMsgResult extends ResultState{ private String type; //Media file types include image (image), voice (voice), video (video) and thumbnail (thumb), and the number of times is news, that is, graphic and text messages private String msg_id; private String msg_data_id; }Configuration class
// Send mass by group public static final String SEND_ALL_MASS_MESSAGE_URL = "https://api.weixin.qq.com/cgi-bin/message/mass/sendall"; // Send mass messages according to openid (at least 2 OpenIDs, up to 10,000 10,000) public static final String SEND_MASS_MESSAGE_URL = "https://api.weixin.qq.com/cgi-bin/message/mass/send"; // Upload graphic and text message material public static final String UPLOAD_NEWS_MEDIA_URL = "https://api.weixin.qq.com/cgi-bin/media/uploadnews"; //Get media_id public static final String UPLOAD_VIDEO_MEDIA_URL = "https://api.weixin.qq.com/cgi-bin/media/uploadvideo" in the mass video post;
Regarding the source code, I hope it helps you
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.