飞书机器人提醒 动态消息模板-可读取列值进行替换
continuous-integration/drone/push Build is passing Details

于相涌/robot_optimize
YXY 2 years ago
parent f0e6ec42ec
commit 1f461c7702

@ -9,7 +9,8 @@ public enum TemplateSubTypeEnum {
/** /**
* *
*/ */
TEXT("text", "文本文档"); TEXT("text", "文本文档"),
INTERACTIVE("interactive", "消息卡片");
private final String code; private final String code;
private final String info; private final String info;

@ -1,26 +1,21 @@
package com.ruoyi.quartz.task; package com.ruoyi.quartz.task;
import cn.hutool.extra.template.Template;
import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.google.gson.internal.LinkedTreeMap; import com.google.gson.internal.LinkedTreeMap;
import com.lark.oapi.service.bitable.v1.model.AppTableRecord; import com.lark.oapi.service.bitable.v1.model.AppTableRecord;
import com.lark.oapi.service.bitable.v1.model.ListAppTableRecordRespBody; import com.lark.oapi.service.bitable.v1.model.ListAppTableRecordRespBody;
import com.lark.oapi.service.contact.v3.model.GetUserRespBody;
import com.ruoyi.common.enums.*; import com.ruoyi.common.enums.*;
import com.ruoyi.flyingbook.LarkHelper.LarkRobotHelper; import com.ruoyi.flyingbook.LarkHelper.LarkRobotHelper;
import com.ruoyi.flyingbook.LarkHelper.LarkTableHelper; import com.ruoyi.flyingbook.LarkHelper.LarkTableHelper;
import com.ruoyi.flyingbook.LarkHelper.LarkUserHelper;
import com.ruoyi.flyingbook.LarkHelper.script.LarkTableFilterScriptHelper; import com.ruoyi.flyingbook.LarkHelper.script.LarkTableFilterScriptHelper;
import com.ruoyi.flyingbook.domain.EventLog; import com.ruoyi.flyingbook.domain.*;
import com.ruoyi.flyingbook.domain.LarkCompanyTableInfo;
import com.ruoyi.flyingbook.domain.LarkTableConfiguration;
import com.ruoyi.flyingbook.domain.LarkTemplate;
import com.ruoyi.flyingbook.domain.lark.LarkRobotRequest; import com.ruoyi.flyingbook.domain.lark.LarkRobotRequest;
import com.ruoyi.flyingbook.domain.lark.LarkTableRequest; import com.ruoyi.flyingbook.domain.lark.LarkTableRequest;
import com.ruoyi.flyingbook.mapper.EventLogMapper; import com.ruoyi.flyingbook.domain.lark.LarkUserRequest;
import com.ruoyi.flyingbook.mapper.LarkTableConfigurationMapper; import com.ruoyi.flyingbook.mapper.*;
import com.ruoyi.flyingbook.mapper.LarkTableRelationMapper;
import com.ruoyi.flyingbook.mapper.LarkTemplateMapper;
import com.ruoyi.quartz.helper.OkHttpHelper;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
@ -46,12 +41,16 @@ import java.util.stream.Collectors;
@Component("scheduledRemindersTask") @Component("scheduledRemindersTask")
public class ScheduledRemindersTask { public class ScheduledRemindersTask {
@Autowired
protected LarkUserHelper larkUserHelper;
@Autowired @Autowired
private LarkTableRelationMapper larkTableRelationMapper; private LarkTableRelationMapper larkTableRelationMapper;
@Autowired @Autowired
private LarkTableHelper larkTableHelper; private LarkTableHelper larkTableHelper;
@Autowired @Autowired
private LarkTableRowRelationMapper larkTableRowRelationMapper;
@Autowired
private LarkTemplateMapper larkTemplateMapper; private LarkTemplateMapper larkTemplateMapper;
@Autowired @Autowired
private LarkTableConfigurationMapper larkTableConfigurationMapper; private LarkTableConfigurationMapper larkTableConfigurationMapper;
@ -78,7 +77,7 @@ public class ScheduledRemindersTask {
} }
List<EventLog> logList = new ArrayList<>(); List<EventLog> logList = new ArrayList<>();
LocalDateTime now = LocalDateTime.now(); LocalDateTime now = LocalDateTime.now();
// LocalDateTime now = LocalDateTime.of(2023, 4, 23, 20, 0); // LocalDateTime now = LocalDateTime.of(2023, 5, 4, 20, 0);
for (LarkCompanyTableInfo larkCompanyTableInfo : larkList) { for (LarkCompanyTableInfo larkCompanyTableInfo : larkList) {
try { try {
List<LarkTableConfiguration> larkConfiguration = this.getLarkConfiguration(larkCompanyTableInfo); List<LarkTableConfiguration> larkConfiguration = this.getLarkConfiguration(larkCompanyTableInfo);
@ -120,7 +119,11 @@ public class ScheduledRemindersTask {
} else { } else {
pageToken = respBody.getPageToken(); pageToken = respBody.getPageToken();
} }
for (AppTableRecord item : respBody.getItems()) { AppTableRecord[] items = respBody.getItems();
if (items == null || items.length == 0){
return;
}
for (AppTableRecord item : items) {
try { try {
Map<String, Object> fields = item.getFields(); Map<String, Object> fields = item.getFields();
LocalDateTime localDateTime = changeLocalDateTime((Double) fields.get(RANGE_KEY)); LocalDateTime localDateTime = changeLocalDateTime((Double) fields.get(RANGE_KEY));
@ -181,14 +184,11 @@ public class ScheduledRemindersTask {
throw new RuntimeException(String.format("当前tableId:%s 模板类型%s对应策略不存在", larkCompanyTableInfo.getId(), template.getSubTemplateType())); throw new RuntimeException(String.format("当前tableId:%s 模板类型%s对应策略不存在", larkCompanyTableInfo.getId(), template.getSubTemplateType()));
} }
LarkRobotRequest robotRequest = new LarkRobotRequest(larkCompanyTableInfo.getAppId(), larkCompanyTableInfo.getAppSecret()); LarkRobotRequest robotRequest = new LarkRobotRequest(larkCompanyTableInfo.getAppId(), larkCompanyTableInfo.getAppSecret());
switch (templateSubTypeEnum) {
case TEXT:
robotRequest.setMsgType(templateSubTypeEnum.getCode());
String templateContent = template.getTemplateContent(); String templateContent = template.getTemplateContent();
templateContent = templateContent.replaceAll("#name", String.valueOf(fields.get("学生姓名"))); robotRequest.setMsgType(templateSubTypeEnum.getCode());
JSONObject body = new JSONObject(); JSONObject record = new JSONObject(fields);
body.put("text",templateContent); String body = this.buildTemplate(templateContent, record, larkCompanyTableInfo,larkCompanyTableInfo.getId());
robotRequest.setContent(body.toJSONString()); robotRequest.setContent(body);
List<LinkedTreeMap<String, String>> people = (List<LinkedTreeMap<String, String>>) fields.get("业务员"); List<LinkedTreeMap<String, String>> people = (List<LinkedTreeMap<String, String>>) fields.get("业务员");
for (int i = 0; i < people.size(); i++) { for (int i = 0; i < people.size(); i++) {
LinkedTreeMap<String, String> detail = people.get(i); LinkedTreeMap<String, String> detail = people.get(i);
@ -196,11 +196,88 @@ public class ScheduledRemindersTask {
msg = robotRequest; msg = robotRequest;
larkRobotHelper.sendUserMessage(robotRequest); larkRobotHelper.sendUserMessage(robotRequest);
} }
break; return msg;
default: }
private String buildTemplate(String template, JSONObject record, LarkCompanyTableInfo request,Long tableRelationId) {
List<LarkTableRowRelation> rowRelationList = larkTableRowRelationMapper.queryListByTableRelationIdList(Arrays.asList(tableRelationId));
if (CollectionUtils.isEmpty(rowRelationList)){
return template;
}
for (LarkTableRowRelation entry : rowRelationList) {
String msg = recursiveValue(record, entry.getFromId(), request);
template = template.replaceAll(entry.getToId(), msg);
}
return template;
}
/**
*
* #
* ##
*
* @param record
* @param key
* @return
*/
private String recursiveValue(JSONObject record, String key, LarkCompanyTableInfo request) {
if (record == null) {
return null;
}
String[] split = key.split(",");
int index = 0;
for (index = 0; index < split.length; index++) {
String k = split[index].trim();
if (k.startsWith("##") && k.endsWith("${user}")) {
k = k.replace("##", "").replace("${user}", "");
List<String> list = new ArrayList<>();
JSONArray jsonArray = record.getJSONArray(k);
for (int i = 0; i < jsonArray.size(); i++) {
String openId = jsonArray.getString(i);
GetUserRespBody userInfoDetail = larkUserHelper.getUserInfoDetail(new LarkUserRequest(request.getAppId(), request.getAppSecret(), openId));
list.add(userInfoDetail.getUser().getName());
}
return String.join(",", list);
} else if (k.startsWith("##") && k.endsWith("${checkbox}")) {
k = k.replace("##", "").replace("${checkbox}", "");
List<String> list = new ArrayList<>();
JSONArray jsonArray = record.getJSONArray(k);
for (int i = 0; i < jsonArray.size(); i++) {
String param = jsonArray.getString(i);
list.add(param);
}
return String.join(",", list);
} else if (k.startsWith("##")) {
k = k.replace("##", "");
List<String> list = new ArrayList<>();
JSONArray jsonArray = record.getJSONArray(k);
for (int i = 0; i < jsonArray.size(); i++) {
Object object = jsonArray.get(i);
if (object instanceof String) {
list.add(String.valueOf(object));
} else {
List<String> keyList = new ArrayList<>();
for (int j = index + 1; j < split.length; j++) {
keyList.add(split[j]);
}
Object o = recursiveValue(jsonArray.getJSONObject(i), String.join(",", keyList), request);
list.add(String.valueOf(o));
}
}
return String.join(",", list);
} else if (k.startsWith("#")) {
k = k.replace("#", "");
record = record.getJSONObject(k);
} else if (k.endsWith("${time}")) {
String time = record.getString(k.replace("${time}", "").trim());
if (time.contains("T")) {
return time.replace("T", " ").substring(0, time.indexOf("+"));
}
} else {
break; break;
} }
return msg; }
return record.getString(split[index].trim());
} }
private List<LarkCompanyTableInfo> getLarkList() { private List<LarkCompanyTableInfo> getLarkList() {

Loading…
Cancel
Save