本例中有些引用的类与方法不做过多介绍,之后会提供完整源码下载,请自行查看。
本篇根据开发者文档-发送消息编写。请对照查看,一些传入与返回参数就不过多介绍。地址为:https://mp.weixin.qq.com/wiki/14/0c53fac3bdec3906aaa36987b91d64ea.html
客服接口
当用户主动发消息给公众号的时候(包括发送信息、点击自定义菜单、订阅事件、扫描二维码事件、支付成功事件、用户维权),微信将会把消息数据推送给开发者,开发者在一段时间内(目前修改为48小时)可以调用客服消息接口,通过POST一个JSON数据包来发送消息给普通用户,在48小时内不限制发送次数。此接口主要用于客服等有人工消息处理环节的功能,方便开发者为用户提供更加优质的服务。
为了帮助公众号使用不同的客服身份服务不同的用户群体,客服接口进行了升级,开发者可以管理客服账号,并设置客服账号的头像和昵称。该能力针对所有拥有客服接口权限的公众号开放。
/* * 客服 * http://mp.weixin.qq.com/wiki/1/70a29afed17f56d537c833f89be979c9.html */ public class KFAccount { private String kf_account; //是 完整客服账号,格式为:账号前缀@公众号微信号 private String kf_nick; //是 客服昵称 private String kf_id; //是 客服工号 private String nickname; //是 客服昵称,最长6个汉字或12个英文字符 private String password; //否 客服账号登录密码,格式为密码明文的32位加密MD5值。该密码仅用于在公众平台官网的多客服功能中使用,若不使用多客服功能,则不必设置密码 private String media; //是 该参数仅在设置客服头像时出现,是form-data中媒体文件标识,有filename、filelength、content-type等信息 }
@RunWith(SpringJUnit4ClassRunner.class) // 表示继承了 SpringJUnit4ClassRunner 类 @ContextConfiguration("classpath:spring-mybatis.xml") public class TestWXCustomService { @Resource private CustomServiceService customServiceService; // @Test public void addKFAccount() { KFAccount kfAccount = new KFAccount(); kfAccount.setKf_account("test1@wx2081831e3e9f99fb"); kfAccount.setNickname("小白"); kfAccount.setPassword("hq246512"); System.out.println(">>>"+customServiceService.addKFAccount(kfAccount)); } // @Test public void updateKFAccount() { KFAccount kfAccount = new KFAccount(); kfAccount.setKf_account("975656343@qq.com"); kfAccount.setNickname("大白"); kfAccount.setPassword("hq246512"); System.out.println(">>>"+customServiceService.updateKFAccount(kfAccount)); } // @Test public void deleteKFAccount() { KFAccount kfAccount = new KFAccount(); kfAccount.setKf_account("975656343@qq.com"); kfAccount.setNickname("大白"); kfAccount.setPassword("hq246512"); System.out.println(">>>"+customServiceService.deleteKFAccount(kfAccount)); } // @Test public void setKFAccountHeadImg() { System.out.println(">>>"+customServiceService.setKFAccountHeadImg("D:\\test\\head.jpg", "975656343@qq.com")); } // @Test public void getAllKFAccount() { System.out.println(">>>"+customServiceService.getAllKFAccount()); } // @Test public void sendMessageByKF() { KFResponse kfResponse = new KFResponse(); kfResponse.setTouser(WeChatCertificate.OPENID1); //发送文字消息 kfResponse.setMsgtype("text"); KFResponse.Text text = kfResponse.new Text(); text.setContent("hello, this is kf response!"); kfResponse.setText(text); //发送音乐消息 注:40007需重新上传素材,Remark->第三条。 // kfResponse.setMsgtype("music"); // KFResponse.Music music = kfResponse.new Music(); // music.setTitle("残酷月光"); // music.setDescription(" 林宥嘉 词:向月娥 曲:陈小霞"); // music.setMusicurl("http://wma.5282.cc/2008-11//20140226/1.mp3"); // music.setHqmusicurl("http://wma.5282.cc/2008-11//20140226/1.mp3"); // music.setThumb_media_id(WeChatCertificate.VIDEOMEDIAID); // kfResponse.setMusic(music); //发送图文消息 // kfResponse.setMsgtype("news"); // KFResponse.News news = kfResponse.new News(); // for(int i=0; i<3; i++) { // KFResponse.News.Article article = news.new Article(); // article.setTitle("title"+i); // article.setDescription("description"+i); // article.setPicurl("picurl"+i); // article.setUrl("url"+i); // news.getArticles().add(article); // } // kfResponse.setNews(news); //某个客服帐号来发消息 // KFResponse.CustomService customservice = kfResponse.new CustomService(); // customservice.setKF_Account("975656343@qq.com"); // kfResponse.setCustomservice(customservice); System.out.println(">>>"+customServiceService.sendMessageByKF(kfResponse)); } }
#
在公众平台网站上,为订阅号提供了每天一条的群发权限,为服务号提供每月(自然月)4条的群发权限。而对于某些具备开发能力的公众号运营者,可以通过高级群发接口,实现更灵活的群发能力。
/* * 根据标签进行群发【订阅号与服务号认证后均可用】 * https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140549&token=&lang=zh_CN */ public class MassSendResponse { private String msgtype; //群发的消息类型, private Filter filter; //用于设定图文消息的接收者 private String previewUser;//用于设定图文消息的接收者 private List<String> touser;//填写图文消息的接收者,一串OpenID列表,OpenID最少2个,最多10000个 private MPNews mpnews; //图文消息(注意图文消息的media_id需要通过上述方法来得到): private MPVideo mpvideo; //此处视频的media_id需通过POST请求WeChatInterface.uploadVideo的返回结果获得 private Text text; //文本 private Voice voice; //语音(注意此处media_id需通过基础支持中的上传下载多媒体文件来得到): private Image image; //图片(注意此处media_id需通过基础支持中的上传下载多媒体文件来得到): private WXCard wxcard; //卡券消息(注意图文消息的media_id需要通过上述方法来得到) public String getMsgtype() { return msgtype; } public void setMsgtype(String msgtype) { this.msgtype = msgtype; } public Filter getFilter() { return filter; } public void setFilter(Filter filter) { this.filter = filter; } public MPNews getMpnews() { return mpnews; } public void setMpnews(MPNews mpnews) { this.mpnews = mpnews; } public MPVideo getMpvideo() { return mpvideo; } public void setMpvideo(MPVideo mpvideo) { this.mpvideo = mpvideo; } public Text getText() { return text; } public void setText(Text text) { this.text = text; } public Voice getVoice() { return voice; } public void setVoice(Voice voice) { this.voice = voice; } public Image getImage() { return image; } public void setImage(Image image) { this.image = image; } public WXCard getWxcard() { return wxcard; } public void setWxcard(WXCard wxcard) { this.wxcard = wxcard; } public List<String> getTouser() { if(touser==null) touser= new ArrayList<String>(); return touser; } public void setTouser(List<String> touser) { this.touser = touser; } public String getPreviewUser() { return previewUser; } public void setPreviewUser(String previewUser) { this.previewUser = previewUser; } public class Filter{ private boolean is_to_all; //用于设定是否向全部用户发送,值为true或false,选择true该消息群发给所有用户,选择false可根据tag_id发送给指定群组的用户 private String group_id; //群发到的分组的group_id.若is_to_all值为true,可不填写group_id public boolean getIs_to_all() { return is_to_all; } public void setIs_to_all(boolean is_to_all) { this.is_to_all = is_to_all; } public String getGroup_id() { return group_id; } public void setGroup_id(String group_id) { this.group_id = group_id; } } public class MPNews{ private String media_id; //用于群发的消息的media_id public String getMedia_id() { return media_id; } public void setMedia_id(String media_id) { this.media_id = media_id; } } public class MPVideo{ private String media_id; public String getMedia_id() { return media_id; } public void setMedia_id(String media_id) { this.media_id = media_id; } } public class Text{ private String content; public String getContent() { return content; } public void setContent(String content) { this.content = content; } } public class Voice{ private String media_id; public String getMedia_id() { return media_id; } public void setMedia_id(String media_id) { this.media_id = media_id; } } public class Image{ private String media_id; public String getMedia_id() { return media_id; } public void setMedia_id(String media_id) { this.media_id = media_id; } } public class WXCard{ private String media_id; public String getMedia_id() { return media_id; } public void setMedia_id(String media_id) { this.media_id = media_id; } } }
@RunWith(SpringJUnit4ClassRunner.class) // 表示继承了 SpringJUnit4ClassRunner 类 @ContextConfiguration("classpath:spring-mybatis.xml") public class TestWXMassSendMsg { @Resource private MassSendMsgService massSendMsgService; // @Test public void getUrlByUploadImg() { //curl: (60) SSL certificate problem: unable to get local issuer certificate 请查看 Remark第五条 System.out.println(">>>"+massSendMsgService.getUrlByUploadImg("D:/test/test.jpg")); } // @Test public void getVideoMediaIdMassMsg() { Material massMsg = new Material(); massMsg.setMedia_id(WeChatCertificate.VIDEO_MEDIAID); massMsg.setTitle("title"); massMsg.setDescription("description"); System.out.println(">>>"+massSendMsgService.getVideoMediaIdMassMsg(massMsg)); } // @Test public void uploadNews() { Material mass = new Material(); for(int i=0; i<1; i++) { Material.Article article = mass.new Article(); article.setThumb_media_id(WeChatCertificate.THUMB_MEDIA_ID); article.setAuthor("Author"+i); article.setTitle("Title"+i); article.setContent_source_url("www.baidu.com"); article.setContent("Content"+i); article.setDigest("Digest"+i); article.setShow_cover_pic(article.SHOW); mass.getArticles().add(article); } System.out.println(">>>"+massSendMsgService.uploadNews(mass)); } // @Test public void massMsgByPacket() { MassSendResponse mass = new MassSendResponse(); mass.setMsgtype("mpnews"); MassSendResponse.Filter filter = mass.new Filter(); filter.setIs_to_all(false); filter.setGroup_id("101"); mass.setFilter(filter); MassSendResponse.MPNews mpnews = mass.new MPNews(); mpnews.setMedia_id(WeChatCertificate.NEWS_ID); mass.setMpnews(mpnews); System.out.println(">>>"+massSendMsgService.massMsgByPacket(mass)); } // @Test public void massMsgByOpenID() { MassSendResponse mass = new MassSendResponse(); mass.setMsgtype("mpnews"); mass.getTouser().add(WeChatCertificate.OPENID1); mass.getTouser().add(WeChatCertificate.OPENID2); MassSendResponse.MPNews mpnews = mass.new MPNews(); mpnews.setMedia_id(WeChatCertificate.NEWS_ID); mass.setMpnews(mpnews); System.out.println(">>>"+massSendMsgService.massMsgByOpenID(mass)); } // @Test public void deleteMassMsg() { Material mass = new Material(); mass.setMsg_id(WeChatCertificate.MSG_ID); System.out.println(">>>"+massSendMsgService.deleteMassMsg(mass)); } // @Test public void previewMassMsg() { MassSendResponse mass = new MassSendResponse(); mass.setMsgtype("mpnews"); mass.setPreviewUser(WeChatCertificate.OPENID1); MassSendResponse.MPNews mpnews = mass.new MPNews(); mpnews.setMedia_id(WeChatCertificate.NEWS_ID); mass.setMpnews(mpnews); System.out.println(">>>"+massSendMsgService.previewMassMsg(mass)); } // @Test public void getMassMsgStatus() { Material mass = new Material(); mass.setMsg_id(WeChatCertificate.MSG_ID); System.out.println(">>>"+massSendMsgService.getMassMsgStatus(mass)); } }
模板消息接口
模板消息仅用于公众号向用户发送重要的服务通知,只能用于符合其要求的服务场景中,如信用卡刷卡通知,商品购买成功通知等。不支持广告等营销类消息以及其它所有可能对用户造成骚扰的消息。
/* * 模板消息 * http://mp.weixin.qq.com/wiki/17/304c1885ea66dbedf7dc170d84999a9d.html */ public class MessageModel { public String industry_id1; //公众号模板消息所属行业编号 public String industry_id2; //公众号模板消息所属行业编号 public String template_id_short; //模板库中模板的编号,有“TM**”和“OPENTMTM**”等形式 public String template_id; //公众帐号下模板消息ID public String touser; //是 接收者openid public String url; //否 模板跳转链接 public String data; //是 模板数据 public String getTouser() { return touser; } public void setTouser(String touser) { this.touser = touser; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String getData() { return data; } public void setData(String data) { this.data = data; } public String getTemplate_id() { return template_id; } public void setTemplate_id(String template_id) { this.template_id = template_id; } public String getTemplate_id_short() { return template_id_short; } public void setTemplate_id_short(String template_id_short) { this.template_id_short = template_id_short; } public String getIndustry_id1() { return industry_id1; } public void setIndustry_id1(String industry_id1) { this.industry_id1 = industry_id1; } public String getIndustry_id2() { return industry_id2; } public void setIndustry_id2(String industry_id2) { this.industry_id2 = industry_id2; } }
@RunWith(SpringJUnit4ClassRunner.class) // 表示继承了 SpringJUnit4ClassRunner 类 @ContextConfiguration("classpath:spring-mybatis.xml") public class TestWXMsgModel { @Resource private MsgModelService msgModelService; // @Test public void setIndustry() { MessageModel industry = new MessageModel(); industry.setIndustry_id1("2"); industry.setIndustry_id2("31"); System.out.println(">>>"+msgModelService.setIndustry(industry)); } // @Test public void getIndustry() { System.out.println(">>>"+msgModelService.getIndustry()); } // @Test public void getIdByaddTemplate() { MessageModel model = new MessageModel(); model.setTemplate_id_short("TM00015"); System.out.println(">>>"+msgModelService.getIdByaddTemplate(model)); } // @Test public void deleteTemplate() { MessageModel model = new MessageModel(); model.setTemplate_id(WeChatCertificate.MODELID); System.out.println(">>>"+msgModelService.deleteTemplate(model)); } // @Test public void getAllTemplate() { System.out.println(">>>"+msgModelService.getAllTemplate()); } // @Test public void sendTemplate() { MessageModel model = new MessageModel(); model.setTouser(WeChatCertificate.OPENID2); model.setTemplate_id(WeChatCertificate.MODELID); model.setUrl("http://www.baidu.com"); System.out.println(">>>"+msgModelService.sendTemplate(model)); } }