1
0
mirror of https://github.com/chatopera/cosin.git synced 2025-08-01 16:38:02 +08:00

#75 处理AiUser

This commit is contained in:
Hai Liang Wang 2018-09-11 23:32:13 +08:00
parent 1891891e4a
commit 7bc572ae2e
4 changed files with 272 additions and 238 deletions

View File

@ -67,6 +67,7 @@ public class OnlineUserUtils {
/** /**
* 更新cache * 更新cache
*
* @param id * @param id
* @param cousultInvite * @param cousultInvite
*/ */
@ -598,6 +599,7 @@ public class OnlineUserUtils {
*/ */
public static void offline(String user, String orgi) throws Exception { public static void offline(String user, String orgi) throws Exception {
if (UKDataContext.getContext() != null) { if (UKDataContext.getContext() != null) {
try {
OnlineUser onlineUser = (OnlineUser) CacheHelper.getOnlineUserCacheBean().getCacheObject(user, orgi); OnlineUser onlineUser = (OnlineUser) CacheHelper.getOnlineUserCacheBean().getCacheObject(user, orgi);
if (onlineUser != null) { if (onlineUser != null) {
CousultInvite invite = OnlineUserUtils.cousult(onlineUser.getAppid(), onlineUser.getOrgi(), UKDataContext.getContext().getBean(ConsultInviteRepository.class)); CousultInvite invite = OnlineUserUtils.cousult(onlineUser.getAppid(), onlineUser.getOrgi(), UKDataContext.getContext().getBean(ConsultInviteRepository.class));
@ -626,6 +628,11 @@ public class OnlineUserUtils {
} }
} }
} }
} catch (ClassCastException e) {
// #TODO workaround for
// https://github.com/chatopera/cosin/issues/75
// AiUser is not saved, just remove from cache.
}
CacheHelper.getOnlineUserCacheBean().delete(user, orgi); CacheHelper.getOnlineUserCacheBean().delete(user, orgi);
} }
} }

View File

@ -16,21 +16,19 @@
*/ */
package com.chatopera.cc.webim.util.server; package com.chatopera.cc.webim.util.server;
import java.lang.reflect.Constructor; import com.chatopera.cc.core.UKDataContext;
import java.lang.reflect.InvocationTargetException; import com.chatopera.cc.webim.util.server.handler.AgentEventHandler;
import com.chatopera.cc.webim.util.server.handler.ChatbotEventHandler;
import com.chatopera.cc.webim.util.server.handler.EntIMEventHandler; import com.chatopera.cc.webim.util.server.handler.EntIMEventHandler;
import com.chatopera.cc.webim.util.server.handler.IMEventHandler; import com.chatopera.cc.webim.util.server.handler.IMEventHandler;
import com.corundumstudio.socketio.SocketIONamespace;
import com.corundumstudio.socketio.SocketIOServer;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner; import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import com.corundumstudio.socketio.SocketIONamespace; import java.lang.reflect.Constructor;
import com.corundumstudio.socketio.SocketIOServer; import java.lang.reflect.InvocationTargetException;
import com.chatopera.cc.core.UKDataContext;
import com.chatopera.cc.webim.util.server.handler.AgentEventHandler;
@Component @Component
public class ServerRunner implements CommandLineRunner { public class ServerRunner implements CommandLineRunner {
@ -48,7 +46,6 @@ public class ServerRunner implements CommandLineRunner {
imSocketNameSpace = server.addNamespace(UKDataContext.NameSpaceEnum.IM.getNamespace()); imSocketNameSpace = server.addNamespace(UKDataContext.NameSpaceEnum.IM.getNamespace());
agentSocketIONameSpace = server.addNamespace(UKDataContext.NameSpaceEnum.AGENT.getNamespace()); agentSocketIONameSpace = server.addNamespace(UKDataContext.NameSpaceEnum.AGENT.getNamespace());
entIMSocketIONameSpace = server.addNamespace(UKDataContext.NameSpaceEnum.ENTIM.getNamespace()); entIMSocketIONameSpace = server.addNamespace(UKDataContext.NameSpaceEnum.ENTIM.getNamespace());
chatbotSocketIONameSpace = server.addNamespace(UKDataContext.NameSpaceEnum.CHATBOT.getNamespace()) ;
if (UKDataContext.model.get("sales") != null && UKDataContext.model.get("sales") == true) { if (UKDataContext.model.get("sales") != null && UKDataContext.model.get("sales") == true) {
calloutSocketIONameSpace = server.addNamespace(UKDataContext.NameSpaceEnum.CALLOUT.getNamespace()); calloutSocketIONameSpace = server.addNamespace(UKDataContext.NameSpaceEnum.CALLOUT.getNamespace());
@ -61,6 +58,12 @@ public class ServerRunner implements CommandLineRunner {
} else { } else {
callCenterSocketIONameSpace = null; callCenterSocketIONameSpace = null;
} }
if (UKDataContext.model.get("chatbot") != null && UKDataContext.model.get("chatbot") == true) {
chatbotSocketIONameSpace = server.addNamespace(UKDataContext.NameSpaceEnum.CHATBOT.getNamespace());
} else {
chatbotSocketIONameSpace = null;
}
} }
@Bean(name = "imNamespace") @Bean(name = "imNamespace")
@ -83,8 +86,18 @@ public class ServerRunner implements CommandLineRunner {
@Bean(name = "chatbotNamespace") @Bean(name = "chatbotNamespace")
public SocketIONamespace getChatbotSocketIONameSpace(SocketIOServer server) { public SocketIONamespace getChatbotSocketIONameSpace(SocketIOServer server) {
chatbotSocketIONameSpace.addListeners(new ChatbotEventHandler(server)); if (UKDataContext.model.get("chatbot") != null && UKDataContext.model.get("chatbot") == true) {
Constructor<?> constructor;
try {
constructor = Class.forName("com.chatopera.cc.webim.util.server.handler.ChatbotEventHandler").getConstructor(new Class[]{SocketIOServer.class});
chatbotSocketIONameSpace.addListeners(constructor.newInstance(server));
} catch (NoSuchMethodException | SecurityException
| ClassNotFoundException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
e.printStackTrace();
}
}
return chatbotSocketIONameSpace; return chatbotSocketIONameSpace;
} }
@Bean(name = "callCenterNamespace") @Bean(name = "callCenterNamespace")

View File

@ -44,19 +44,16 @@ import org.springframework.beans.factory.annotation.Autowired;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.util.Date; import java.util.Date;
public class ChatbotEventHandler public class ChatbotEventHandler {
{
protected SocketIOServer server; protected SocketIOServer server;
@Autowired @Autowired
public ChatbotEventHandler(SocketIOServer server) public ChatbotEventHandler(SocketIOServer server) {
{
this.server = server; this.server = server;
} }
@OnConnect @OnConnect
public void onConnect(SocketIOClient client) public void onConnect(SocketIOClient client) {
{
try { try {
String user = client.getHandshakeData().getSingleUrlParam("userid"); String user = client.getHandshakeData().getSingleUrlParam("userid");
String orgi = client.getHandshakeData().getSingleUrlParam("orgi"); String orgi = client.getHandshakeData().getSingleUrlParam("orgi");
@ -66,7 +63,7 @@ public class ChatbotEventHandler
// String agent = client.getHandshakeData().getSingleUrlParam("agent") ; // String agent = client.getHandshakeData().getSingleUrlParam("agent") ;
// String skill = client.getHandshakeData().getSingleUrlParam("skill") ; // String skill = client.getHandshakeData().getSingleUrlParam("skill") ;
if(!StringUtils.isBlank(user)){ if (StringUtils.isNotBlank(user)) {
// /** // /**
// * 加入到 缓存列表 // * 加入到 缓存列表
// */ // */
@ -76,7 +73,7 @@ public class ChatbotEventHandler
if (invite != null && !StringUtils.isBlank(invite.getAisuccesstip())) { if (invite != null && !StringUtils.isBlank(invite.getAisuccesstip())) {
outMessage.setMessage(invite.getAisuccesstip()); outMessage.setMessage(invite.getAisuccesstip());
} else { } else {
outMessage.setMessage("欢迎使用优客服小E我来帮您解答问题"); outMessage.setMessage("欢迎使用华夏春松机器人客服!");
} }
outMessage.setMessageType(UKDataContext.MessageTypeEnum.MESSAGE.toString()); outMessage.setMessageType(UKDataContext.MessageTypeEnum.MESSAGE.toString());
@ -109,8 +106,7 @@ public class ChatbotEventHandler
//添加@OnDisconnect事件客户端断开连接时调用刷新客户端信息 //添加@OnDisconnect事件客户端断开连接时调用刷新客户端信息
@OnDisconnect @OnDisconnect
public void onDisconnect(SocketIOClient client) throws Exception public void onDisconnect(SocketIOClient client) throws Exception {
{
String user = client.getHandshakeData().getSingleUrlParam("userid"); String user = client.getHandshakeData().getSingleUrlParam("userid");
String orgi = client.getHandshakeData().getSingleUrlParam("orgi"); String orgi = client.getHandshakeData().getSingleUrlParam("orgi");
if (!StringUtils.isBlank(user)) { if (!StringUtils.isBlank(user)) {
@ -126,22 +122,19 @@ public class ChatbotEventHandler
//消息接收入口网站有新用户接入对话 //消息接收入口网站有新用户接入对话
@OnEvent(value = "new") @OnEvent(value = "new")
public void onEvent(SocketIOClient client, AckRequest request, NewRequestMessage data) public void onEvent(SocketIOClient client, AckRequest request, NewRequestMessage data) {
{
} }
//消息接收入口坐席状态更新 //消息接收入口坐席状态更新
@OnEvent(value = "agentstatus") @OnEvent(value = "agentstatus")
public void onEvent(SocketIOClient client, AckRequest request, AgentStatusMessage data) public void onEvent(SocketIOClient client, AckRequest request, AgentStatusMessage data) {
{
System.out.println(data.getMessage()); System.out.println(data.getMessage());
} }
//消息接收入口收发消息用户向坐席发送消息和 坐席向用户发送消息 //消息接收入口收发消息用户向坐席发送消息和 坐席向用户发送消息
@OnEvent(value = "message") @OnEvent(value = "message")
public void onEvent(SocketIOClient client, AckRequest request, ChatMessage data) public void onEvent(SocketIOClient client, AckRequest request, ChatMessage data) {
{
String orgi = client.getHandshakeData().getSingleUrlParam("orgi"); String orgi = client.getHandshakeData().getSingleUrlParam("orgi");
String aiid = client.getHandshakeData().getSingleUrlParam("aiid"); String aiid = client.getHandshakeData().getSingleUrlParam("aiid");
String user = client.getHandshakeData().getSingleUrlParam("userid"); String user = client.getHandshakeData().getSingleUrlParam("userid");
@ -193,6 +186,5 @@ public class ChatbotEventHandler
CacheHelper.getOnlineUserCacheBean().put(user, aiUser, UKDataContext.SYSTEM_ORGI); CacheHelper.getOnlineUserCacheBean().put(user, aiUser, UKDataContext.SYSTEM_ORGI);
} }
} }
UKTools.ai(data);
} }
} }

View File

@ -253,7 +253,17 @@ public class IMController extends Handler {
*/ */
@RequestMapping("/online") @RequestMapping("/online")
@Menu(type = "im", subtype = "online", access = true) @Menu(type = "im", subtype = "online", access = true)
public SseEmitter callable(HttpServletRequest request, HttpServletResponse response, @Valid Contacts contacts, final @Valid String orgi, final @Valid String sessionid, @Valid String appid, final @Valid String userid, @Valid String sign, final @Valid String client, final @Valid String title, final @Valid String traceid) { public SseEmitter callable(HttpServletRequest request,
HttpServletResponse response,
@Valid Contacts contacts,
final @Valid String orgi,
final @Valid String sessionid,
@Valid String appid,
final @Valid String userid,
@Valid String sign,
final @Valid String client,
final @Valid String title,
final @Valid String traceid) {
BlackEntity black = (BlackEntity) CacheHelper.getSystemCacheBean().getCacheObject(userid, orgi); BlackEntity black = (BlackEntity) CacheHelper.getSystemCacheBean().getCacheObject(userid, orgi);
SseEmitter retSseEmitter = null; SseEmitter retSseEmitter = null;
if ((black == null || (black.getEndtime() != null && black.getEndtime().before(new Date())))) { if ((black == null || (black.getEndtime() != null && black.getEndtime().before(new Date())))) {
@ -288,9 +298,21 @@ public class IMController extends Handler {
contacts = processContacts(orgi, contacts, appid, userid); contacts = processContacts(orgi, contacts, appid, userid);
} }
if (StringUtils.isNotBlank(sign)) { if (StringUtils.isNotBlank(sign)) {
OnlineUserUtils.online(super.getIMUser(request, sign, contacts != null ? contacts.getName() : null), orgi, sessionid, UKDataContext.OnlineUserTypeStatus.WEBIM.toString(), request, UKDataContext.ChannelTypeEnum.WEBIM.toString(), appid, contacts, invite); try {
OnlineUserUtils.online(super.getIMUser(request, sign, contacts != null ? contacts.getName() : null),
orgi,
sessionid,
UKDataContext.OnlineUserTypeStatus.WEBIM.toString(),
request,
UKDataContext.ChannelTypeEnum.WEBIM.toString(),
appid,
contacts,
invite);
} catch (java.lang.ClassCastException e) {
// #TODO workaround for
// https://github.com/chatopera/cosin/issues/75
}
} }
OnlineUserUtils.webIMClients.putClient(userid, new WebIMClient(userid, client, emitter)); OnlineUserUtils.webIMClients.putClient(userid, new WebIMClient(userid, client, emitter));
} }
} }