|
|
@@ -63,7 +63,9 @@ public class WebSocket {
|
|
63
|
63
|
private static Map<String, Integer> servicers = new HashMap<>();// 客服状态(1:空闲,2:忙碌)
|
|
64
|
64
|
private static Map<String, Integer> customers = new HashMap<>();// 客户来源
|
|
65
|
65
|
|
|
66
|
|
- private static Map<String, String> conversation = new HashMap<>();// 历史聊天消息id
|
|
|
66
|
+ //private static Map<String, String> conversation = new HashMap<>();// 历史聊天消息id
|
|
|
67
|
+
|
|
|
68
|
+ private static Map<String, Map<String, Object>> conversations = new HashMap<>();// 历史聊天消息id
|
|
67
|
69
|
private static Map<String, String> khkfs = new HashMap<>(); // 客户-客服映射关系
|
|
68
|
70
|
|
|
69
|
71
|
/**
|
|
|
@@ -529,150 +531,181 @@ public class WebSocket {
|
|
529
|
531
|
message.setCreateTime(new Date());
|
|
530
|
532
|
|
|
531
|
533
|
String content = "";
|
|
532
|
|
- if ("text".equals(msgType)) {
|
|
533
|
|
- content = map.get("Content");
|
|
534
|
|
- message.setMsgContent(content);
|
|
535
|
|
- }
|
|
536
|
|
- else if ("image".equals(msgType) || "voice".equals(msgType)) {
|
|
537
|
|
- String mediaId=map.get("MediaId");
|
|
538
|
|
- message.setMsgMediaid(mediaId);
|
|
539
|
|
- String fn = System.currentTimeMillis()+".";
|
|
540
|
|
- String fileName = fn + "jpg";
|
|
541
|
|
- if ("voice".equals(msgType)) {
|
|
542
|
|
- message.setMsgFormat(map.get("Format"));
|
|
543
|
|
- fileName = fn + map.get("Format");
|
|
544
|
|
- }
|
|
545
|
|
- //下载临时素材
|
|
546
|
|
- try {
|
|
547
|
|
- String path = "files/online/" + DateHelper.getDate();
|
|
548
|
|
- File pFile = new File(path);
|
|
549
|
|
- if (!pFile.exists()) pFile.mkdirs();
|
|
550
|
|
- String filePath = pFile.getAbsolutePath() + "/" + fileName;
|
|
551
|
|
- File lFile = new File(filePath);
|
|
552
|
|
- if (!lFile.exists()) lFile.createNewFile();
|
|
553
|
|
- File file = webSocket.wxMpService.getMaterialService().mediaDownload(mediaId);
|
|
554
|
|
- InputStream inputStream = new FileInputStream(file);
|
|
555
|
|
- byte[] buffer = new byte[1024];
|
|
556
|
|
- FileOutputStream outputStream = new FileOutputStream(lFile);
|
|
557
|
|
- int length;
|
|
558
|
|
- while ((length = inputStream.read(buffer)) > 0) {
|
|
559
|
|
- outputStream.write(buffer, 0, length);
|
|
560
|
|
- }
|
|
561
|
|
- inputStream.close();
|
|
562
|
|
- outputStream.close();
|
|
563
|
|
- message.setMsgFile(path + "/" + fileName);
|
|
564
|
|
- content = path + "/" + fileName;
|
|
565
|
|
- } catch (WxErrorException e) {
|
|
566
|
|
- log.error("下载文件失败", e);
|
|
567
|
|
- throw new RuntimeException(e);
|
|
568
|
|
- } catch (IOException e) {
|
|
569
|
|
- log.error("下载文件失败", e);
|
|
570
|
|
- throw new RuntimeException(e);
|
|
571
|
|
- }
|
|
572
|
|
- }
|
|
573
|
|
-
|
|
574
|
|
- if (!customers.containsKey(sendUser)) {
|
|
575
|
|
- customers.put(sendUser, 2);
|
|
576
|
|
- addCustomer(sendUser,2,null);
|
|
577
|
|
- };
|
|
578
|
|
-
|
|
579
|
|
- String kf = khkfs.get(sendUser);
|
|
580
|
|
- if(content.contains("人工"))
|
|
|
534
|
+ if (!"event".equals(msgType))
|
|
581
|
535
|
{
|
|
582
|
|
-
|
|
583
|
|
-
|
|
584
|
|
- if (StringHelper.isEmpty(kf)||servicers.get(kf)==2) {
|
|
585
|
|
- kf = servicerQueue.poll();
|
|
586
|
|
- if (!StringHelper.isEmpty(kf)) {
|
|
587
|
|
- servicerQueue.add(kf);
|
|
588
|
|
- khkfs.put(sendUser, kf);
|
|
589
|
|
- WxMpKefuMessage wechatMessage = WxMpKefuMessage.TEXT().toUser(sendUser).content("正在为您转接人工,请稍后").build();
|
|
590
|
|
- try {
|
|
591
|
|
- webSocket.wxMpService.getKefuService().sendKefuMessage(wechatMessage);
|
|
592
|
|
- } catch (WxErrorException e) {
|
|
593
|
|
- throw new RuntimeException(e);
|
|
594
|
|
- }
|
|
595
|
|
- addMsg(sendUser,"正在为您转接人工,请稍后");
|
|
|
536
|
+ if ("text".equals(msgType)) {
|
|
|
537
|
+ content = map.get("Content");
|
|
|
538
|
+ message.setMsgContent(content);
|
|
|
539
|
+ }
|
|
|
540
|
+ else if ("image".equals(msgType) || "voice".equals(msgType)) {
|
|
|
541
|
+ String mediaId=map.get("MediaId");
|
|
|
542
|
+ message.setMsgMediaid(mediaId);
|
|
|
543
|
+ String fn = System.currentTimeMillis()+".";
|
|
|
544
|
+ String fileName = fn + "jpg";
|
|
|
545
|
+ if ("voice".equals(msgType)) {
|
|
|
546
|
+ message.setMsgFormat(map.get("Format"));
|
|
|
547
|
+ fileName = fn + map.get("Format");
|
|
596
|
548
|
}
|
|
597
|
|
- else
|
|
598
|
|
- {
|
|
599
|
|
- webSocket.messageService.insert(message);
|
|
600
|
|
- String answer= configService.getConfigValue("prompt");
|
|
601
|
|
- WxMpKefuMessage wechatMessage = WxMpKefuMessage.TEXT().toUser(sendUser).content(answer).build();
|
|
602
|
|
- try {
|
|
603
|
|
- webSocket.wxMpService.getKefuService().sendKefuMessage(wechatMessage);
|
|
604
|
|
- } catch (WxErrorException e) {
|
|
605
|
|
- throw new RuntimeException(e);
|
|
|
549
|
+ //下载临时素材
|
|
|
550
|
+ try {
|
|
|
551
|
+ String path = "files/online/" + DateHelper.getDate();
|
|
|
552
|
+ File pFile = new File(path);
|
|
|
553
|
+ if (!pFile.exists()) pFile.mkdirs();
|
|
|
554
|
+ String filePath = pFile.getAbsolutePath() + "/" + fileName;
|
|
|
555
|
+ File lFile = new File(filePath);
|
|
|
556
|
+ if (!lFile.exists()) lFile.createNewFile();
|
|
|
557
|
+ File file = webSocket.wxMpService.getMaterialService().mediaDownload(mediaId);
|
|
|
558
|
+ InputStream inputStream = new FileInputStream(file);
|
|
|
559
|
+ byte[] buffer = new byte[1024];
|
|
|
560
|
+ FileOutputStream outputStream = new FileOutputStream(lFile);
|
|
|
561
|
+ int length;
|
|
|
562
|
+ while ((length = inputStream.read(buffer)) > 0) {
|
|
|
563
|
+ outputStream.write(buffer, 0, length);
|
|
606
|
564
|
}
|
|
607
|
|
- addMsg(sendUser,answer);
|
|
|
565
|
+ inputStream.close();
|
|
|
566
|
+ outputStream.close();
|
|
|
567
|
+ message.setMsgFile(path + "/" + fileName);
|
|
|
568
|
+ content = path + "/" + fileName;
|
|
|
569
|
+ } catch (WxErrorException e) {
|
|
|
570
|
+ log.error("下载文件失败", e);
|
|
|
571
|
+ throw new RuntimeException(e);
|
|
|
572
|
+ } catch (IOException e) {
|
|
|
573
|
+ log.error("下载文件失败", e);
|
|
|
574
|
+ throw new RuntimeException(e);
|
|
608
|
575
|
}
|
|
609
|
576
|
}
|
|
610
|
|
- }
|
|
|
577
|
+
|
|
|
578
|
+ if (!customers.containsKey(sendUser)) {
|
|
|
579
|
+ customers.put(sendUser, 2);
|
|
|
580
|
+ addCustomer(sendUser,2,null);
|
|
|
581
|
+ };
|
|
|
582
|
+
|
|
|
583
|
+ String kf = khkfs.get(sendUser);
|
|
|
584
|
+// if(content.contains("人工"))
|
|
|
585
|
+// {
|
|
|
586
|
+//
|
|
|
587
|
+//
|
|
|
588
|
+// if (StringHelper.isEmpty(kf)||servicers.get(kf)==2) {
|
|
|
589
|
+// kf = servicerQueue.poll();
|
|
|
590
|
+// if (!StringHelper.isEmpty(kf)) {
|
|
|
591
|
+// servicerQueue.add(kf);
|
|
|
592
|
+// khkfs.put(sendUser, kf);
|
|
|
593
|
+// WxMpKefuMessage wechatMessage = WxMpKefuMessage.TEXT().toUser(sendUser).content("正在为您转接人工,请稍后").build();
|
|
|
594
|
+// try {
|
|
|
595
|
+// webSocket.wxMpService.getKefuService().sendKefuMessage(wechatMessage);
|
|
|
596
|
+// } catch (WxErrorException e) {
|
|
|
597
|
+// throw new RuntimeException(e);
|
|
|
598
|
+// }
|
|
|
599
|
+// addMsg(sendUser,"正在为您转接人工,请稍后");
|
|
|
600
|
+// }
|
|
|
601
|
+// else
|
|
|
602
|
+// {
|
|
|
603
|
+// webSocket.messageService.insert(message);
|
|
|
604
|
+// String answer= configService.getConfigValue("prompt");
|
|
|
605
|
+// WxMpKefuMessage wechatMessage = WxMpKefuMessage.TEXT().toUser(sendUser).content(answer).build();
|
|
|
606
|
+// try {
|
|
|
607
|
+// webSocket.wxMpService.getKefuService().sendKefuMessage(wechatMessage);
|
|
|
608
|
+// } catch (WxErrorException e) {
|
|
|
609
|
+// throw new RuntimeException(e);
|
|
|
610
|
+// }
|
|
|
611
|
+// addMsg(sendUser,answer);
|
|
|
612
|
+// }
|
|
|
613
|
+// }
|
|
|
614
|
+// }
|
|
611
|
615
|
|
|
612
|
616
|
|
|
613
|
|
- if (!StringHelper.isEmpty(kf)) {
|
|
614
|
|
- message.setKfUser(kf);
|
|
615
|
|
- Session session1 = kfsessionPool.get(kf);
|
|
616
|
|
- if (session1 != null) {
|
|
617
|
|
- MessageView mv = new MessageView();
|
|
618
|
|
- mv.setStatus("success");
|
|
619
|
|
- mv.setType(msgType);
|
|
620
|
|
- mv.setContent(content);
|
|
621
|
|
- mv.setUser(sendUser);
|
|
622
|
|
- session1.getAsyncRemote().sendText(JSON.toJSONString(mv));
|
|
|
617
|
+ if (!StringHelper.isEmpty(kf)) {
|
|
|
618
|
+ message.setKfUser(kf);
|
|
|
619
|
+ Session session1 = kfsessionPool.get(kf);
|
|
|
620
|
+ if (session1 != null) {
|
|
|
621
|
+ MessageView mv = new MessageView();
|
|
|
622
|
+ mv.setStatus("success");
|
|
|
623
|
+ mv.setType(msgType);
|
|
|
624
|
+ mv.setContent(content);
|
|
|
625
|
+ mv.setUser(sendUser);
|
|
|
626
|
+ session1.getAsyncRemote().sendText(JSON.toJSONString(mv));
|
|
|
627
|
+ }
|
|
|
628
|
+ webSocket.messageService.insert(message);
|
|
623
|
629
|
}
|
|
624
|
|
- webSocket.messageService.insert(message);
|
|
625
|
|
- }
|
|
626
|
|
- else if (!content.contains("人工"))
|
|
627
|
|
- {
|
|
628
|
|
- webSocket.messageService.insert(message);
|
|
629
|
|
- DifyChatMessagesInput input=new DifyChatMessagesInput();
|
|
630
|
|
- LambdaQueryWrapper<DifyAssistant> qw = new LambdaQueryWrapper<>();
|
|
631
|
|
- qw.eq(DifyAssistant::getCode, "mxyh");
|
|
632
|
|
- DifyAssistant dify=difyAssistantService.getEntity(qw);
|
|
633
|
|
- if (dify!=null)
|
|
|
630
|
+ else
|
|
634
|
631
|
{
|
|
635
|
|
- input.setId(dify.getAssistantId());
|
|
636
|
|
- input.setQuery(content);
|
|
637
|
|
- input.setConversation_id(conversation.getOrDefault(sendUser, ""));
|
|
638
|
|
- input.setAuto_generate_name(false);
|
|
639
|
|
- try
|
|
|
632
|
+ if ("text".equals(msgType))
|
|
640
|
633
|
{
|
|
641
|
|
- String waitAnswer="正在为你查询,请稍后";
|
|
642
|
|
- WxMpKefuMessage wechatMessage = WxMpKefuMessage.TEXT().toUser(sendUser).content(waitAnswer).build();
|
|
643
|
|
- try {
|
|
644
|
|
- webSocket.wxMpService.getKefuService().sendKefuMessage(wechatMessage);
|
|
645
|
|
- } catch (WxErrorException e) {
|
|
646
|
|
- throw new RuntimeException(e);
|
|
647
|
|
- }
|
|
648
|
|
- addMsg(sendUser,waitAnswer);
|
|
649
|
|
- String json=difyAssistantService.sendMessages(input,dify,sendUser);
|
|
650
|
|
- ObjectMapper mapper = new ObjectMapper();
|
|
651
|
|
- JsonNode rootNode = mapper.readTree(json);
|
|
652
|
|
- String answer = rootNode.get("answer").asText();
|
|
653
|
|
- String conversationId = rootNode.get("conversation_id").asText();
|
|
654
|
|
- if(!conversation.containsKey(sendUser))
|
|
|
634
|
+ webSocket.messageService.insert(message);
|
|
|
635
|
+ DifyChatMessagesInput input=new DifyChatMessagesInput();
|
|
|
636
|
+ LambdaQueryWrapper<DifyAssistant> qw = new LambdaQueryWrapper<>();
|
|
|
637
|
+ qw.eq(DifyAssistant::getCode, "mxyh");
|
|
|
638
|
+ DifyAssistant dify=difyAssistantService.getEntity(qw);
|
|
|
639
|
+ if (dify!=null)
|
|
655
|
640
|
{
|
|
656
|
|
- conversation.remove(sendUser);
|
|
657
|
|
- conversation.put(sendUser,conversationId);
|
|
658
|
|
- }
|
|
659
|
|
- WxMpKefuMessage wechatMsg = WxMpKefuMessage.TEXT().toUser(sendUser).content(answer).build();
|
|
660
|
|
- try {
|
|
661
|
|
- webSocket.wxMpService.getKefuService().sendKefuMessage(wechatMsg);
|
|
662
|
|
- } catch (WxErrorException e) {
|
|
663
|
|
- throw new RuntimeException(e);
|
|
664
|
|
- }
|
|
665
|
|
- addMsg(sendUser,answer);
|
|
|
641
|
+ input.setId(dify.getAssistantId());
|
|
|
642
|
+ input.setQuery(content);
|
|
|
643
|
+ Map<String, Object> conversationMap= conversations.get(sendUser);
|
|
|
644
|
+ String Conversation_id="";
|
|
|
645
|
+ if (conversationMap!=null)
|
|
|
646
|
+ {
|
|
|
647
|
+ Object Aime=conversationMap.get("conversationAime");
|
|
|
648
|
+ Object Id=conversationMap.get("conversationId");
|
|
|
649
|
+ if (Aime!=null)
|
|
|
650
|
+ {
|
|
|
651
|
+ Date time= (Date) conversationMap.get("conversationAime");
|
|
|
652
|
+ // 获取当前时间
|
|
|
653
|
+ Date now = new Date();
|
|
|
654
|
+ // 计算时间差的绝对值(毫秒)
|
|
|
655
|
+ long diffInMilliseconds = Math.abs(now.getTime() - time.getTime());
|
|
|
656
|
+ if (diffInMilliseconds <= (30 * 60 * 1000)&&Id!=null)
|
|
|
657
|
+ {
|
|
|
658
|
+ Conversation_id=Id.toString();
|
|
|
659
|
+ }
|
|
|
660
|
+ }
|
|
|
661
|
+ }
|
|
|
662
|
+ input.setConversation_id(Conversation_id);
|
|
|
663
|
+ input.setAuto_generate_name(false);
|
|
|
664
|
+ try
|
|
|
665
|
+ {
|
|
|
666
|
+// String waitAnswer="正在为你查询,请稍后";
|
|
|
667
|
+// WxMpKefuMessage wechatMessage = WxMpKefuMessage.TEXT().toUser(sendUser).content(waitAnswer).build();
|
|
|
668
|
+// try {
|
|
|
669
|
+// webSocket.wxMpService.getKefuService().sendKefuMessage(wechatMessage);
|
|
|
670
|
+// } catch (WxErrorException e) {
|
|
|
671
|
+// throw new RuntimeException(e);
|
|
|
672
|
+// }
|
|
|
673
|
+// addMsg(sendUser,waitAnswer);
|
|
|
674
|
+ String json=difyAssistantService.sendMessages(input,dify,sendUser);
|
|
|
675
|
+ ObjectMapper mapper = new ObjectMapper();
|
|
|
676
|
+ JsonNode rootNode = mapper.readTree(json);
|
|
|
677
|
+ String answer = rootNode.get("answer").asText();
|
|
|
678
|
+ String conversationId = rootNode.get("conversation_id").asText();
|
|
|
679
|
+ if(!conversations.containsKey(sendUser))
|
|
|
680
|
+ {
|
|
|
681
|
+ conversations.remove(sendUser);
|
|
|
682
|
+ Map<String,Object> conversationsMap=new HashMap<>();
|
|
|
683
|
+ conversationsMap.put("conversationId",conversationId);
|
|
|
684
|
+ conversationsMap.put("conversationAime",new Date());
|
|
|
685
|
+ conversations.put(sendUser,conversationsMap);
|
|
|
686
|
+ }
|
|
|
687
|
+ WxMpKefuMessage wechatMsg = WxMpKefuMessage.TEXT().toUser(sendUser).content(answer).build();
|
|
|
688
|
+ try {
|
|
|
689
|
+ webSocket.wxMpService.getKefuService().sendKefuMessage(wechatMsg);
|
|
|
690
|
+ } catch (WxErrorException e) {
|
|
|
691
|
+ throw new RuntimeException(e);
|
|
|
692
|
+ }
|
|
|
693
|
+ addMsg(sendUser,answer);
|
|
|
694
|
+
|
|
|
695
|
+ }
|
|
|
696
|
+ catch (Exception E)
|
|
|
697
|
+ {}
|
|
666
|
698
|
|
|
|
699
|
+ }
|
|
667
|
700
|
}
|
|
668
|
|
- catch (Exception E)
|
|
669
|
|
- {}
|
|
670
|
701
|
|
|
671
|
|
- }
|
|
672
|
702
|
|
|
673
|
703
|
|
|
|
704
|
+ }
|
|
|
705
|
+
|
|
674
|
706
|
}
|
|
675
|
707
|
|
|
|
708
|
+
|
|
676
|
709
|
}
|
|
677
|
710
|
public void sendMessage(String kf, String message) {
|
|
678
|
711
|
Session session1 = kfsessionPool.get(kf);
|