weixin-java-mp集成微信公众号自带客服功能

时间:2023-06-15 11:34:14

电脑端登录公众号管理后台,【添加功能插件】开通客服功能,输入"人工客服"接入客服热线

底部有我的微信二维码,如有问题,可加好友进行技术交流!

weixin-java-mp集成微信公众号自带客服功能weixin-java-mp集成微信公众号自带客服功能

weixin-java-mp集成微信公众号自带客服功能weixin-java-mp集成微信公众号自带客服功能

weixin-java-mp集成微信公众号自带客服功能weixin-java-mp集成微信公众号自带客服功能

weixin-java-mp集成微信公众号自带客服功能weixin-java-mp集成微信公众号自带客服功能

weixin-java-mp集成微信公众号自带客服功能weixin-java-mp集成微信公众号自带客服功能

weixin-java-mp集成微信公众号自带客服功能weixin-java-mp集成微信公众号自带客服功能

weixin-java-mp集成微信公众号自带客服功能weixin-java-mp集成微信公众号自带客服功能

weixin-java-mp集成微信公众号自带客服功能代码

增加TextBuilder.java文件 内容如下:

public class TextBuilder extends AbstractBuilder {

  @Override
 public WxMpXmlOutMessage build(String content, WxMpXmlMessage wxMessage,
     WeixinService service) {
WxMpXmlOutTextMessage m = WxMpXmlOutMessage.TEXT().content(content)
.fromUser(wxMessage.getToUser()).toUser(wxMessage.getFromUser())
.build();
   return m;
 }
public WxMpXmlOutMessage buildKf(String content, WxMpXmlMessage wxMessage,WeixinService service) {
WxMpXmlOutTextMessage m = WxMpXmlOutMessage.TEXT().content(content)
.fromUser(wxMessage.getToUser()).toUser(wxMessage.getFromUser())
.build();
   m.setToUserName(wxMessage.getFromUser());
   m.setFromUserName(wxMessage.getToUser());
   m.setCreateTime(System.currentTimeMillis() / 1000l);
   m.setMsgType("transfer_customer_service");
   return m;
 } }

weixin-java-mp集成微信公众号自带客服功能

增加MsgHandler.java

@Component
public class MsgHandler extends AbstractHandler { @Resource
   private WeixinService weixinService;    @Override
   public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, Map<String, Object> context, WxMpService wxMpService, WxSessionManager sessionManager) throws WxErrorException {
WeixinService weixinService = (WeixinService) wxMpService;
       if(wxMessage.getMsgType().contains("text")) { //收到的是文本消息
           //回复转人工服务
           if("人工客服".equals(wxMessage.getContent())){
return new TextBuilder().buildKf("感谢关注", wxMessage, weixinService);
           }
}
}

weixin-java-mp集成微信公众号自带客服功能

增加WeixinService.java文件,内容如下

@Service
public class WeixinService extends WxMpServiceImpl {
private final Logger logger = LoggerFactory.getLogger(this.getClass());  @Autowired
 protected LogHandler logHandler;  @Autowired
 protected NullHandler nullHandler;  @Autowired
 protected KfSessionHandler kfSessionHandler;  @Autowired
 protected StoreCheckNotifyHandler storeCheckNotifyHandler;  @Autowired
 private WxMpConfig wxConfig;  @Autowired
 private LocationHandler locationHandler;  @Autowired
 private MenuHandler menuHandler;  @Autowired
 private MsgHandler msgHandler;  @Autowired
 private UnsubscribeHandler unsubscribeHandler;  @Autowired
 private SubscribeHandler subscribeHandler;
 @Autowired
 private ScanHandler scanHandler;  private WxMpMessageRouter router;  @PostConstruct
 public void init() {
final WxMpInMemoryConfigStorage config = new WxMpInMemoryConfigStorage();
   config.setAppId(this.wxConfig.getAppid());// 设置微信公众号的appid
   config.setSecret(this.wxConfig.getAppsecret());// 设置微信公众号的app corpSecret
   config.setToken(this.wxConfig.getToken());// 设置微信公众号的token
   config.setAesKey(this.wxConfig.getAesKey());// 设置消息加解密密钥
   super.setWxMpConfigStorage(config);    this.refreshRouter();
 } private void refreshRouter() {
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.EVT_POI_CHECK_NOTIFY)
.handler(this.storeCheckNotifyHandler)
.end();    // 自定义菜单事件
   newRouter.rule().async(false).msgType(WxConsts.XML_MSG_EVENT)
.event(WxConsts.BUTTON_CLICK).handler(this.getMenuHandler()).end();    // 点击菜单连接事件
   newRouter.rule().async(false).msgType(WxConsts.XML_MSG_EVENT)
.event(WxConsts.BUTTON_VIEW).handler(this.nullHandler).end();    // 关注事件
   newRouter.rule().async(false).msgType(WxConsts.XML_MSG_EVENT)
.event(WxConsts.EVT_SUBSCRIBE).handler(this.getSubscribeHandler())
.end();    // 取消关注事件
   newRouter.rule().async(false).msgType(WxConsts.XML_MSG_EVENT)
.event(WxConsts.EVT_UNSUBSCRIBE).handler(this.getUnsubscribeHandler())
.end();    // 上报地理位置事件
   newRouter.rule().async(false).msgType(WxConsts.XML_MSG_EVENT)
.event(WxConsts.EVT_LOCATION).handler(this.getLocationHandler()).end();    // 接收地理位置消息
   newRouter.rule().async(false).msgType(WxConsts.XML_MSG_LOCATION)
.handler(this.getLocationHandler()).end();    // 扫码事件
   newRouter.rule().async(false).msgType(WxConsts.XML_MSG_EVENT)
.event(WxConsts.EVT_SCAN).handler(this.getScanHandler()).end();    // 默认
   newRouter.rule().async(false).handler(this.getMsgHandler()).end();    this.router = newRouter;
 } public WxMpXmlOutMessage route(WxMpXmlMessage message) {
try {
return this.router.route(message);
   } catch (Exception e) {
this.logger.error(e.getMessage(), e);
   } return null;
 } public boolean hasKefuOnline() {
try {
WxMpKfOnlineList kfOnlineList = this.getKefuService().kfOnlineList();
     return kfOnlineList != null && kfOnlineList.getKfOnlineList().size() > 0;
   } catch (Exception e) {
this.logger.error("获取客服在线状态异常: " + e.getMessage(), e);
   } return false;
 } protected MenuHandler getMenuHandler() {
return this.menuHandler;
 } protected SubscribeHandler getSubscribeHandler() {
return this.subscribeHandler;
 } protected UnsubscribeHandler getUnsubscribeHandler() {
return this.unsubscribeHandler;
 } protected AbstractHandler getLocationHandler() {
return this.locationHandler;
 } protected MsgHandler getMsgHandler() {
return this.msgHandler;
 } protected AbstractHandler getScanHandler() {
return scanHandler;
 } /**
  * 为生成二维码准备ticket
  * @return
  */
 public WxMpQrCodeTicket generateTicketForTwoDimensionCode(Long customerId) throws WxErrorException {
//永久二维码
   return this.getQrcodeService().qrCodeCreateLastTicket(customerId.intValue());
   //临时二维码
   //return this.getQrcodeService().qrCodeCreateTmpTicket(customerId.intValue(), 2592000);
 }
}

weixin-java-mp集成微信公众号自带客服功能