微信客服消息机制
如果公众号处于开发模式,普通微信用户向公众号发消息时,微信服务器会先将消息POST到开发者填写的url上,如果希望将消息转发到客服系统,则需要开发者在响应包中返回MsgType为transfer_customer_service的消息,微信服务器收到响应后会把当次发送的消息再转发至客服系统。
因此处于开发模式中,普通微信用户向公众号发送的任何消息,都会经过后端服务器(客服和普通用户创建会话除外)。
微信官方提供的API有以下:
- 消息转发至微信客服系统
- 消息转发至指定客服
- 创建客服与微信用户的会话
- 关闭客服与微信用户的会话
- 获取客服聊天记录(目前只支持文本消息)
- 获取客服会话列表
- 获取未接入会话列表
- 客服接口发送消息
当创建客服会话和关闭客服会话时,微信会给后端系统事件通知
创建会话事件通知
<xml>
<ToUserName><![CDATA[gh_47bd9550a0a1]]></ToUserName>
<FromUserName><![CDATA[oFTYt0l9wQDDhwmx1js_d1T6zZkw]]></FromUserName>
<CreateTime>1530688486</CreateTime>
<MsgType><![CDATA[event]]></MsgType>
<Event><![CDATA[kf_create_session]]></Event>
<KfAccount><![CDATA[kf2004@testttmwsh]]></KfAccount>
</xml>
关闭会话事件通知
<xml>
<ToUserName><![CDATA[gh_47bd9550a0a1]]></ToUserName>
<FromUserName><![CDATA[oFTYt0l9wQDDhwmx1js_d1T6zZkw]]></FromUserName>
<CreateTime>1530688335</CreateTime>
<MsgType><![CDATA[event]]></MsgType>
<Event><![CDATA[kf_close_session]]></Event>
<KfAccount><![CDATA[kf2004@testttmwsh]]></KfAccount>
<CloseType><![CDATA[KF]]></CloseType>
</xml>
本文主要简单介绍客服消息的发送、客服会话创建、客服会话关闭三个接口。
客服消息的发送
接口调用请求说明
http请求方式: POST
https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=ACCESS_TOKEN
发送文本消息
{
"touser":"OPENID",
"msgtype":"text",
"text":
{
"content":"Hello World"
}
}
实现流程
构建CustomerTextData类封装文本数据
public class CustomerTextData implements Serializable{
private String touser;
private String msgtype = "text";
private Map<String,Object> text;
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 Map<String, Object> getText() {
return text;
}
public void setText(Map<String, Object> text) {
this.text = text;
}
}
构建CustomerMessageService服务来发送客服消息
public class CustomerMessageService extends BaseService {
public static String CUSTOMER_SERVICE_MESSAGE_URL = "https://api.weixin.qq.com/cgi-bin/message/custom/send";
public void pushTextMessage(String msg, String openId) {
try {
//设置客服消息发送接口的URL
setApiURL(CUSTOMER_SERVICE_MESSAGE_URL);
//构建客服消息数据
List<NameValuePair> nameValuePairs = new ArrayList<>();
nameValuePairs.add(new BasicNameValuePair("access_token", this.getAccessToken()));
CustomerTextData data = new CustomerTextData();
data.setTouser(openId);
Map<String, Object> text = Maps.newHashMap();
text.put("content", msg);
data.setText(text);
String result = sendPost(nameValuePairs, JSON.toJSONString(data));
//打印客服消息发送日志
logger.info("push textMessage result :{}", result);
} catch (Exception e) {
logger.error("textMessage error msg:{},openId:{}", msg, openId, e);
}
}
}
客服会话创建
此接口在客服和用户之间创建一个会话,如果该客服和用户会话已存在,则直接返回0。指定的客服帐号必须已经绑定微信号且在线。
接口调用请求说明
http请求方式: POST
https://api.weixin.qq.com/customservice/kfsession/create?access_token=ACCESS_TOKEN
POST数据示例如下:
{
"kf_account" : "test1@test",
"openid" : "OPENID"
}
同样在CustomerMessageService服务中来实现客服会话的创建
public class CustomerMessageService extends BaseService {
public static String CUSTOMER_SERVICE_CREATE_CHAT_URL = "https://api.weixin.qq.com/customservice/kfsession/create";
public void creatSession(String openId, String kfAccount) {
try {
//设置创建客服会话接口的URL
setApiURL(CUSTOMER_SERVICE_CREATE_CHAT_URL);
//构建创建客服会话数据
List<NameValuePair> nameValuePairs = new ArrayList<>();
nameValuePairs.add(new BasicNameValuePair("access_token", this.getAccessToken()));
Map<String, Object> creatData = Maps.newHashMap();
createData.put("openid", openId);
createData.put("kf_account",kfAccount);
String result = sendPost(nameValuePairs, JSON.toJSONString(createData));
//打印客服消息发送日志
logger.info("create session param : {} result :{} ",createData, result);
} catch (Exception e) {
logger.error("create session openId : {} kfAccount : {}", openId, kfAccount, e);
}
}
}
接口调用请求说明
http请求方式: POST
https: //api.weixin.qq.com/customservice/kfsession/close?access_token=ACCESS_TOKEN
POST数据示例如下:
{
"kf_account":"test1@test" ,
"openid": "OPENID"
}
同样在CustomerMessageService服务中来实现客服会话的关闭
public class CustomerMessageService extends BaseService {
public static String CUSTOMER_SERVICE_CLOSE_CHAT_URL = "https://api.weixin.qq.com/customservice/kfsession/close";
public void creatSession(String openId, String kfAccount) {
try {
//设置关闭客服会话接口的URL
setApiURL(CUSTOMER_SERVICE_CLOSE_CHAT_URL);
//构建关闭客服会话数据
List<NameValuePair> nameValuePairs = new ArrayList<>();
nameValuePairs.add(new BasicNameValuePair("access_token", this.getAccessToken()));
Map<String, Object> closeData = Maps.newHashMap();
closeData.put("openid", openId);
closeData.put("kf_account",kfAccount);
String result = sendPost(nameValuePairs, JSON.toJSONString(closeData));
//打印客服消息发送日志
logger.info("close session param : {} result :{} ",closeData, result);
} catch (Exception e) {
logger.error("close session openId : {} kfAccount : {}", openId, kfAccount, e);
}
}
}
微信官方文档:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1458557405