微信开发六 发送消息(客服/群发/模板)
本例中有些引用的类与方法不做过多介绍,之后会提供完整源码下载,请自行查看。
本篇根据开发者文档-发送消息编写。请对照查看,一些传入与返回参数就不过多介绍。地址为: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;// @Testpublic void addKFAccount() {KFAccount kfAccount = new KFAccount();kfAccount.setKf_account("test1@wx2081831e3e9f99fb");kfAccount.setNickname("小白");kfAccount.setPassword("hq246512");System.out.println(">>>"+customServiceService.addKFAccount(kfAccount));}// @Testpublic void updateKFAccount() {KFAccount kfAccount = new KFAccount();kfAccount.setKf_account("975656343@qq.com");kfAccount.setNickname("大白");kfAccount.setPassword("hq246512");System.out.println(">>>"+customServiceService.updateKFAccount(kfAccount));}// @Testpublic void deleteKFAccount() {KFAccount kfAccount = new KFAccount();kfAccount.setKf_account("975656343@qq.com");kfAccount.setNickname("大白");kfAccount.setPassword("hq246512");System.out.println(">>>"+customServiceService.deleteKFAccount(kfAccount));}// @Testpublic void setKFAccountHeadImg() {System.out.println(">>>"+customServiceService.setKFAccountHeadImg("D:\\test\\head.jpg", "975656343@qq.com"));}// @Testpublic void getAllKFAccount() {System.out.println(">>>"+customServiceService.getAllKFAccount());}// @Testpublic 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_idpublic 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;// @Testpublic void getUrlByUploadImg() {//curl: (60) SSL certificate problem: unable to get local issuer certificate 请查看 Remark第五条System.out.println(">>>"+massSendMsgService.getUrlByUploadImg("D:/test/test.jpg"));}// @Testpublic void getVideoMediaIdMassMsg() {Material massMsg = new Material();massMsg.setMedia_id(WeChatCertificate.VIDEO_MEDIAID);massMsg.setTitle("title");massMsg.setDescription("description");System.out.println(">>>"+massSendMsgService.getVideoMediaIdMassMsg(massMsg));}// @Testpublic 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));}// @Testpublic 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));}// @Testpublic 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));}// @Testpublic void deleteMassMsg() {Material mass = new Material();mass.setMsg_id(WeChatCertificate.MSG_ID);System.out.println(">>>"+massSendMsgService.deleteMassMsg(mass));}// @Testpublic 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));}// @Testpublic 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; //公众帐号下模板消息IDpublic String touser; //是 接收者openidpublic 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;// @Testpublic void setIndustry() {MessageModel industry = new MessageModel();industry.setIndustry_id1("2");industry.setIndustry_id2("31");System.out.println(">>>"+msgModelService.setIndustry(industry));}// @Testpublic void getIndustry() {System.out.println(">>>"+msgModelService.getIndustry());}// @Testpublic void getIdByaddTemplate() {MessageModel model = new MessageModel();model.setTemplate_id_short("TM00015");System.out.println(">>>"+msgModelService.getIdByaddTemplate(model));}// @Testpublic void deleteTemplate() {MessageModel model = new MessageModel();model.setTemplate_id(WeChatCertificate.MODELID);System.out.println(">>>"+msgModelService.deleteTemplate(model));}// @Testpublic void getAllTemplate() {System.out.println(">>>"+msgModelService.getAllTemplate());}// @Testpublic 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));} }
微信开发六 发送消息(客服/群发/模板)相关推荐
- 微信开发之发送消息接口
发送消息,是指用户公众号向用户发送相应形式的消息.根据微信开发文档,由以下四种形式:被动回复,群发接口,客服消息接口以及模板消息接口.本文将基于Java语言以及个人微信测试号,说明被动回复.客服消息接 ...
- 微信公众号怎么推送消息_微信公众号发送消息
A.模板消息发送 模板消息仅用于公众号向用户发送重要的服务通知,只能用于符合其要求的服务场景中,如信用卡刷卡通知,商品购买成功通知等.不支持广告等营销类消息以及其它所有可能对用户造成骚扰的消息. 备注 ...
- 【微信公众号】微信集成功能--接入多客服系统
目录 需求来源 实现思路 1.邀请指定人员成为客服角色: 2.用户在公众号发送信息,通过关键字触发事件:通知客服: 3.客服角色接入会话,与用户对话沟通交流解决疑问,完成对话: 代码实现(基于Lara ...
- php微信公众号向指定客服发信息,微信公众号给用户发送一条消息 客服消息
可以用客服消息接口或模板消息接口实现.但是需要认证公众号才能有权限. 认证服务号可以发送客服接口消息[需要对应openid24小时内有互动]和模板消息,订阅号则没有模板消息权限. 下面是发送客服消息的 ...
- 微信公众号开发教程[019]-新版客服
新版客服是针对之前文章<<微信公众号开发教程[007]-消息管理-客服消息>>里面提到的客服功能而言的.在其基础上做了很多提升. 首先如果公众号曾经使用过旧版多客服功能,则要在 ...
- java企业微信发送语言_java微信企业号开发之发送消息(文本、图片、语音)
上篇文章介绍了开启回调模式,开始回调模式后我们就要实现聊天功能了.平时使用微信聊天可以发送文本消息.语音.图片.视频等,这里只实现了其中的一些功能和大家分享. 一.与微信企业号建立连接1.企业应用调用 ...
- 微信企业号开发(6)--发送消息
[本文所描述的内容具有一定的时效性,请以实际开发所处环境为准] 今天我们介绍我们日常使用最频繁的发送消息功能,在企业号中,由于移除了诸多的限制,消息的发送量几乎没有限制.但是,在此,特别提醒大家,千万 ...
- python企业微信回调_Python微信企业号开发之回调模式接收微信端客户端发送消息及被动返回消息示例...
本文实例讲述了Python微信企业号开发之回调模式接收微信端客户端发送消息及被动返回消息.分享给大家供大家参考,具体如下: 说明:此代码用于接收手机微信端发送的消息 #-*- coding:utf-8 ...
- 微信公众号接入多客服系统,功能使用说明
微信公众号在用户获取和服务方面发挥着重大作用,那么有没有好用的公众号运营工具来辅助粉丝运营呢,不仅仅解决沟通而已,还承担着私域流量,后续转化等. 公众号怎么接入客服系统?哪些亮点功能提示运营/服务效率 ...
最新文章
- Nature:1000种植物的测序揭示10亿年来的进化
- elastichd安装部署
- JNDI 笔记(一) 概述
- 如何:为 Windows 窗体 RichTextBox 控件设置字体属性
- 使用GDI+实现圆形进度条控件的平滑效果
- python客户价值分析_[Python数据挖掘]第7章、航空公司客户价值分析
- flink 写kafka_flink消费kafka的offset与checkpoint
- ECS实践案例丨逻辑卷的创建和扩容操作指导
- Java String 类的方法
- VS2017 启动调试出现 无法启动程序“http://localhost:15613” 操作在当前状态中是非法的。 同时附加进程也是错误的解决方法
- java 创建本地文件、写入文本内容、调用IE浏览器打开
- 机器学习:更多的数据总是优于更好的算法吗?
- guided Filter--引导滤波算法原理及实现
- 锐捷长ping_锐捷实战系列(二) Wireshark抓包分析Ping过程
- java开发html如何转换为word,Java怎么将html转换成word
- 华人教授世界一流大学观察报告:斯坦福师生吃饭时,谈论的都是什么话题?...
- 中小企业常遇到这些问题,看APS系统是如何解决的
- 计算机电源接口在哪,电脑电源接口定义图解
- 刚开始学Python
- idea的git报错You have not concluded your merge