demo中增加对客服会话管理的支持

This commit is contained in:
BinaryWang 2016-07-08 12:33:50 +08:00
parent 3ab7e66b48
commit 06a3ed17d0
4 changed files with 57 additions and 6 deletions

View File

@ -0,0 +1,36 @@
package com.github.binarywang.demo.spring.handler;
import java.util.Map;
import org.springframework.stereotype.Component;
import com.github.binarywang.demo.spring.config.WxConfig;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.WxMpXmlOutMessage;
/**
*
* @author Binary Wang
*
*/
@Component
public class KfSessionHandler extends AbstractHandler{
@Override
public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage,
Map<String, Object> context, WxMpService wxMpService,
WxSessionManager sessionManager) throws WxErrorException {
//TODO 对会话做处理
return null;
}
@Override
protected WxConfig getWxConfig() {
return null;
}
}

View File

@ -26,7 +26,7 @@ public class LogHandler extends AbstractHandler {
public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage,
Map<String, Object> context, WxMpService wxMpService,
WxSessionManager sessionManager) {
this.logger.info("接收到请求消息,内容:【{}】" ,wxMessage.toString());
this.logger.info("接收到请求消息,内容:【{}】", wxMessage.toString());
return null;
}

View File

@ -31,8 +31,8 @@ public abstract class MsgHandler extends AbstractHandler {
//TODO 可以选择将消息保存到本地
}
//当用户输入关键词如客服并且有客服在线时把消息转发给在线客服
if (StringUtils.startsWithAny(wxMessage.getContent(), "","客服")
//当用户输入关键词如你好客服并且有客服在线时把消息转发给在线客服
if (StringUtils.startsWithAny(wxMessage.getContent(), "你好", "客服")
&& weixinService.hasKefuOnline()) {
return WxMpXmlOutMessage
.TRANSFER_CUSTOMER_SERVICE().fromUser(wxMessage.getToUserName())

View File

@ -8,10 +8,11 @@ import org.springframework.beans.factory.annotation.Autowired;
import com.github.binarywang.demo.spring.config.WxConfig;
import com.github.binarywang.demo.spring.handler.AbstractHandler;
import com.github.binarywang.demo.spring.handler.KfSessionHandler;
import com.github.binarywang.demo.spring.handler.LogHandler;
import com.github.binarywang.demo.spring.handler.MenuHandler;
import com.github.binarywang.demo.spring.handler.MsgHandler;
import com.github.binarywang.demo.spring.handler.NullHandler;
import com.github.binarywang.demo.spring.handler.LogHandler;
import com.github.binarywang.demo.spring.handler.SubscribeHandler;
import com.github.binarywang.demo.spring.handler.UnsubscribeHandler;
@ -37,6 +38,9 @@ public abstract class BaseWxService extends WxMpServiceImpl {
@Autowired
protected NullHandler nullHandler;
@Autowired
protected KfSessionHandler kfSessionHandler;
private WxMpMessageRouter router;
protected abstract WxConfig getServerConfig();
@ -50,7 +54,7 @@ public abstract class BaseWxService extends WxMpServiceImpl {
protected abstract AbstractHandler getLocationHandler();
protected abstract MsgHandler getMsgHandler();
protected abstract AbstractHandler getScanHandler();
@PostConstruct
@ -68,9 +72,19 @@ public abstract class BaseWxService extends WxMpServiceImpl {
final WxMpMessageRouter newRouter = new WxMpMessageRouter(this);
//记录所有事件的日志
// 记录所有事件的日志
newRouter.rule().handler(this.logHandler).next();
// 接收客服会话管理事件
newRouter.rule().async(false).msgType(WxConsts.XML_MSG_EVENT)
.event(WxConsts.EVT_KF_CREATE_SESSION).handler(this.kfSessionHandler).end();
newRouter.rule().async(false).msgType(WxConsts.XML_MSG_EVENT)
.event(WxConsts.EVT_KF_CLOSE_SESSION).handler(this.kfSessionHandler).end();
newRouter.rule().async(false).msgType(WxConsts.XML_MSG_EVENT)
.event(WxConsts.EVT_KF_SWITCH_SESSION).handler(this.kfSessionHandler).end();
// 自定义菜单事件
newRouter.rule().async(false).msgType(WxConsts.XML_MSG_EVENT)
.event(WxConsts.BUTTON_CLICK).handler(this.getMenuHandler()).end();
@ -107,6 +121,7 @@ public abstract class BaseWxService extends WxMpServiceImpl {
this.router = newRouter;
}
public WxMpXmlOutMessage route(WxMpXmlMessage message) {
try {
final WxMpXmlOutMessage responseMessage = this.router.route(message);