From 06a3ed17d0469161c7cd476919e8429a78fca6cf Mon Sep 17 00:00:00 2001 From: BinaryWang Date: Fri, 8 Jul 2016 12:33:50 +0800 Subject: [PATCH] =?UTF-8?q?demo=E4=B8=AD=E5=A2=9E=E5=8A=A0=E5=AF=B9?= =?UTF-8?q?=E5=AE=A2=E6=9C=8D=E4=BC=9A=E8=AF=9D=E7=AE=A1=E7=90=86=E7=9A=84?= =?UTF-8?q?=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../demo/spring/handler/KfSessionHandler.java | 36 +++++++++++++++++++ .../demo/spring/handler/LogHandler.java | 2 +- .../demo/spring/handler/MsgHandler.java | 4 +-- .../demo/spring/service/BaseWxService.java | 21 +++++++++-- 4 files changed, 57 insertions(+), 6 deletions(-) create mode 100644 weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/handler/KfSessionHandler.java diff --git a/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/handler/KfSessionHandler.java b/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/handler/KfSessionHandler.java new file mode 100644 index 00000000..dac1be80 --- /dev/null +++ b/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/handler/KfSessionHandler.java @@ -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 context, WxMpService wxMpService, + WxSessionManager sessionManager) throws WxErrorException { + //TODO 对会话做处理 + return null; + } + + @Override + protected WxConfig getWxConfig() { + return null; + } + +} diff --git a/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/handler/LogHandler.java b/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/handler/LogHandler.java index 28aa264b..32890f73 100644 --- a/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/handler/LogHandler.java +++ b/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/handler/LogHandler.java @@ -26,7 +26,7 @@ public class LogHandler extends AbstractHandler { public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, Map context, WxMpService wxMpService, WxSessionManager sessionManager) { - this.logger.info("接收到请求消息,内容:【{}】" ,wxMessage.toString()); + this.logger.info("接收到请求消息,内容:【{}】", wxMessage.toString()); return null; } diff --git a/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/handler/MsgHandler.java b/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/handler/MsgHandler.java index 5984749c..7519420d 100644 --- a/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/handler/MsgHandler.java +++ b/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/handler/MsgHandler.java @@ -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()) diff --git a/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/service/BaseWxService.java b/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/service/BaseWxService.java index 29b319a9..a7d851d8 100644 --- a/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/service/BaseWxService.java +++ b/weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/service/BaseWxService.java @@ -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);