電腦端登錄公眾號管理後臺,【添加功能插件】開通客服功能,輸入"人工客服"接入客服熱線 底部有我的微信二維碼,如有問題,可加好友進行技術交流! weixin-java-mp集成微信公眾號自帶客服功能代碼 增加TextBuilder.java文件 內容如下: public c ...
電腦端登錄公眾號管理後臺,【添加功能插件】開通客服功能,輸入"人工客服"接入客服熱線
底部有我的微信二維碼,如有問題,可加好友進行技術交流!
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;
}
}
增加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);
}
}
}
增加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);
}
}