微信開發之客服訊息的使用
阿新 • • 發佈:2018-12-24
之前的訊息傳送與接收都是使用者給微信發一條,之後微信回覆給使用者,也就是API上面所說的被動回覆訊息,那麼微信是否支援主動給關注使用者發訊息嗎,答案是肯定的,但是卻有一些限制。看下微信開發文件上怎麼說就明白了:
當用戶主動發訊息給公眾號的時候(包括髮送資訊、點選自定義選單、訂閱事件、掃描二維碼事件、支付成功事件、使用者維權),微信將會把訊息資料推送給開發者,開發者在一段時間內(目前修改為48小時)可以呼叫客服訊息介面,通過POST一個JSON資料包來發送訊息給普通使用者,在48小時內不限制傳送次數。此介面主要用於客服等有人工訊息處理環節的功能,方便開發者為使用者提供更加優質的服務。
這次就針對客服訊息分享下我的心得和體會
1 客服介面-發訊息介面使用說明
介面呼叫請求說明
各訊息型別所需的JSON資料包如下:
傳送文字訊息
{ "touser":"OPENID", "msgtype":"text", "text": { "content":"Hello World" } }
傳送圖片訊息
{ "touser":"OPENID", "msgtype":"image", "image": { "media_id":"MEDIA_ID" } }
傳送語音訊息
{ "touser":"OPENID", "msgtype":"voice", "voice": { "media_id":"MEDIA_ID" } }
......,基本上來說常用的訊息型別,客服介面都是支援的
2 封裝客服訊息介面
public static String makeTextCustomMessage(String openId,String content){ content.replace("\"", "\\\""); String jsonMsg="{\"touser\":\"%s\",\"msgtype\":\"text\",\"text\":{\"content\":\"%s\"}}"; return String.format(jsonMsg, openId,content); } /** * 組裝圖片客服訊息 * * @param openId 訊息傳送物件 * @param mediaId 媒體檔案id * @return */ public static String makeImageCustomMessage(String openId, String mediaId) { String jsonMsg = "{\"touser\":\"%s\",\"msgtype\":\"image\",\"image\":{\"media_id\":\"%s\"}}"; return String.format(jsonMsg, openId, mediaId); } /** * 組裝語音客服訊息 * * @param openId 訊息傳送物件 * @param mediaId 媒體檔案id * @return */ public static String makeVoiceCustomMessage(String openId, String mediaId) { String jsonMsg = "{\"touser\":\"%s\",\"msgtype\":\"voice\",\"voice\":{\"media_id\":\"%s\"}}"; return String.format(jsonMsg, openId, mediaId); } /** * 組裝視訊客服訊息 * * @param openId 訊息傳送物件 * @param mediaId 媒體檔案id * @param thumbMediaId 視訊訊息縮圖的媒體id * @return */ public static String makeVideoCustomMessage(String openId, String mediaId, String thumbMediaId) { String jsonMsg = "{\"touser\":\"%s\",\"msgtype\":\"video\",\"video\":{\"media_id\":\"%s\",\"thumb_media_id\":\"%s\"}}"; return String.format(jsonMsg, openId, mediaId, thumbMediaId); } /** * 組裝音樂客服訊息 * * @param openId 訊息傳送物件 * @param music 音樂物件 * @return */ public static String makeMusicCustomMessage(String openId, Music music) { String jsonMsg = "{\"touser\":\"%s\",\"msgtype\":\"music\",\"music\":%s}"; jsonMsg = String.format(jsonMsg, openId, JSONObject.fromObject(music).toString()); // 引數名稱替換 @20140125 jsonMsg = jsonMsg.replace("musicUrl", "musicurl"); jsonMsg = jsonMsg.replace("HQMusicUrl", "hqmusicurl"); jsonMsg = jsonMsg.replace("thumbMediaId", "thumb_media_id"); return jsonMsg; } /** * 組裝圖文客服訊息 * * @param openId 訊息傳送物件 * @param articleList 圖文訊息列表 * @return */ public static String makeNewsCustomMessage(String openId, List<Article> articleList) { String jsonMsg = "{\"touser\":\"%s\",\"msgtype\":\"news\",\"news\":{\"articles\":%s}}"; jsonMsg = String.format(jsonMsg, openId, JSONArray.fromObject(articleList).toString().replaceAll("\"", "\\\"")); // 將jsonMsg中的picUrl替換為picurl jsonMsg = jsonMsg.replace("picUrl", "picurl"); return jsonMsg; } public static boolean sendCustomMessage(String token,String jsonMsg){ boolean flag=false; //String accessToken=getAccessToken("xxxx","xxxx").getToken(); String requestUrl="https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=ACCESS_TOKEN"; requestUrl=requestUrl.replace("ACCESS_TOKEN", token); JSONObject jsonResult=CommonUtil.httpsRequest(requestUrl, "POST", jsonMsg); if(jsonResult!=null){ int errorCode=jsonResult.getInt("errcode"); String errorMessage=jsonResult.getString("errmsg"); if(errorCode==0){ flag=true; }else{ System.out.println("客服訊息傳送失敗:"+errorCode+","+errorMessage); flag=false; } } return flag; }
3 呼叫客服訊息傳送介面給指定使用者傳送客服訊息
List<Article> all=new ArrayList<Article>();
Article a2=new Article();
a2.setDescription("最崇拜的明星Justin Timberlake");
a2.setPicUrl("http://img3.douban.com/view/photo/photo/public/p1408738004.jpg");
a2.setTitle("賈斯汀·汀布萊克(Justin Timberlake),1981年1月31日出生於美國田納西州孟菲斯市,美國男歌手、演員、音樂製作人、主持人,前男子演唱組合超級男孩成員。");
a2.setUrl("http://baike.haosou.com/doc/3382630-3560934.html?from=1358&sid=3560934&redirect=search");
all.add(a2);
String articleMsg=makeNewsCustomMessage("xxx", all);
boolean res=sendCustomMessage(token,articleMsg);
if(res){
System.out.println("客服訊息傳送成功");
}else{
System.out.println("客服訊息傳送失敗");
}
到這裡程式就寫好了,來看下執行效果:
4 多客服的使用
多客服功能主要用於處理需人工操作、耗時較長的場景,我們都知道如果微信伺服器在5秒鐘沒給使用者返回訊息則會出現“公眾號暫時無法提供服務”。這樣使用者體驗會很差,有這種情況不妨考慮下多客服。
個人建議是可以把多客服看成是一種比較特別的訊息型別,這樣的話只需修改一下MessageUtil就OK了,加入如下程式碼:
/**
* 客服訊息物件轉換成xml
*
* @return xml
*/
public static String customMessageToXml(CustomServiceMessage customServiceMessage) {
xstream.alias("xml", customServiceMessage.getClass());
return xstream.toXML(customServiceMessage);
}
並且加一種訊息型別:
/**
* 多客服訊息
*/
public static final String TRANSFER_CUSTOMER_SERVICE = "transfer_customer_service";
5 修改微信訊息處理核心工具類
// 文字訊息
if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_TEXT)) {
String content=requestMap.get("Content");
if(content.startsWith("人工客服")){
CustomServiceMessage cus=new CustomServiceMessage();
cus.setToUserName(fromUserName);
cus.setFromUserName(toUserName);
cus.setCreateTime(new Date().getTime());
cus.setMsgType(MessageUtil.TRANSFER_CUSTOMER_SERVICE);
TransInfo t=new TransInfo();
t.setKfAccount("你的客服賬號");
cus.setTransInfo(t);
respMessage=MessageUtil.customMessageToXml(cus);
}else{
respContent = "您傳送的是文字訊息!";
}
}
順帶貼上CustomServiceMessage和TransInfo的程式碼:
package com.debug.weixin.message.res;
import com.debug.weixin.pojo.TransInfo;
public class CustomServiceMessage extends BaseMessage {
private TransInfo TransInfo;
public TransInfo getTransInfo() {
return TransInfo;
}
public void setTransInfo(TransInfo transInfo) {
TransInfo = transInfo;
}
}
package com.debug.weixin.pojo;
public class TransInfo {
private String KfAccount;
public String getKfAccount() {
return KfAccount;
}
public void setKfAccount(String kfAccount) {
KfAccount = kfAccount;
}
}
上面的程式碼如需測試需使用微信關注“多客服助手”或下載安裝“windows多客服”,具體參照如下連結:
這裡在貼2個我的執行截圖:
下面是windows的客戶端,就類似QQ的介面
差不多就這樣子了