再次吐槽下,微信素材管理和群发这块文档对Java很不友好,此文需要结合我前文和官方文档。
测试号调试群发只需看是否群发消息是否能组装成功,不需要看结果如何(反正不会发送成功的),因为微信还没开放这个功能(估计也不会开放的)。
在公众平台网站上,为订阅号提供了每天一条的群发权限,为服务号提供每月(自然月)4条的群发权限。
1、对于认证订阅号,群发接口每天可成功调用1次,此次群发可选择发送给全部用户或某个标签;
2、对于认证服务号虽然开发者使用高级群发接口的每日调用限制为100次,但是用户每月只能接收4条,无论在公众平台网站上,还是使用接口群发,用户每月只能接收4条群发消息,多于4条的群发将对该用户发送失败;
3、开发者可以主动设置 clientmsgid 来避免重复推送。
4、群发接口每分钟限制请求60次,超过限制的请求会被拒绝。
5、图文消息正文中插入自己帐号和其他公众号已群发文章链接的能力。
4、在上述过程中,如果需要,还可以预览图文消息、查询群发状态,或删除已群发的消息等。
2、如果是群发图片、视频等消息,则需要预先通过素材管理接口准备好 mediaID。
用于设定是否向全部用户发送,值为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
@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 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 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 params = new TreeMap<>();
params.put("access_token", accessToken);
// post 提交的参数
Map filterParams = new HashMap<>();
filterParams.put("is_to_all", false);
filterParams.put("tag_id", tagId);
Map textParams = new HashMap<>();
textParams.put("content", content);
TreeMap 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 params = new TreeMap<>();
params.put("access_token", accessToken);
// post 提交的参数
Map filterParams = new HashMap<>();
filterParams.put("is_to_all", false);
filterParams.put("tag_id", tagId);
Map mpnewsParams = new HashMap<>();
mpnewsParams.put("media_id", mediaId);
TreeMap 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 params = new TreeMap<>();
params.put("access_token", accessToken);
// post 提交的参数
Map filterParams = new HashMap<>();
filterParams.put("is_to_all", false);
filterParams.put("tag_id", tagId);
Map imageParams = new HashMap<>();
imageParams.put("media_id", mediaId);
TreeMap 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 params = new TreeMap<>();
params.put("access_token", accessToken);
// post 提交的参数
Map filterParams = new HashMap<>();
filterParams.put("is_to_all", false);
filterParams.put("tag_id", tagId);
Map voiceParams = new HashMap<>();
voiceParams.put("media_id", mediaId);
TreeMap 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 params = new TreeMap<>();
params.put("access_token", accessToken);
// post 提交的参数
Map filterParams = new HashMap<>();
filterParams.put("is_to_all", false);
filterParams.put("tag_id", tagId);
Map mpvideoParams = new HashMap<>();
mpvideoParams.put("media_id", mediaId);
TreeMap 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 params = new TreeMap<>();
params.put("access_token", accessToken);
// post 提交的参数
Map filterParams = new HashMap<>();
filterParams.put("is_to_all", false);
filterParams.put("tag_id", tagId);
Map wxcardParams = new HashMap<>();
wxcardParams.put("card_id", cardId);
TreeMap 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 openids, String mediaId){
MassMsgResult result = null;
TreeMap params = new TreeMap<>();
params.put("access_token", accessToken);
// post 提交的参数
Map mpnewsParams = new HashMap<>();
mpnewsParams.put("media_id", mediaId);
TreeMap 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 openids, String content){
MassMsgResult result = null;
TreeMap params = new TreeMap<>();
params.put("access_token", accessToken);
// post 提交的参数
Map textParams = new HashMap<>();
textParams.put("content", content);
TreeMap 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 openids, String mediaId){
MassMsgResult result = null;
TreeMap params = new TreeMap<>();
params.put("access_token", accessToken);
// post 提交的参数
Map voiceParams = new HashMap<>();
voiceParams.put("media_id", mediaId);
TreeMap 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 openids, String mediaId){
MassMsgResult result = null;
TreeMap params = new TreeMap<>();
params.put("access_token", accessToken);
// post 提交的参数
Map imageParams = new HashMap<>();
imageParams.put("media_id", mediaId);
TreeMap 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 openids, MpVideoMedia mpVideoMedia){
MassMsgResult result = null;
TreeMap params = new TreeMap<>();
params.put("access_token", accessToken);
// post 提交的参数
TreeMap 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 openids, String cardId){
MassMsgResult result = null;
TreeMap params = new TreeMap<>();
params.put("access_token", accessToken);
// post 提交的参数
Map wxcardParams = new HashMap<>();
wxcardParams.put("card_id", cardId);
TreeMap 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 params = new TreeMap();
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";
关于源码,希望帮到了你