1、客服消息转发到服务器配置:
进入微信小程序管理页面—》左侧菜单“开发”–》开发管理—》开发设置–》消息推送;在消息推送里面配置自己的服务器url,也就是接收消息的url,这样所有的消息都会转发到这个服务器。具体配置流程自己百度吧,这里要讲的重点是如何在配置了消息推送后讲特定的消息转发给客服处理
2、可以明确的通过文字告诉用户,只要回复“0”就可以切换到某个在线客服。
所以用户只要回复0,接口就会接收到,判断是0,使用下面两种的方式返回;其他情况还是返回success就行
以下是代码:
如果消息推送里面配置的是xml:
public static String toCustomer(String touser, String fromuser) { StringBuilder relayCustomMsg = new StringBuilder(); relayCustomMsg.append("<xml>"); relayCustomMsg.append("<ToUserName><![CDATA[" + touser + "]]></ToUserName>"); relayCustomMsg.append("<FromUserName><![CDATA[" + fromuser + "]]></FromUserName>"); relayCustomMsg.append("<CreateTime>" + new Date().getTime() + "</CreateTime>"); relayCustomMsg.append("<MsgType><![CDATA[transfer_customer_service]]></MsgType>"); relayCustomMsg.append("<TransInfo><KfAccount><![CDATA[kf2002@****]]></KfAccount></TransInfo>"); relayCustomMsg.append("</xml>"); return relayCustomMsg.toString(); }
如果消息推送里面配置的是json:
public static String toCustomer(String touser, String fromuser) { Map<String, Object> map = new HashMap<>(); map.put("ToUserName", touser); map.put("FromUserName", fromuser); map.put("CreateTime", new Date().getTime()); map.put("MsgType", "transfer_customer_service"); Map<String, Object> kfAccountMap = new HashMap<>(); kfAccountMap.put("KfAccount", "kf2002@****"); map.put("TransInfo", kfAccountMap); return JSON.toJSONString(map); } ``` 有两个注意点: 1、 KfAccount的获取(客服)可以通过下面的方法: try { String httpClientGet = HttpUtils.HttpClientGet( "https://api.weixin.qq.com/cgi-bin/customservice/getkflist?access_token=" + acquireAccessToken + ""); log.info(httpClientGet); return httpClientGet; } catch (Exception e) { log.error("send msg1 error ", e); } 2、ToUserName是我们后端自己,也就是小程序的原始ID,FromUserName是发送者的openid