主动发送客服消息,示例:
// 客服消息、图文消息
// 图文消息1
WxMpKefuMessage.WxArticle article1 = new WxMpKefuMessage.WxArticle();
article1.setUrl("http://xial9527.w3.luyouxia.net/wx/jsp/login.html");
article1.setPicUrl("http://test.iwocloud.com/wx/images/index_bg.png");
article1.setDescription("今天天气很好,适合出去玩");
article1.setTitle("测试测试测试一下啦啦啦");
// 图文消息2
WxMpKefuMessage.WxArticle article2 = new WxMpKefuMessage.WxArticle();
article2.setUrl("http://xial9527.w3.luyouxia.net/wx/index?code=1YF00137");
article2.setPicUrl("http://test.iwocloud.com/wx/images/index_bg.png");
article2.setDescription("晚上去吃火锅吧,逛超市");
article2.setTitle("今日安排");
// 封装 xml
WxMpKefuMessage wxMpKefuMessage = WxMpKefuMessage.NEWS()
.toUser("otAIM6PimOuARRXXTWcloLL-XLU0")
.addArticle(article1)
//.addArticle(article2)
.build();
// 发送给指定openid
wxMpService.getKefuService().sendKefuMessage(wxMpKefuMessage);
更多用例,请参考me.chanjar.weixin.mp.api.impl.WxMpKefuServiceImplTest,用例连接:
https://github.com/Wechat-Group/WxJava/blob/master/weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/impl/WxMpKefuServiceImplTest.java
WxMpKefuMessage message = ...;
// 设置消息的内容等信息
wxMpService.getKefuService().sendKefuMessage(message);
不同类型的客服消息有不同的构造方法:
WxMpKefuMessage
.TEXT()
.toUser("OPENID")
.content("sfsfdsdf")
.build();
WxMpKefuMessage
.IMAGE()
.toUser("OPENID")
.mediaId("MEDIA_ID")
.build();
WxMpKefuMessage.VOICE()
.toUser("OPENID")
.mediaId("MEDIA_ID")
.build();
WxMpKefuMessage.VIDEO()
.toUser("OPENID")
.title("TITLE")
.mediaId("MEDIA_ID")
.thumbMediaId("MEDIA_ID")
.description("DESCRIPTION")
.build();
WxMpKefuMessage.MUSIC()
.toUser("OPENID")
.title("TITLE")
.thumbMediaId("MEDIA_ID")
.description("DESCRIPTION")
.musicUrl("MUSIC_URL")
.hqMusicUrl("HQ_MUSIC_URL")
.build();
WxMpKefuMessage.WxArticle article1 = new WxMpKefuMessage.WxArticle();
article1.setUrl("URL");
article1.setPicUrl("PIC_URL");
article1.setDescription("Is Really A Happy Day");
article1.setTitle("Happy Day");
WxMpKefuMessage.WxArticle article2 = new WxMpKefuMessage.WxArticle();
article2.setUrl("URL");
article2.setPicUrl("PIC_URL");
article2.setDescription("Is Really A Happy Day");
article2.setTitle("Happy Day");
WxMpKefuMessage.NEWS()
.toUser("OPENID")
.addArticle(article1)
.addArticle(article2)
.build();
参考文档地址:
微信官方文档(客服消息):https://developers.weixin.qq.com/doc/offiaccount/Message_Management/Service_Center_messages.html
WxJava (微信开发 Java SDK)框架文档:
https://github.com/Wechat-Group/WxJava/wiki/MP_%E4%B8%BB%E5%8A%A8%E5%8F%91%E9%80%81%E6%B6%88%E6%81%AF%EF%BC%88%E5%AE%A2%E6%9C%8D%E6%B6%88%E6%81%AF%EF%BC%89
完!!!
示例:
/**
* 图文消息推送(客服消息推送)
*
* @param req
* @param resp
* @return
* @throws Exception
*/
@RequestMapping(value = "/notifyPreferential.api")
@ResponseBody
public Map notifyPreferential(HttpServletRequest req, HttpServletResponse resp) {
wxMpService.setMaxRetryTimes(1);
// 获取用户手机号、密码、action
String phone = (String) req.getParameter("tellphone");
String openid = (String) req.getParameter("openid");
String title = (String) req.getParameter("title");
String desc = (String) req.getParameter("desc");
String picUrl = (String) req.getParameter("picUrl");
String status = (String) req.getParameter("status");
String startTime = (String) req.getParameter("startTime");
String endTime = (String) req.getParameter("endTime");
logger.info("收到服务器图文消息推送:请求参数为" + phone + "," + openid + "," + title + "," + picUrl + "," + status);
try { /*
// 客服消息、图文消息
// 图文消息1
WxMpKefuMessage.WxArticle article1 = new WxMpKefuMessage.WxArticle();
article1.setUrl("http://xial9527.w3.luyouxia.net/wx/jsp/login.html");
article1.setPicUrl("http://test.iwocloud.com/wx/images/index_bg.png");
article1.setDescription("今天天气很好,适合出去玩");
article1.setTitle("测试测试测试一下啦啦啦");
// 图文消息2
WxMpKefuMessage.WxArticle article2 = new WxMpKefuMessage.WxArticle();
article2.setUrl("http://xial9527.w3.luyouxia.net/wx/index?code=1YF00137");
article2.setPicUrl("http://test.iwocloud.com/wx/images/index_bg.png");
article2.setDescription("晚上去吃火锅吧,逛超市");
article2.setTitle("今日安排");
// 封装 xml
WxMpKefuMessage wxMpKefuMessage = WxMpKefuMessage.NEWS()
.toUser("otAIM6PimOuARRXXTWcloLL-XLU0")
.addArticle(article1)
//.addArticle(article2)
.build();
// 发送给指定openid
wxMpService.getKefuService().sendKefuMessage(wxMpKefuMessage); */
InputStream inputStream = null;
HttpURLConnection conn = (HttpURLConnection) new URL("http://test.iwocloud.com/wx/images/index_bg.png").openConnection();
conn.setReadTimeout(5000);
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
inputStream = conn.getInputStream();
}
/*
String path = this.getClass().getResource("/").getPath();
path = path.substring(0, path.indexOf("WEB-INF")) + "images/index_bg.png";
logger.info(path);
inputStream = new FileInputStream(new File(path)); */
// 上传图文消息的封面图片
WxMediaUploadResult uploadMediaRes = wxMpService.getMaterialService().mediaUpload(WxConsts.MediaFileType.IMAGE, "png", inputStream);
WxMpMassNews news = new WxMpMassNews();
WxMpMassNews.WxMpMassNewsArticle article1 = new WxMpMassNews.WxMpMassNewsArticle();
article1.setTitle("标题1");
article1.setContent("内容1");
article1.setThumbMediaId(uploadMediaRes.getMediaId());
//article1.setThumbMediaId("67MYWryQ20BhSCeG8DPavcpHubxbjk9OPBlDJpkywXJi16kec7Pjs7Ul-H8ubN3T");
news.addArticle(article1);
WxMpMassNews.WxMpMassNewsArticle article2 = new WxMpMassNews.WxMpMassNewsArticle();
article2.setTitle("标题2");
article2.setContent("内容2");
article2.setThumbMediaId(uploadMediaRes.getMediaId());
//article2.setThumbMediaId("67MYWryQ20BhSCeG8DPavcpHubxbjk9OPBlDJpkywXJi16kec7Pjs7Ul-H8ubN3T");
article2.setShowCoverPic(true);
article2.setAuthor("作者2");
article2.setContentSourceUrl("www.baidu.com");
article2.setDigest("摘要2");
news.addArticle(article2);
WxMpMassUploadResult massUploadResult = wxMpService.getMassMessageService().massNewsUpload(news);
WxMpMassOpenIdsMessage massMessage = new WxMpMassOpenIdsMessage();
massMessage.setMsgType(WxConsts.MassMsgType.MPNEWS);
massMessage.setMediaId(massUploadResult.getMediaId());
//massMessage.setMediaId("67MYWryQ20BhSCeG8DPavcpHubxbjk9OPBlDJpkywXJi16kec7Pjs7Ul-H8ubN3T");
massMessage.getToUsers().add("otAIM6PimOuARRXXTWcloLL-XLU0");
massMessage.getToUsers().add("otAIM6OlscpCu4PSFb8dSM0jwIxE");
WxMpMassSendResult massResult = wxMpService.getMassMessageService().massOpenIdsMessageSend(massMessage);
} catch (Exception e) {
logger.error(e.getMessage());
}
// 封装返回结果
Map returnMap = new HashMap();
returnMap.put(GlobalDeclare.RETURN_CODE, 0);
returnMap.put(GlobalDeclare.RETURN_MSG, "success");
returnMap.put(GlobalDeclare.RETURN_DATA, "{}");
return returnMap;
}