微信公眾平臺技術(shù)文檔:客服消息
專注于為中小企業(yè)提供成都網(wǎng)站設(shè)計、成都網(wǎng)站制作服務(wù),電腦端+手機端+微信端的三站合一,更高效的管理,為中小企業(yè)魏都免費做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了近1000家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實現(xiàn)規(guī)模擴充和轉(zhuǎn)變。
一、接口說明
當(dāng)用戶和公眾號產(chǎn)生特定動作的交互時(具體動作列表請見下方說明),微信將會把消息數(shù)據(jù)推送給開發(fā)者,開發(fā)者可以在一段時間內(nèi)(目前修改為48小時)調(diào)用客服接口,通過POST一個JSON數(shù)據(jù)包來發(fā)送消息給普通用戶。此接口主要用于客服等有人工消息處理環(huán)節(jié)的功能,方便開發(fā)者為用戶提供更加優(yōu)質(zhì)的服務(wù)。
目前允許的動作列表如下(公眾平臺會根據(jù)運營情況更新該列表,不同動作觸發(fā)后,允許的客服接口下發(fā)消息條數(shù)不同,下發(fā)條數(shù)達(dá)到上限后,會遇到錯誤返回碼,具體請見返回碼說明頁):
1、用戶發(fā)送信息
2、點擊自定義菜單(僅有點擊推事件、掃碼推事件、掃碼推事件且彈出“消息接收中”提示框這3種菜單類型是會觸發(fā)客服接口的)
3、關(guān)注公眾號
4、掃描二維碼
5、支付成功
6、用戶維權(quán)
二、客服接口-發(fā)消息
1 接口調(diào)用請求說明
http請求方式: POST
https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=ACCESS_TOKEN
2 發(fā)送客服消息
各消息類型所需的JSON數(shù)據(jù)包如下:
(1)發(fā)送文本消息
{ "touser":"OPENID", "msgtype":"text", "text": { "content":"Hello World" } }
(2)發(fā)送圖片消息
{ "touser":"OPENID", "msgtype":"image", "image": { "media_id":"MEDIA_ID" } }
(3)發(fā)送語音消息
{ "touser":"OPENID", "msgtype":"voice", "voice": { "media_id":"MEDIA_ID" } }
(4)發(fā)送視頻消息
{ "touser":"OPENID", "msgtype":"video", "video": { "media_id":"MEDIA_ID", "thumb_media_id":"MEDIA_ID", "title":"TITLE", "description":"DESCRIPTION" } }
(5)發(fā)送音樂消息
{ "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)發(fā)送圖文消息(點擊跳轉(zhuǎn)到外鏈) 圖文消息條數(shù)限制在8條以內(nèi),注意,如果圖文數(shù)超過8,則將會無響應(yīng)。
{ "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)發(fā)送圖文消息(點擊跳轉(zhuǎn)到圖文消息頁面) 圖文消息條數(shù)限制在8條以內(nèi),注意,如果圖文數(shù)超過8,則將會無響應(yīng)。
{ "touser":"OPENID", "msgtype":"mpnews", "mpnews": { "media_id":"MEDIA_ID" } }
(8)發(fā)送卡券
{ "touser":"OPENID", "msgtype":"wxcard", "wxcard":{ "card_id":"123dsdajkasd231jhksad" }, }
特別注意客服消息接口投放卡券僅支持非自定義Code碼和導(dǎo)入code模式的卡券的卡券,詳情請見:是否自定義code碼。
請注意,如果需要以某個客服帳號來發(fā)消息(在微信6.0.2及以上版本中顯示自定義頭像),則需在JSON數(shù)據(jù)包的后半部分加入customservice參數(shù),例如發(fā)送文本消息則改為:
{ "touser":"OPENID", "msgtype":"text", "text": { "content":"Hello World" }, "customservice": { "kf_account": "test1@kftest" } }
3 請求參數(shù)說明
參數(shù) | 是否必須 | 說明 |
---|---|---|
access_token | 是 | 調(diào)用接口憑證 |
touser | 是 | 普通用戶openid |
msgtype | 是 | 消息類型,文本為text,圖片為image,語音為voice,視頻消息為video,音樂消息為music,圖文消息(點擊跳轉(zhuǎn)到外鏈)為news,圖文消息(點擊跳轉(zhuǎn)到圖文消息頁面)為mpnews,卡券為wxcard |
content | 是 | 文本消息內(nèi)容 |
media_id | 是 | 發(fā)送的圖片/語音/視頻/圖文消息(點擊跳轉(zhuǎn)到圖文消息頁)的媒體ID |
thumb_media_id | 是 | 縮略圖的媒體ID |
title | 否 | 圖文消息/視頻消息/音樂消息的標(biāo)題 |
description | 否 | 圖文消息/視頻消息/音樂消息的描述 |
musicurl | 是 | 音樂鏈接 |
hqmusicurl | 是 | 高品質(zhì)音樂鏈接,wifi環(huán)境優(yōu)先使用該鏈接播放音樂 |
url | 否 | 圖文消息被點擊后跳轉(zhuǎn)的鏈接 |
picurl | 否 | 圖文消息的圖片鏈接,支持JPG、PNG格式,較好的效果為大圖640*320,小圖80*80 |
4 java接口開發(fā)
(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文本消息內(nèi)容封裝對象
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; } }
注:接口發(fā)送http請求基于Spring RestTemplate。
參考文章地址:
1.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 接口實例開發(fā)
/** * 發(fā)送客服消息 * @param openId 要發(fā)給的用戶 * @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");//圖文消息(點擊跳轉(zhuǎn)到外鏈)為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("客服消息圖文標(biāo)題");//圖文消息/視頻消息/音樂消息的標(biāo)題 //圖文推送鏈接 String url="https://www.baidu.com"; article.setUrl(url);//圖文消息被點擊后跳轉(zhuǎn)的鏈接 Article[] articles = {article}; news.setArticles(articles); message.setNews(news); int i=1; while(i<=3){//循環(huán)發(fā)送3次 WeixinResponse response = rest.postForObject(postUrl, message, WeixinResponse.class, new HashMap()); LOG.info("發(fā)送客服消息返回信息:"+response.toString()); if(response.getErrcode()==0){//發(fā)送成功-退出循環(huán)發(fā)送 i=4; break; }else{ i++;//發(fā)送失敗-繼續(xù)循環(huán)發(fā)送 } } } catch (Exception e) { LOG.error("發(fā)送客服消息失敗,openId="+openId,e); } }
6 客服接口圖文推送上傳圖片
在發(fā)送圖文消息時,我們需要添加圖片的地址,介紹一個好方法。
(1)進(jìn)入微信公眾平臺接口調(diào)試工具
https://mp.weixin.qq.com/debug
(2)選擇類型和列表
接口類型:基礎(chǔ)支持
接口列表:上傳logo接口/media/uploadimg
添加access_token,選擇類型是image,最后選擇文件
注:添加視頻、音樂是一樣的
(3)最后就會生成圖片的url
(4)在瀏覽器訪問url即可看見生成的圖片
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。