微信公众平台 客服接口发消息的实现代码(Java接口开发)
微信公众平台技术文档:
一、接口说明
当用户和公众号产生特定动作的交互时(具体动作列表请见下方说明),微信将会把消息数据推送给开发者,开发者可以在一段时间内(目前修改为48小时)调用客服接口,通过post一个json数据包来发送消息给普通用户。此接口主要用于客服等有人工消息处理环节的功能,方便开发者为用户提供更加优质的服务。
目前允许的动作列表如下(公众平台会根据运营情况更新该列表,不同动作触发后,允许的客服接口下发消息条数不同,下发条数达到上限后,会遇到错误返回码,具体请见返回码说明页):
1、用户发送信息
2、点击自定义菜单(仅有点击推事件、扫码推事件、扫码推事件且弹出“消息接收中”提示框这3种菜单类型是会触发客服接口的)
3、关注公众号
4、扫描二维码
5、支付成功
6、用户维权
二、客服接口-发消息
1 接口调用请求说明
http请求方式: post
https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=access_token
2 发送客服消息
各消息类型所需的json数据包如下:
(1)发送文本消息
{ "touser":"openid", "msgtype":"text", "text": { "content":"hello world" } }
(2)发送图片消息
{ "touser":"openid", "msgtype":"image", "image": { "media_id":"media_id" } }
(3)发送语音消息
{ "touser":"openid", "msgtype":"voice", "voice": { "media_id":"media_id" } }
(4)发送视频消息
{ "touser":"openid", "msgtype":"video", "video": { "media_id":"media_id", "thumb_media_id":"media_id", "title":"title", "description":"description" } }
(5)发送音乐消息
{ "touser":"openid", "msgtype":"music", "music": { "title":"music_title", "description":"music_description", "musicurl":"music_url", "hqmusicurl":"hq_music_url", "thumb_media_id":"thumb_media_id" } }
(6)发送图文消息(点击跳转到外链) 图文消息条数限制在8条以内,注意,如果图文数超过8,则将会无响应。
{ "touser":"openid", "msgtype":"news", "news":{ "articles": [ { "title":"happy day", "description":"is really a happy day", "url":"url", "picurl":"pic_url" }, { "title":"happy day", "description":"is really a happy day", "url":"url", "picurl":"pic_url" } ] } }
(7)发送图文消息(点击跳转到图文消息页面) 图文消息条数限制在8条以内,注意,如果图文数超过8,则将会无响应。
{ "touser":"openid", "msgtype":"mpnews", "mpnews": { "media_id":"media_id" } }
(8)发送卡券
{ "touser":"openid", "msgtype":"wxcard", "wxcard":{ "card_id":"123dsdajkasd231jhksad" }, }
特别注意客服消息接口投放卡券仅支持非自定义code码和导入code模式的卡券的卡券,详情请见:。
请注意,如果需要以某个客服帐号来发消息(在微信6.0.2及以上版本中显示自定义头像),则需在json数据包的后半部分加入customservice参数,例如发送文本消息则改为:
{ "touser":"openid", "msgtype":"text", "text": { "content":"hello world" }, "customservice": { "kf_account": "test1@kftest" } }
3 请求参数说明
参数 | 是否必须 | 说明 |
---|---|---|
access_token | 是 | 调用接口凭证 |
touser | 是 | 普通用户openid |
msgtype | 是 | 消息类型,文本为text,图片为image,语音为voice,视频消息为video,音乐消息为music,图文消息(点击跳转到外链)为news,图文消息(点击跳转到图文消息页面)为mpnews,卡券为wxcard |
content | 是 | 文本消息内容 |
media_id | 是 | 发送的图片/语音/视频/图文消息(点击跳转到图文消息页)的媒体id |
thumb_media_id | 是 | 缩略图的媒体id |
title | 否 | 图文消息/视频消息/音乐消息的标题 |
description | 否 | 图文消息/视频消息/音乐消息的描述 |
musicurl | 是 | 音乐链接 |
hqmusicurl | 是 | 高品质音乐链接,wifi环境优先使用该链接播放音乐 |
url | 否 | 图文消息被点击后跳转的链接 |
picurl | 否 | 图文消息的图片链接,支持jpg、png格式,较好的效果为大图640*320,小图80*80 |
4 java接口开发
(1)message客服接口消息封装对象
public class message { private string touser; private string msgtype; private textcontent text; private mediacontent image; private mediacontent voice; private mediacontent video; private musiccontent music; private articles news; public string gettouser() { return touser; } public void settouser(string touser) { this.touser = touser; } public string getmsgtype() { return msgtype; } public void setmsgtype(string msgtype) { this.msgtype = msgtype; } public textcontent gettext() { return text; } public void settext(textcontent text) { this.text = text; } public mediacontent getimage() { return image; } public void setimage(mediacontent image) { this.image = image; } public mediacontent getvoice() { return voice; } public void setvoice(mediacontent voice) { this.voice = voice; } public mediacontent getvideo() { return video; } public void setvideo(mediacontent video) { this.video = video; } public musiccontent getmusic() { return music; } public void setmusic(musiccontent music) { this.music = music; } public articles getnews() { return news; } public void setnews(articles news) { this.news = news; } @override public string tostring() { return "message [touser=" + touser + ", msgtype=" + msgtype + ", text=" + text + ", image=" + image + ", voice=" + voice + ", video=" + video + ", music=" + music + ", news=" + news + "]"; } }
(2)textcontent文本消息内容封装对象
public class textcontent { private string content; public string getcontent() { return content; } public void setcontent(string content) { this.content = content; } }
(3)mediacontent媒体id封装对象
public class mediacontent { private string media_id; public string getmedia_id() { return media_id; } public void setmedia_id(string media_id) { this.media_id = media_id; } }
(4)musiccontent音乐消息封装对象
public class musiccontent { private string title; private string description; private string musicurl; private string hqmusicurl; private string thumb_media_id; public string gettitle() { return title; } public void settitle(string title) { this.title = title; } public string getdescription() { return description; } public void setdescription(string description) { this.description = description; } public string getmusicurl() { return musicurl; } public void setmusicurl(string musicurl) { this.musicurl = musicurl; } public string gethqmusicurl() { return hqmusicurl; } public void sethqmusicurl(string hqmusicurl) { this.hqmusicurl = hqmusicurl; } public string getthumb_media_id() { return thumb_media_id; } public void setthumb_media_id(string thumb_media_id) { this.thumb_media_id = thumb_media_id; } }
(5)articles图文集合封装对象
public class articles { private article[] articles; public article[] getarticles() { return articles; } public void setarticles(article[] articles) { this.articles = articles; } }
(6)article图文消息封装对象
public class article { private string title; private string description; private string url; private string picurl; private string thumb_media_id; private string author; private string content_source_url; private string content; private string digest; private integer show_cover_pic; public string gettitle() { return title; } public void settitle(string title) { this.title = title; } public string getdescription() { return description; } public void setdescription(string description) { this.description = description; } public string geturl() { return url; } public void seturl(string url) { this.url = url; } public string getpicurl() { return picurl; } public void setpicurl(string picurl) { this.picurl = picurl; } public string getthumb_media_id() { return thumb_media_id; } public void setthumb_media_id(string thumb_media_id) { this.thumb_media_id = thumb_media_id; } public string getauthor() { return author; } public void setauthor(string author) { this.author = author; } public string getcontent_source_url() { return content_source_url; } public void setcontent_source_url(string content_source_url) { this.content_source_url = content_source_url; } public string getcontent() { return content; } public void setcontent(string content) { this.content = content; } public string getdigest() { return digest; } public void setdigest(string digest) { this.digest = digest; } public integer getshow_cover_pic() { return show_cover_pic; } public void setshow_cover_pic(integer show_cover_pic) { this.show_cover_pic = show_cover_pic; } }
(7)客服消息请求接口
public class crmsendmessageservice{ private resttemplate resttemplate ; private string servicehost = "https://api.weixin.qq.com"; public crmsendmessageserviceimpl() { resttemplate = resttemplatefactory.makeresttemplate(); } @override public weixinresponse sendmessage(string accesstoken, message message) { weixinresponse weixinresponse = null; string url = new stringbuffer(servicehost).append("/cgi-bin/message/custom/send?access_token=") .append(accesstoken).tostring(); weixinresponse = resttemplate.postforobject(url, message, weixinresponse.class); return weixinresponse; } }
注:接口发送http请求基于spring resttemplate。
参考文章地址:
(8)weixinresponse客服消息接口返回对象
public class weixinresponse { private string msgid; private string code; private int errcode; private string errmsg; public string getmsgid() { return msgid; } public void setmsgid(string msgid) { this.msgid = msgid; } public int geterrcode() { return errcode; } public void seterrcode(int errcode) { this.errcode = errcode; } public string geterrmsg() { return errmsg; } public void seterrmsg(string errmsg) { this.errmsg = errmsg; } public string getcode() { return code; } public void setcode(string code) { this.code = code; } }
5 接口实例开发
/** * 发送客服消息 * @param openid 要发给的用户 * @param accesstoken 微信公众号token * @param weixinappid 微信公众号appid */ private void sendcustommessage(string openid,string accesstoken,string weixinappid){ try { resttemplate rest = new resttemplate(); string posturl = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" + accesstoken; //推送图文消息 message message = new message(); message.settouser(openid);//普通用户openid message.setmsgtype("news");//图文消息(点击跳转到外链)为news articles news = new articles(); article article = new article(); article.setdescription("客服消息图文描述");//图文消息/视频消息/音乐消息的描述 article.setpicurl("http://mmbiz.qpic.cn/mmbiz_jpg/cdw6ticice130g6rcxcknndwic4deaahqdia2og5athbqsvspucfuqoyeelwenia4ciakt3khwq9t2lrpdpuo5akoyya/0");//图文消息的图片链接,支持jpg、png格式,较好的效果为大图640*320,小图80*80 article.settitle("客服消息图文标题");//图文消息/视频消息/音乐消息的标题 //图文推送链接 string url="https://www.baidu.com"; article.seturl(url);//图文消息被点击后跳转的链接 article[] articles = {article}; news.setarticles(articles); message.setnews(news); int i=1; while(i<=3){//循环发送3次 weixinresponse response = rest.postforobject(posturl, message, weixinresponse.class, new hashmap<string,string>()); log.info("发送客服消息返回信息:"+response.tostring()); if(response.geterrcode()==0){//发送成功-退出循环发送 i=4; break; }else{ i++;//发送失败-继续循环发送 } } } catch (exception e) { log.error("发送客服消息失败,openid="+openid,e); } }
6 客服接口图文推送上传图片
在发送图文消息时,我们需要添加图片的地址,介绍一个好方法。
(1)进入微信公众平台接口调试工具
(2)选择类型和列表
接口类型:基础支持
接口列表:上传logo接口/media/uploadimg
添加access_token,选择类型是image,最后选择文件
注:添加视频、音乐是一样的
(3)最后就会生成图片的url
(4)在浏览器访问url即可看见生成的图片
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: .Net 垃圾回收机制详细介绍
下一篇: 电子杂志的创编制作过程实例详解