百度智能外呼对接。

This commit is contained in:
haown 2024-09-13 11:32:35 +08:00
parent bfe64372fc
commit 2b7afe7d52
6 changed files with 84 additions and 77 deletions

View File

@ -38,6 +38,11 @@ public class ScriptInfoTaskInfo {
*/
private String taskId;
/**
* 任务Id
*/
private Long signPatientManageRouteNodeId;
/**
* 创建时间
*/

View File

@ -44,6 +44,16 @@ public class CreateTaskDto {
*/
private List<Integer> forbidDialDate;
/**
* 重试次数整数 N 3默认为空表示不重试
*/
private Integer retryTimes;
/**
* 重试间隔时间单位min0 N 720
*/
private Integer retryInterval;
/**
* 是否开启24小时空号检测
*/
@ -70,4 +80,6 @@ public class CreateTaskDto {
*/
private Integer callFinishTaskEnd = 0;
private Integer callOnePhoneTimeLimit = 0;
}

View File

@ -10,7 +10,7 @@ import com.xinelu.manage.domain.scriptinfotaskinfo.ScriptInfoTaskInfo;
*/
public interface ScriptInfoTaskInfoMapper {
String getByScriptInfoId(Long scriptInfoId);
String getByNodeId(Long signPatientManageRouteNodeId);
/**
* 新增百度外呼话术任务临时表

View File

@ -14,7 +14,6 @@ import com.xinelu.common.enums.ErrorStatusEnum;
import com.xinelu.common.enums.NodeExecuteResultStatusEnum;
import com.xinelu.common.enums.NodeExecuteStatusEnum;
import com.xinelu.common.enums.PhoneDialMethodEnum;
import com.xinelu.common.enums.PhoneRedialTimesEnum;
import com.xinelu.common.enums.TaskNodeTypeEnum;
import com.xinelu.common.exception.ServiceException;
import com.xinelu.common.utils.StringUtils;
@ -73,7 +72,6 @@ import java.util.regex.Pattern;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
@ -332,15 +330,8 @@ public class AIOBServiceImpl implements IAIOBService {
List<PhoneDialRecord> phoneDialRecordList = phoneDialRecordMapper.selectPhoneDialRecordList(phoneDialRecordQuery);
// 电话拨打未成功判断是否需要重拨电话拨打成功设置电话拨通情况为成功
if (data.getEndType() != null && data.getEndType() == 0) {//接通状态1-已接通 0-未接通
// 已经拨打的次数
int dialCount = CollectionUtils.isEmpty(phoneDialRecordList) ? 0 : phoneDialRecordList.size() + 1;
if (PhoneRedialTimesEnum.getValueByInfo(signPatientManageRouteNode.getPhoneRedialTimes()).getValue() + 1 > dialCount) {
// 需要重拨-导入名单
uploadCustomerInfoList(signPatientManageRouteNode, data.getTaskId());
} else {
// 不需要重拨设置电话拨通情况
signPatientManageRouteNode.setPhoneNodeExecuteResultStatus(NodeExecuteResultStatusEnum.FAILURE.getInfo());
}
// 电话短信提醒
if (StringUtils.equals(PhoneMessageRemindConstants.NOT_CONNECTED_SEND_MESSAGE, signPatientManageRouteNode.getPhoneMessageRemind())) {
// 发送短信
@ -373,6 +364,7 @@ public class AIOBServiceImpl implements IAIOBService {
// 发送短信
if (needSendSms) {
System.out.println("发送短信");
//sendSms(signPatientManageRoute.getPatientId(), signPatientManageRouteNode);
}
}
@ -478,20 +470,12 @@ public class AIOBServiceImpl implements IAIOBService {
* @Date 2024-9-6 14:30
*/
private void parseQuestionInfo(JSONArray records, SignPatientManageRouteNode node, Long patientInfoId, Long taskExecuteRecordId) {
// record=[{"role":"speech","timestamp":1724917698521,"contextText":"您好,我是山东省立医院的电话客服,请问您是李四本人或家属吗?", "sn":"19a09431-1ac6-43a1-8f9f-4b6f617bed73","intent":"NOINTENT","start":"00:00.033","stop":"00:06.974","timeLen":6941,"interrupted":false,"silent":false,"nodeInfo":"eyJwcm9jZXNzTmFtZSI6IuaFoueXheeUqOiNr+Wbnuiuv+ivneacryIsIm5vZGVOYW1lIjoi5byA5Zy655m96IqC54K5X3RwcnZtc2g4IiwiaW50ZW50IjoiIiwiZW50aXR5IjpbXSwic3lzdGVtRXZlbnQiOiIiLCJmYXEiOiIifQ=="},
// {"role":"voice","timestamp":1724917707542,"content":"是的","contextText":"是的","sn":"0e5c9c4e65db11ef_2_1","start":"00:06.930","stop":"00:08.330","timeLen":1400,"interrupted":false,"silent":false},
// {"role":"speech","timestamp":1724917708359,"content":"是的","contextText":"好的,您之前在我们医院住过院,想了解一下您近期的健康状况。","sn":"0e5c9c4e65db11ef_2_1","intent":"patient_self","start":"00:09.871","stop":"00:15.793","timeLen":5922,"interrupted":false,"silent":false,"nodeInfo":"eyJwcm9jZXNzTmFtZSI6IuaFoueXheeUqOiNr+Wbnuiuv+ivneacryIsIm5vZGVOYW1lIjoi5pys5Lq66IqC54K5X3Nyd2Q5MTR3IiwiaW50ZW50Ijoi5piv5pys5Lq6IiwiZW50aXR5IjpbXSwic3lzdGVtRXZlbnQiOiIiLCJmYXEiOiIifQ=="},
// {"role":"voice","timestamp":1724917718191,"content":"好的嗯","contextText":"好的嗯","sn":"0e5c9c4e65db11ef_4_1","start":"00:16.650","stop":"00:18.970","timeLen":2320,"interrupted":false,"silent":false},
// {"role":"speech","timestamp":1724917718873,"content":"好的嗯","contextText":"您是否正常按时服药呢?","sn":"0e5c9c4e65db11ef_4_1","intent":"NOINTENT","start":"00:20.385","stop":"00:22.917","timeLen":2532,"interrupted":false,"silent":false,"nodeInfo":"eyJwcm9jZXNzTmFtZSI6IuaFoueXheeUqOiNr+Wbnuiuv+ivneacryIsIm5vZGVOYW1lIjoi6IKv5a6a6IqC54K5X3F6aWpzb2g2IiwiaW50ZW50IjoiIiwiZW50aXR5IjpbXSwic3lzdGVtRXZlbnQiOiIiLCJmYXEiOiIifQ=="},
// {"role":"voice","timestamp":1724917725425,"content":"是的","contextText":"是的","sn":"0e5c9c4e65db11ef_6_1","start":"00:24.810","stop":"00:26.210","timeLen":1400,"interrupted":false,"silent":false},
// {"role":"speech","timestamp":1724917725993,"content":"是的","contextText":"祝您早日康复。","sn":"0e5c9c4e65db11ef_6_1","intent":"sys_yes","start":"00:27.505","stop":"00:29.637","timeLen":2132,"interrupted":false,"silent":false,"nodeInfo":"eyJwcm9jZXNzTmFtZSI6IuaFoueXheeUqOiNr+Wbnuiuv+ivneacryIsIm5vZGVOYW1lIjoi5oyJ5pe25pyN6I2v6IqC54K5X3Bxd2RkcDBpIiwiaW50ZW50Ijoi6IKv5a6a5oSP5Zu+IiwiZW50aXR5IjpbXSwic3lzdGVtRXZlbnQiOiIiLCJmYXEiOiIifQ=="}],
List<AiboRecord> aiboRecords = records.toList(AiboRecord.class);
List<AiboRecord> speechList = aiboRecords.stream().filter(record -> StringUtils.equals("speech", record.getRole()))
.sorted(Comparator.comparing(AiboRecord::getSn))
.collect(Collectors.toMap(AiboRecord::getContextText, p -> p, (p1, p2) -> p2))
.values()
.stream()
.sorted(Comparator.comparing(AiboRecord::getTimestamp))
.collect(Collectors.toList());
// 查询患者画像信息
@ -522,17 +506,28 @@ public class AIOBServiceImpl implements IAIOBService {
patientQuestionSubmitResult.setQuestionCount(questionInfo.getQuestionCount());
patientQuestionSubmitResult.setResidentId(patientInfo.getResidentId());
questionSubmitResultMapper.insertPatientQuestionSubmitResult(patientQuestionSubmitResult);
// 2保存patient_question_subject_result,计算总得分
// 2保存patient_question_subject_result
BigDecimal totalScore = BigDecimal.ZERO;
List<QuestionSubjectVO> questionSubjectList = questionSubjectMapper.selectQuestionSubjectBy(node.getQuestionInfoId());
questionSubjectList.forEach(questionSubjectVO -> {
// 判断问题中有无变量有变量替换变量
String questionName = replaceQuestionName(questionSubjectVO.getQuestionName(), labelFieldContentList);
// 根据通话记录筛查问题
if (contextMap.containsKey(questionName)) {
// 根据通话记录筛查是否询问该问题
boolean hasQuestion = false;
String aiobQuestionName = "";
for (String questionContent : contextMap.keySet()) {
if (questionContent.replaceAll("\\p{P}", "").contains(questionName.replaceAll("\\p{P}", ""))) {
hasQuestion = true;
aiobQuestionName = questionContent;
break;
}
}
if (hasQuestion) {
PatientQuestionSubjectResult patientQuestionSubjectResult = new PatientQuestionSubjectResult();
BeanUtils.copyProperties(questionSubjectVO, patientQuestionSubjectResult);
patientQuestionSubjectResult.setId(null);
patientQuestionSubjectResult.setQuestionSubmitResultId(patientQuestionSubmitResult.getId());
patientQuestionSubjectResult.setCreateTime(LocalDateTime.now());
patientQuestionSubjectResultMapper.insertPatientQuestionSubjectResult(patientQuestionSubjectResult);
// 3保存patient_question_option_result
@ -540,29 +535,20 @@ public class AIOBServiceImpl implements IAIOBService {
questionSubjectOptionQuery.setQuestionnaireSubjectId(questionSubjectVO.getId());
List<QuestionSubjectOption> questionSubjectOptions = questionSubjectOptionMapper.selectQuestionSubjectOptionList(questionSubjectOptionQuery);
List<PatientQuestionOptionResult> patientQuestionOptionResults = new ArrayList<>();
String finalAiobQuestionName = aiobQuestionName;
questionSubjectOptions.forEach(questionSubjectOption -> {
PatientQuestionOptionResult patientQuestionOptionResult = new PatientQuestionOptionResult();
BeanUtils.copyProperties(questionSubjectOptions, patientQuestionOptionResult);
BeanUtils.copyProperties(questionSubjectOption, patientQuestionOptionResult);
// 设置是否选中, 通过通话记录中下一个问题的intent字段查询QuestionSubjectOption
// speech_context_text中的字段需要替换为文本竖线分隔的分段都要匹配 input.replaceAll("[\\p{Punct}]", "")
//{% &department_name %} /.*{% & %}.*/ #\{([^\}]*)\}
//Pattern pattern = Pattern.compile("\\{%\\s*&[\\s\\S]*?\\s*%}");
//Matcher matcher = pattern.matcher(questionSubjectOption.getSpeechContextText());
//while (matcher.find()) {
// LabelFieldInfoContentVo labelFieldContent = labelFieldContentList.stream().filter(s -> Objects.equals(s.getFieldCode(), matcher.group(1).replaceAll("_", "").toUpperCase())).findFirst().orElse(null);
// if (ObjectUtils.isNotEmpty(labelFieldContent)) {
// String speechContextText = questionSubjectOption.getSpeechContextText().replaceAll("\\{%\\s*&[\\s\\S]*?\\s*%}", ObjectUtils.isEmpty(labelFieldContent) ? "" : labelFieldContent.getFieldValue())
// .replaceAll("[\\p{Punct}]", "")
// .replaceAll("|", "");
// }
//}
List<String> intents = Arrays.asList(questionSubjectOption.getIntent().split("|"));
AiboRecord nextRecord = getNextQuestion(speechList, questionName);
List<String> intents = Arrays.asList(questionSubjectOption.getIntent().split("\\|"));
AiboRecord nextRecord = getNextQuestion(speechList, finalAiobQuestionName);
if (ObjectUtils.isNotEmpty(nextRecord) && intents.contains(nextRecord.getIntent())) {
patientQuestionOptionResult.setOptionChooseSign(0);
} else {
patientQuestionOptionResult.setOptionChooseSign(1);
}
patientQuestionOptionResult.setId(null);
patientQuestionOptionResult.setCreateTime(LocalDateTime.now());
patientQuestionOptionResult.setQuestionSubjectResultId(patientQuestionSubjectResult.getId());
patientQuestionOptionResults.add(patientQuestionOptionResult);
});
@ -577,11 +563,13 @@ public class AIOBServiceImpl implements IAIOBService {
}
private AiboRecord getNextQuestion(List<AiboRecord> speechList, String currentQuestion) {
if (StringUtils.isNotBlank(currentQuestion)) {
for (int i = 0; i < speechList.size(); i++) {
if (StringUtils.equals(speechList.get(i).getContextText(), currentQuestion)) {
return speechList.get(i+1);
}
}
}
return null;
}
@ -596,4 +584,5 @@ public class AIOBServiceImpl implements IAIOBService {
}
return questionName;
}
}

View File

@ -21,10 +21,10 @@
from script_info_task_info
</sql>
<select id="getByScriptInfoId" resultType="java.lang.String">
<select id="getByNodeId" resultType="java.lang.String">
select task_id
from script_info_task_info
where script_info_id = #{scriptInfoId} and del_flag = 0
where sign_patient_manage_route_node_id = #{signPatientManageRouteNodeId} and del_flag = 0
</select>
<insert id="insertScriptInfoTaskInfo" parameterType="ScriptInfoTaskInfo" useGeneratedKeys="true"
@ -37,6 +37,8 @@
</if>
<if test="taskId != null">task_id,
</if>
<if test="signPatientManageRouteNodeId != null">sign_patient_manage_route_node_id,
</if>
<if test="createTime != null">create_time,
</if>
</trim>
@ -47,6 +49,8 @@
</if>
<if test="taskId != null">#{taskId},
</if>
<if test="signPatientManageRouteNodeId != null">#{signPatientManageRouteNodeId},
</if>
<if test="createTime != null">#{createTime},
</if>
</trim>

View File

@ -3,6 +3,7 @@ package com.xinelu.quartz.task;
import com.alibaba.fastjson2.JSONObject;
import com.xinelu.common.enums.NodeExecuteStatusEnum;
import com.xinelu.common.enums.PhoneDialMethodEnum;
import com.xinelu.common.enums.PhoneRedialTimesEnum;
import com.xinelu.common.utils.StringUtils;
import com.xinelu.manage.domain.patientinfo.PatientInfo;
import com.xinelu.manage.domain.patientvisitrecord.PatientVisitRecord;
@ -33,9 +34,7 @@ import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
@ -95,28 +94,28 @@ public class UploadRobotPublishTask {
SignPatientRecord signPatientRecord = signPatientRecordMapper.selectByPrimaryKey(signPatientManageRoute.getSignPatientRecordId());
PatientVisitRecord patientVisitRecord = patientVisitRecordMapper.selectPatientVisitRecordById(signPatientRecord.getPatientVisitRecordId());
LocalDateTime executeTime = signPatientManageRouteNodeService.getExecuteTime(node.getExecuteTime(), node.getRouteNodeName(), node.getRouteNodeDay(), patientVisitRecord);
if (executeTime.toLocalDate().isBefore(LocalDate.now())|| executeTime.toLocalDate().isEqual(LocalDate.now())) {
if (node.getScriptInfoId() != null && (executeTime.toLocalDate().isBefore(LocalDate.now()) || executeTime.toLocalDate().isEqual(LocalDate.now()))) {
executeNodeList.add(node);
}
});
if (CollectionUtils.isNotEmpty(executeNodeList)) {
// 按照话术id分组每个话术id对应不同的机器人id, 每个机器人每天创建一条任务
Map<Long, List<SignPatientManageRouteNode>> groupByScriptInfo = executeNodeList.stream().filter(node -> node.getScriptInfoId() != null).collect(Collectors.groupingBy(SignPatientManageRouteNode::getScriptInfoId));
// 根据机器人id查询智能外呼系统的任务id
for (Long scriptInfoId : groupByScriptInfo.keySet()) {
ScriptInfo scriptInfo = scriptInfoMapper.selectScriptInfoById(scriptInfoId);
String taskId = scriptInfoTaskInfoMapper.getByScriptInfoId(scriptInfoId);
executeNodeList.forEach(node -> {
ScriptInfo scriptInfo = scriptInfoMapper.selectScriptInfoById(node.getScriptInfoId());
String taskId = scriptInfoTaskInfoMapper.getByNodeId(node.getId());
if (StringUtils.isBlank(taskId)) {
// 没有任务则创建任务
if (ObjectUtils.isNotEmpty(scriptInfo)) {
log.info("创建任务......");
CreateTaskDto createTaskDto = new CreateTaskDto();
createTaskDto.setTaskName(LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")) + scriptInfo.getScriptName());
createTaskDto.setTaskName(LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")) + scriptInfo.getScriptName() + "(" +node.getId()+ ")");
createTaskDto.setRobotId(scriptInfo.getRobotPublishId());
createTaskDto.setDialStartDate(LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
createTaskDto.setDialEndDate(LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
createTaskDto.setForbidDialDate(Arrays.asList(99));
createTaskDto.setRetryTimes(StringUtils.isBlank(node.getPhoneRedialTimes()) ? 0 : PhoneRedialTimesEnum.getValueByInfo(node.getPhoneRedialTimes()).getValue());
createTaskDto.setRetryInterval(node.getPhoneTimeInterval());
createTaskDto.setNumTypeFilterList(Arrays.asList(1,2));
createTaskDto.setTaskDataCallback(true);
createTaskDto.setCallBackUrl(callBackUrl);
@ -126,17 +125,17 @@ public class UploadRobotPublishTask {
aiobService.updateTaskStatus(taskId, 2);
ScriptInfoTaskInfo scriptInfoTaskInfo = new ScriptInfoTaskInfo();
scriptInfoTaskInfo.setScriptInfoId(scriptInfoId);
scriptInfoTaskInfo.setScriptInfoId(node.getScriptInfoId());
scriptInfoTaskInfo.setTaskId(taskId);
scriptInfoTaskInfo.setRobotId(scriptInfo.getRobotPublishId());
scriptInfoTaskInfo.setCreateTime(LocalDateTime.now());
scriptInfoTaskInfo.setSignPatientManageRouteNodeId(node.getId());
scriptInfoTaskInfoMapper.insertScriptInfoTaskInfo(scriptInfoTaskInfo);
}
}
// 客户名单list
List<SignPatientManageRouteNode> scriptNodeList = groupByScriptInfo.get(scriptInfoId);
List<CustomerInfoDto> customerInfoList = new ArrayList<>();
scriptNodeList.forEach(node -> {
CustomerInfoDto customerInfoDto = new CustomerInfoDto();
customerInfoDto.setExtJson(node.getId() + "");
SignPatientManageRoute signPatientManageRoute = signPatientManageRouteMapper.selectSignPatientManageRouteById(node.getManageRouteId());
@ -156,7 +155,6 @@ public class UploadRobotPublishTask {
customerInfoDto.setVar(jsonObject);
customerInfoList.add(customerInfoDto);
});
// 上传名单
log.info("任务导入客户名单......");
@ -169,13 +167,12 @@ public class UploadRobotPublishTask {
importTaskList.forEach(importTaskVo -> {
// 名单导入成功后sign_patient_manage_route_node表中设置taskId
if (importTaskVo.getStatus()) {
SignPatientManageRouteNode node = new SignPatientManageRouteNode();
node.setTaskIdExt(finalTaskId);
node.setId(Long.valueOf(importTaskVo.getExtJson()));
signPatientManageRouteNodeMapper.updateSignPatientManageRouteNode(node);
}
});
}
});
}
log.info("百度智能外呼创建任务定时任务执行完成......");
}