|
|
@@ -66,6 +66,7 @@ import com.fs.utils.SensitiveDataUtils;
|
|
|
import com.fs.voice.utils.StringUtil;
|
|
|
import com.fs.wxwork.dto.*;
|
|
|
import com.fs.wxwork.service.WxWorkService;
|
|
|
+import com.google.gson.Gson;
|
|
|
import com.vdurmont.emoji.EmojiParser;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.lang3.ObjectUtils;
|
|
|
@@ -384,7 +385,7 @@ public class AiHookServiceImpl implements AiHookService {
|
|
|
/** Ai回复 **/
|
|
|
@Async
|
|
|
@Override
|
|
|
- public R qwHookNotifyAiReply(Long qwUserId, Long sender,String qwContent,String uid,Integer type) {
|
|
|
+ public R qwHookNotifyAiReply(Long qwUserId, Long sender,String qwContent,String uid,Integer type, Boolean isNewVersion) {
|
|
|
if (qwContent==null||qwContent.isEmpty()){
|
|
|
return R.ok();
|
|
|
}
|
|
|
@@ -563,7 +564,12 @@ public class AiHookServiceImpl implements AiHookService {
|
|
|
redisCache.setCacheObject("reply:" + fastGptChatSession.getSessionId(),1,5,TimeUnit.MINUTES);
|
|
|
redisCache.setCacheObject("msg:" + fastGptChatSession.getSessionId(),contentEmj,5,TimeUnit.MINUTES);
|
|
|
log.info("等待");
|
|
|
- R r= sendAiMsg(replyI,fastGptChatSession,role,user,qwExternalContacts.getId(),config.getAPPKey(),qwExternalContacts,sender);
|
|
|
+ R r;
|
|
|
+ if(isNewVersion){
|
|
|
+ r= sendAiMsgNew(replyI,fastGptChatSession,role,user,qwExternalContacts.getId(),config.getAPPKey(),qwExternalContacts,sender);
|
|
|
+ } else {
|
|
|
+ r= sendAiMsg(replyI,fastGptChatSession,role,user,qwExternalContacts.getId(),config.getAPPKey(),qwExternalContacts,sender);
|
|
|
+ }
|
|
|
EventLogUtils.recordEventLog(sender,1L,1,user);
|
|
|
EventLogUtils.recordEventLog(sender,1L,2,user);
|
|
|
log.info("数据:{}", r);
|
|
|
@@ -591,7 +597,15 @@ public class AiHookServiceImpl implements AiHookService {
|
|
|
return R.ok();
|
|
|
}
|
|
|
String contentKh = result.getChoices().get(0).getMessage().getContent();
|
|
|
- String content = replace(result.getChoices().get(0).getMessage().getContent()).trim();
|
|
|
+ String content = null;
|
|
|
+ if(isNewVersion){
|
|
|
+ Gson gson = new Gson();
|
|
|
+ FastGptChatConversation fastGptChatConversation = gson.fromJson(contentKh, FastGptChatConversation.class);
|
|
|
+ content = fastGptChatConversation.getAiContent();
|
|
|
+ }else{
|
|
|
+ content = replace(result.getChoices().get(0).getMessage().getContent()).trim();
|
|
|
+ }
|
|
|
+
|
|
|
//计算token
|
|
|
List<ChatDetailTStreamFResult.ResponseNode> responseData = result.getResponseData();
|
|
|
int token=0;
|
|
|
@@ -611,19 +625,29 @@ public class AiHookServiceImpl implements AiHookService {
|
|
|
notifyArtificial(fastGptChatSession.getSessionId(),user,qwExternalContacts.getName()," 触发关键词",qwExternalContacts.getId(),sender);
|
|
|
return R.ok();
|
|
|
}
|
|
|
- //ai回复文字长度大于500就转人工
|
|
|
- if(content.length() > 500){
|
|
|
- log.error("回复长度异常:"+role.getRoleId()+":"+qwExternalContacts.getName());
|
|
|
- notifyArtificial(fastGptChatSession.getSessionId(),user,qwExternalContacts.getName()," 回复长度异常",qwExternalContacts.getId(),sender);
|
|
|
- return R.ok();
|
|
|
+ if(!isNewVersion){
|
|
|
+ //ai回复文字长度大于500就转人工
|
|
|
+ if(content.length() > 500){
|
|
|
+ log.error("回复长度异常:"+role.getRoleId()+":"+qwExternalContacts.getName());
|
|
|
+ notifyArtificial(fastGptChatSession.getSessionId(),user,qwExternalContacts.getName()," 回复长度异常",qwExternalContacts.getId(),sender);
|
|
|
+ return R.ok();
|
|
|
+ }
|
|
|
}
|
|
|
+
|
|
|
if (result.isLongText()){
|
|
|
//新增用户信息
|
|
|
addUserInfo(contentKh, qwExternalContacts.getId(),fastGptChatSession);
|
|
|
- if (type==16){
|
|
|
- sendAiVoiceMsg(content,sender,uid,serverId,user);
|
|
|
- }else {
|
|
|
+ //发送图片消息
|
|
|
+ sendImgMsg(contentKh,sender,uid,serverId);
|
|
|
+
|
|
|
+ if(isNewVersion){
|
|
|
sendAiMsg(content,sender,uid,serverId);
|
|
|
+ } else {
|
|
|
+ if (type==16){
|
|
|
+ sendAiVoiceMsg(content,sender,uid,serverId,user);
|
|
|
+ }else {
|
|
|
+ sendAiMsg(content,sender,uid,serverId);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
}else {
|
|
|
@@ -638,11 +662,17 @@ public class AiHookServiceImpl implements AiHookService {
|
|
|
List<String> countList = countString(content);
|
|
|
//新增用户信息
|
|
|
addUserInfo(contentKh, qwExternalContacts.getId(),fastGptChatSession);
|
|
|
+ //发送图片消息
|
|
|
+ sendImgMsg(contentKh,sender,uid,serverId);
|
|
|
for (String msg : countList) {
|
|
|
- if (type==16){
|
|
|
- sendAiVoiceMsg(msg,sender,uid,serverId,user);
|
|
|
- }else {
|
|
|
+ if(isNewVersion){
|
|
|
sendAiMsg(msg,sender,uid,serverId);
|
|
|
+ } else {
|
|
|
+ if (type==16){
|
|
|
+ sendAiVoiceMsg(msg,sender,uid,serverId,user);
|
|
|
+ }else {
|
|
|
+ sendAiMsg(msg,sender,uid,serverId);
|
|
|
+ }
|
|
|
}
|
|
|
try {
|
|
|
Thread.sleep(10000);
|
|
|
@@ -658,6 +688,11 @@ public class AiHookServiceImpl implements AiHookService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ //
|
|
|
+ // todo 把当前的内容转成jsonObject,然后取出isRepository,判断是否为知识库,如果不是知识库的,就存储到表中
|
|
|
+ // 什么时候销售回复,销售回复的在哪里可以看到
|
|
|
+// contentKh
|
|
|
+
|
|
|
aiEventProcess(sender, uid, role, contentKh, user, fastGptChatSession, serverId,qwExternalContacts);
|
|
|
|
|
|
|
|
|
@@ -670,6 +705,39 @@ public class AiHookServiceImpl implements AiHookService {
|
|
|
return R.ok();
|
|
|
}
|
|
|
|
|
|
+ private void sendImgMsg(String contentKh, Long sender, String uuid, Long serverId) {
|
|
|
+ com.alibaba.fastjson.JSONObject jsonObject = com.alibaba.fastjson.JSONObject.parseObject(contentKh);
|
|
|
+ JSONArray imgUrls = jsonObject.getJSONArray("imgUrl");
|
|
|
+
|
|
|
+
|
|
|
+ if(imgUrls != null && !imgUrls.isEmpty()){
|
|
|
+ WxWorkResponseDTO<WxwUploadCdnLinkImgRespDTO> dto = new WxWorkResponseDTO<>();
|
|
|
+ for (Object imgUrl : imgUrls) {
|
|
|
+ //1.上传cdn网络图片
|
|
|
+ if(imgUrl != null){
|
|
|
+ String imgUrlString = imgUrl.toString();
|
|
|
+ WxwUploadCdnLinkImgDTO wxwUploadCdnLinkImgDTO = new WxwUploadCdnLinkImgDTO();
|
|
|
+ wxwUploadCdnLinkImgDTO.setUuid(uuid);
|
|
|
+ wxwUploadCdnLinkImgDTO.setUrl(imgUrlString);
|
|
|
+ dto = wxWorkService.uploadCdnLinkImg(wxwUploadCdnLinkImgDTO,serverId);
|
|
|
+ }
|
|
|
+ //图片上传成功后再发送图片
|
|
|
+ if("成功".equals(dto.getErrmsg()) && imgUrl != null){
|
|
|
+ WxwUploadCdnLinkImgRespDTO imgRespDTO = dto.getData();
|
|
|
+ WxwSendCDNImgMsgDTO wxwSendCDNImgMsgDTO = new WxwSendCDNImgMsgDTO();
|
|
|
+ wxwSendCDNImgMsgDTO.setSend_userid(sender);
|
|
|
+ wxwSendCDNImgMsgDTO.setUuid(uuid);
|
|
|
+ wxwSendCDNImgMsgDTO.setIsRoom(false);
|
|
|
+ wxwSendCDNImgMsgDTO.setCdnkey(imgRespDTO.getCdn_key());
|
|
|
+ wxwSendCDNImgMsgDTO.setAeskey(imgRespDTO.getAes_key());
|
|
|
+ wxwSendCDNImgMsgDTO.setMd5(imgRespDTO.getMd5());
|
|
|
+ wxwSendCDNImgMsgDTO.setFileSize(imgRespDTO.getSize());
|
|
|
+ wxWorkService.SendCDNImgMsg(wxwSendCDNImgMsgDTO, serverId);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 根据发送者id设置用户是否为首次回复
|
|
|
* @param sender 发送者id
|
|
|
@@ -1528,6 +1596,69 @@ public class AiHookServiceImpl implements AiHookService {
|
|
|
}
|
|
|
|
|
|
}
|
|
|
+
|
|
|
+ /** 发送Ai消息,-- -----从saas移动过来,主要是返回结构和内部逻辑跟之前的有区别 (2026年4月17日16点08分) **/
|
|
|
+ private R sendAiMsgNew(Integer i,FastGptChatSession fastGptChatSession, FastGptRole role,QwUser user,Long qwExternalContactsId,String appKey,QwExternalContact qwExternalContacts,Long sender){
|
|
|
+ //等待5秒
|
|
|
+ try {
|
|
|
+ Thread.sleep(500); // 5000 毫秒 = 5 秒
|
|
|
+ } catch (InterruptedException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ //获取现在的次数
|
|
|
+ Integer reply = (Integer)redisCache.getCacheObject("reply:" + fastGptChatSession.getSessionId());
|
|
|
+ if (reply!=i){
|
|
|
+ //次数变动 重新等待5秒
|
|
|
+ R r = sendAiMsg(reply, fastGptChatSession, role, user, qwExternalContactsId, appKey, qwExternalContacts,sender);
|
|
|
+ return r;
|
|
|
+ }else {
|
|
|
+ System.out.println("开始ai回答");
|
|
|
+ ChatParam param=new ChatParam();
|
|
|
+ param.setChatId(fastGptChatSession.getChatId());
|
|
|
+ param.setStream(false);
|
|
|
+ param.setDetail(true);
|
|
|
+ ChatParam.Variables variables=new ChatParam.Variables();
|
|
|
+ variables.setUid(user.getFastGptRoleId().toString());
|
|
|
+ variables.setName("test");
|
|
|
+ param.setVariables(variables);
|
|
|
+ List<ChatParam.Message> messageList=new ArrayList<ChatParam.Message>();
|
|
|
+ param.setMessages(messageList);
|
|
|
+ //添加看客记录
|
|
|
+ //addCourseWatchLog(qwExternalContactsId);
|
|
|
+ String msgC = (String)redisCache.getCacheObject("msg:" + fastGptChatSession.getSessionId());
|
|
|
+
|
|
|
+ if (("今正科技".equals(cloudHostProper.getCompanyName()))) {
|
|
|
+ //处理名称替换
|
|
|
+ if (role.getReminderWords() != null && !role.getReminderWords().isEmpty() && role.getReminderWords().contains("#销售名称#")) {
|
|
|
+ CompanyUser companyUser = companyUserMapper.selectCompanyUserByQwUserId(user.getId());
|
|
|
+ if (companyUser != null) {
|
|
|
+ role.setReminderWords(role.getReminderWords().replace("#销售名称#", companyUser.getNickName()));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //添加关键词
|
|
|
+ addPromptWordNew(messageList,msgC,qwExternalContactsId,role,fastGptChatSession);
|
|
|
+ R r = chatService.initiatingTakeChat(param, "http://129.28.170.206:3000/api/", appKey);
|
|
|
+ Object data1 = r.get("data");
|
|
|
+ if(!(data1 instanceof KnowledgeBaseResult)){
|
|
|
+ ChatDetailTStreamFResult data = (ChatDetailTStreamFResult) r.get("data");
|
|
|
+ EventLogUtils.createEventTokenLog("发起对话",user,sender,data);
|
|
|
+ }
|
|
|
+
|
|
|
+ Integer reply2 = (Integer)redisCache.getCacheObject("reply:" + fastGptChatSession.getSessionId());
|
|
|
+ //次数变动 重新等待5秒
|
|
|
+ if (reply2!=i){
|
|
|
+ System.out.println("等待");
|
|
|
+ R r1 = sendAiMsg(reply, fastGptChatSession, role, user, qwExternalContactsId, appKey, qwExternalContacts,sender);
|
|
|
+ return r1;
|
|
|
+ }
|
|
|
+ addSaveAiMsg(2,1,messageList.get(0).getContent(),user,fastGptChatSession.getSessionId(),role.getRoleId(),qwExternalContacts,fastGptChatSession.getUserId(),null,null,null);
|
|
|
+ return r;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
/** 增加课程信息 **/
|
|
|
private void addCourseWatchLog(Long id) {
|
|
|
FsCourseWatchLogVO log = fsCourseWatchLogMapper.selectFsCourseWatchLogByExtId(id);
|
|
|
@@ -1560,6 +1691,73 @@ public class AiHookServiceImpl implements AiHookService {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ /** 组装发送AI内容 **/
|
|
|
+ private void addPromptWordNew(List<ChatParam.Message> messageList,String count,Long extId,FastGptRole role,FastGptChatSession fastGptChatSession){
|
|
|
+
|
|
|
+ FastGptChatConversation conversation = new FastGptChatConversation();
|
|
|
+ conversation.setAiInfo(new com.alibaba.fastjson.JSONObject());
|
|
|
+ conversation.setUserInfo(new com.alibaba.fastjson.JSONObject());
|
|
|
+ conversation.setHistory(new com.alibaba.fastjson.JSONObject());
|
|
|
+
|
|
|
+ //组装客户信息
|
|
|
+ String sessionUserInfo = fastGptChatSession.getUserInfo();
|
|
|
+ String[] split = role.getUserInfo().split(",");
|
|
|
+ com.alibaba.fastjson.JSONObject userInfo = conversation.getUserInfo();
|
|
|
+ if(sessionUserInfo != null){
|
|
|
+ Map<String,Object> map = com.alibaba.fastjson.JSONObject.parseObject(sessionUserInfo, HashMap.class);
|
|
|
+ if(role.getUserInfo() != null){
|
|
|
+ for (String name : split) {
|
|
|
+ if (name != null) {
|
|
|
+ Object value = map.get(name);
|
|
|
+ if(value != null){
|
|
|
+ userInfo.put(name,value);
|
|
|
+ }else{
|
|
|
+ userInfo.put(name,"");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+ for (String name : split) {
|
|
|
+ if (name != null) {
|
|
|
+ userInfo.put(name,"");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ List<FastGptChatMsg> msgs=fastGptChatMsgService.selectFastGptChatMsgByMsgSessionIdAndExtId(fastGptChatSession.getSessionId(),extId);
|
|
|
+ if (!msgs.isEmpty()){
|
|
|
+ com.alibaba.fastjson.JSONObject history = conversation.getHistory();
|
|
|
+ Collections.reverse(msgs);
|
|
|
+ msgs.remove(msgs.size() - 1);
|
|
|
+ for (FastGptChatMsg msg : msgs) {
|
|
|
+ Integer sendType = msg.getSendType();
|
|
|
+ String content = msg.getContent();
|
|
|
+ if(sendType!=1){
|
|
|
+ if (content!=null&&content.length()>500){
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ history.put(sendType==1?"user":"ai",content);
|
|
|
+ }
|
|
|
+ conversation.setHistory(history);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (count!=null&& !count.isEmpty()){
|
|
|
+ conversation.setUserContent(count);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ ChatParam.Message message1=new ChatParam.Message();
|
|
|
+ message1.setRole("user");
|
|
|
+ Gson gson = new Gson();
|
|
|
+ String jsonStr = gson.toJson(conversation);
|
|
|
+ message1.setContent(jsonStr);
|
|
|
+ messageList.add(message1);
|
|
|
+ }
|
|
|
+
|
|
|
/** 组装发送AI内容 **/
|
|
|
private void addPromptWord(List<ChatParam.Message> messageList,String count,Long extId,String words,String countInfo,Long sessionId){
|
|
|
|