Browse Source

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

master
BinaryWang 8 years ago
parent
commit
06a3ed17d0
4 changed files with 57 additions and 6 deletions
  1. +36
    -0
      weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/handler/KfSessionHandler.java
  2. +1
    -1
      weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/handler/LogHandler.java
  3. +2
    -2
      weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/handler/MsgHandler.java
  4. +18
    -3
      weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/service/BaseWxService.java

+ 36
- 0
weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/handler/KfSessionHandler.java 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;
}

}

+ 1
- 1
weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/handler/LogHandler.java 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;
}



+ 2
- 2
weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/handler/MsgHandler.java 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())


+ 18
- 3
weixin-java-demo-with-spring/src/main/java/com/github/binarywang/demo/spring/service/BaseWxService.java 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);


Loading…
Cancel
Save