|
|
|
@ -1,25 +1,25 @@
|
|
|
|
|
package com.supervision.service.impl;
|
|
|
|
|
|
|
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
|
|
import cn.hutool.http.HttpUtil;
|
|
|
|
|
import cn.hutool.json.JSONUtil;
|
|
|
|
|
import com.supervision.exception.BusinessException;
|
|
|
|
|
import com.supervision.model.User;
|
|
|
|
|
import com.supervision.paddlespeech.AsrUtil;
|
|
|
|
|
import com.supervision.paddlespeech.TtsUtil;
|
|
|
|
|
import com.supervision.rasa.RasaUtil;
|
|
|
|
|
import com.supervision.rasa.dto.RasaReqDTO;
|
|
|
|
|
import com.supervision.rasa.dto.RasaResDTO;
|
|
|
|
|
import com.supervision.pojo.vo.ReplyVoiceResVO;
|
|
|
|
|
import com.supervision.service.AskService;
|
|
|
|
|
import com.supervision.util.UserUtil;
|
|
|
|
|
import com.supervision.websocket.cache.WebSocketUserCache;
|
|
|
|
|
import com.supervision.websocket.dto.ActionDTO;
|
|
|
|
|
import com.supervision.websocket.dto.SocketMessageDTO;
|
|
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
import org.springframework.web.socket.TextMessage;
|
|
|
|
|
import org.springframework.web.socket.WebSocketSession;
|
|
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
|
|
@Service
|
|
|
|
|
@RequiredArgsConstructor
|
|
|
|
@ -27,7 +27,7 @@ public class AskServiceImpl implements AskService {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void handlerMessageBySocket(SocketMessageDTO socketMessageDTO) {
|
|
|
|
|
public void handlerMessageBySocket(SocketMessageDTO socketMessageDTO) throws IOException {
|
|
|
|
|
// 首先获取消息的类型
|
|
|
|
|
String text;
|
|
|
|
|
if (0 == socketMessageDTO.getMessageType()) {
|
|
|
|
@ -41,8 +41,52 @@ public class AskServiceImpl implements AskService {
|
|
|
|
|
throw new BusinessException("语音消息不能为空");
|
|
|
|
|
}
|
|
|
|
|
// 进行rasa对话
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
List<String> rasaResultList = RasaUtil.talkRasa(text, socketMessageDTO.getSocketId());
|
|
|
|
|
WebSocketSession session = WebSocketUserCache.getSession(socketMessageDTO.getSocketId());
|
|
|
|
|
for (String rasaResult : rasaResultList) {
|
|
|
|
|
if (StrUtil.isNotBlank(rasaResult)){
|
|
|
|
|
// 这里校验,rasa回复的结果是不是action
|
|
|
|
|
// 这里设置的模板,对于action的动作全部是用---进行标记,详情看生成rasa的yml的代码:RasaServiceImpl.generateDomain
|
|
|
|
|
// ---ancillary---xxx
|
|
|
|
|
// ---tool---xxx
|
|
|
|
|
if (rasaResult.startsWith("---")){
|
|
|
|
|
// ["","ancillary","xxx"]
|
|
|
|
|
List<String> actionList = StrUtil.split(rasaResult, "---");
|
|
|
|
|
if (actionList.size() > 2){
|
|
|
|
|
ActionDTO actionDTO = new ActionDTO();
|
|
|
|
|
actionDTO.setActionType(actionList.get(1));
|
|
|
|
|
actionDTO.setActionId(actionList.get(2));
|
|
|
|
|
// 在这里给socket回复,设置为动作
|
|
|
|
|
SocketMessageDTO res = new SocketMessageDTO();
|
|
|
|
|
res.setSocketId(socketMessageDTO.getSocketId());
|
|
|
|
|
res.setUserId(UserUtil.getUser().getId());
|
|
|
|
|
res.setAction(actionDTO);
|
|
|
|
|
res.setType(2);
|
|
|
|
|
session.sendMessage(new TextMessage(JSONUtil.toJsonStr(res)));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}else {
|
|
|
|
|
// 走到这里,说明是文字,这个时候文字转语音
|
|
|
|
|
String replyVoiceResVO = TtsUtil.ttsTransform(rasaResult);
|
|
|
|
|
// 在这里给socket回复
|
|
|
|
|
SocketMessageDTO res = new SocketMessageDTO();
|
|
|
|
|
res.setSocketId(socketMessageDTO.getSocketId());
|
|
|
|
|
res.setUserId(UserUtil.getUser().getId());
|
|
|
|
|
res.setTextMessage(rasaResult);
|
|
|
|
|
res.setVoiceMessage(replyVoiceResVO);
|
|
|
|
|
res.setType(1);
|
|
|
|
|
session.sendMessage(new TextMessage(JSONUtil.toJsonStr(res)));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// 兜底,如果走到了这里,就直接返回未识别
|
|
|
|
|
SocketMessageDTO res = new SocketMessageDTO();
|
|
|
|
|
res.setSocketId(socketMessageDTO.getSocketId());
|
|
|
|
|
res.setUserId(UserUtil.getUser().getId());
|
|
|
|
|
res.setTextMessage("医生,我们有听懂您说的什么");
|
|
|
|
|
res.setType(1);
|
|
|
|
|
session.sendMessage(new TextMessage(JSONUtil.toJsonStr(res)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@ -55,7 +99,7 @@ public class AskServiceImpl implements AskService {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public ReplyVoiceResVO replyVoice() {
|
|
|
|
|
public String replyVoice() {
|
|
|
|
|
String text = "测试:这是文字转语音的测试,测试是否OK";
|
|
|
|
|
return TtsUtil.ttsTransform(text);
|
|
|
|
|
}
|
|
|
|
|