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

于相涌/robot_optimize
YXY 1 year 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 info;

@ -1,26 +1,21 @@
package com.ruoyi.quartz.task;
import cn.hutool.extra.template.Template;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.internal.LinkedTreeMap;
import com.lark.oapi.service.bitable.v1.model.AppTableRecord;
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.flyingbook.LarkHelper.LarkRobotHelper;
import com.ruoyi.flyingbook.LarkHelper.LarkTableHelper;
import com.ruoyi.flyingbook.LarkHelper.LarkUserHelper;
import com.ruoyi.flyingbook.LarkHelper.script.LarkTableFilterScriptHelper;
import com.ruoyi.flyingbook.domain.EventLog;
import com.ruoyi.flyingbook.domain.LarkCompanyTableInfo;
import com.ruoyi.flyingbook.domain.LarkTableConfiguration;
import com.ruoyi.flyingbook.domain.LarkTemplate;
import com.ruoyi.flyingbook.domain.*;
import com.ruoyi.flyingbook.domain.lark.LarkRobotRequest;
import com.ruoyi.flyingbook.domain.lark.LarkTableRequest;
import com.ruoyi.flyingbook.mapper.EventLogMapper;
import com.ruoyi.flyingbook.mapper.LarkTableConfigurationMapper;
import com.ruoyi.flyingbook.mapper.LarkTableRelationMapper;
import com.ruoyi.flyingbook.mapper.LarkTemplateMapper;
import com.ruoyi.quartz.helper.OkHttpHelper;
import com.ruoyi.flyingbook.domain.lark.LarkUserRequest;
import com.ruoyi.flyingbook.mapper.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
@ -46,12 +41,16 @@ import java.util.stream.Collectors;
@Component("scheduledRemindersTask")
public class ScheduledRemindersTask {
@Autowired
protected LarkUserHelper larkUserHelper;
@Autowired
private LarkTableRelationMapper larkTableRelationMapper;
@Autowired
private LarkTableHelper larkTableHelper;
@Autowired
private LarkTableRowRelationMapper larkTableRowRelationMapper;
@Autowired
private LarkTemplateMapper larkTemplateMapper;
@Autowired
private LarkTableConfigurationMapper larkTableConfigurationMapper;
@ -78,7 +77,7 @@ public class ScheduledRemindersTask {
}
List<EventLog> logList = new ArrayList<>();
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) {
try {
List<LarkTableConfiguration> larkConfiguration = this.getLarkConfiguration(larkCompanyTableInfo);
@ -89,12 +88,12 @@ public class ScheduledRemindersTask {
}
if (ConfigurationSubTypeEnum.REMINDER_AROUND.equals(subType)) {
String script = LarkTableFilterScriptHelper.buildTimeRange(larkTableConfiguration, ConfigurationSubTypeEnum.REMINDER_BEFORE, RANGE_KEY, now);
this.sendMessage(script, larkCompanyTableInfo, logList,larkTableConfiguration,now);
this.sendMessage(script, larkCompanyTableInfo, logList, larkTableConfiguration, now);
script = LarkTableFilterScriptHelper.buildTimeRange(larkTableConfiguration, ConfigurationSubTypeEnum.REMINDER_AFTER, RANGE_KEY, now);
this.sendMessage(script, larkCompanyTableInfo, logList,larkTableConfiguration,now);
this.sendMessage(script, larkCompanyTableInfo, logList, larkTableConfiguration, now);
} else {
String script = LarkTableFilterScriptHelper.buildTimeRange(larkTableConfiguration, subType, RANGE_KEY, now);
this.sendMessage(script, larkCompanyTableInfo, logList,larkTableConfiguration,now);
this.sendMessage(script, larkCompanyTableInfo, logList, larkTableConfiguration, now);
}
}
} catch (Exception e) {
@ -108,23 +107,27 @@ public class ScheduledRemindersTask {
log.info("scheduledRemindersTask end");
}
private void sendMessage(String script, LarkCompanyTableInfo larkCompanyTableInfo, List<EventLog> logList,LarkTableConfiguration larkTableConfiguration,LocalDateTime now) {
private void sendMessage(String script, LarkCompanyTableInfo larkCompanyTableInfo, List<EventLog> logList, LarkTableConfiguration larkTableConfiguration, LocalDateTime now) {
String pageToken = null;
LarkTableRequest request = this.buildRequest(larkCompanyTableInfo, script);
LarkTemplate template = this.getTemplate(larkCompanyTableInfo,larkTableConfiguration.getId());
LarkTemplate template = this.getTemplate(larkCompanyTableInfo, larkTableConfiguration.getId());
do {
Object msg = new JSONObject();
ListAppTableRecordRespBody respBody = larkTableHelper.listTableRecord(request);
if (respBody.getPageToken().equals(pageToken)){
if (respBody.getPageToken().equals(pageToken)) {
return;
}else {
} else {
pageToken = respBody.getPageToken();
}
for (AppTableRecord item : respBody.getItems()) {
AppTableRecord[] items = respBody.getItems();
if (items == null || items.length == 0){
return;
}
for (AppTableRecord item : items) {
try {
Map<String, Object> fields = item.getFields();
LocalDateTime localDateTime = changeLocalDateTime((Double) fields.get(RANGE_KEY));
if ((localDateTime.getHour() == now.getHour() )&& localDateTime.getMinute() == now.getMinute()){
if ((localDateTime.getHour() == now.getHour()) && localDateTime.getMinute() == now.getMinute()) {
continue;
}
msg = this.buildMsg(larkCompanyTableInfo, fields, template);
@ -138,7 +141,7 @@ public class ScheduledRemindersTask {
} while (StringUtils.isNotBlank(pageToken));
}
private LocalDateTime changeLocalDateTime(Double timestamp){
private LocalDateTime changeLocalDateTime(Double timestamp) {
BigDecimal bigDecimal = new BigDecimal(timestamp.toString());
long time = bigDecimal.longValue();
Instant instant = Instant.ofEpochMilli(time);
@ -146,7 +149,7 @@ public class ScheduledRemindersTask {
return LocalDateTime.ofInstant(instant, zone);
}
private LarkTemplate getTemplate(LarkCompanyTableInfo larkCompanyTableInfo,Long configurationId) {
private LarkTemplate getTemplate(LarkCompanyTableInfo larkCompanyTableInfo, Long configurationId) {
LarkTemplate larkTemplate = new LarkTemplate();
larkTemplate.setTemplateType(TemplateTypeEnum.SCHEDULED_REMINDER.getCode());
larkTemplate.setFlag(FlagStatus.OK.getCode());
@ -180,27 +183,101 @@ public class ScheduledRemindersTask {
if (templateSubTypeEnum == null) {
throw new RuntimeException(String.format("当前tableId:%s 模板类型%s对应策略不存在", larkCompanyTableInfo.getId(), template.getSubTemplateType()));
}
LarkRobotRequest robotRequest = new LarkRobotRequest(larkCompanyTableInfo.getAppId(),larkCompanyTableInfo.getAppSecret());
switch (templateSubTypeEnum) {
case TEXT:
robotRequest.setMsgType(templateSubTypeEnum.getCode());
LarkRobotRequest robotRequest = new LarkRobotRequest(larkCompanyTableInfo.getAppId(), larkCompanyTableInfo.getAppSecret());
String templateContent = template.getTemplateContent();
templateContent = templateContent.replaceAll("#name", String.valueOf(fields.get("学生姓名")));
JSONObject body = new JSONObject();
body.put("text",templateContent);
robotRequest.setContent(body.toJSONString());
List<LinkedTreeMap<String,String>> people = (List<LinkedTreeMap<String,String>>)fields.get("业务员");
robotRequest.setMsgType(templateSubTypeEnum.getCode());
JSONObject record = new JSONObject(fields);
String body = this.buildTemplate(templateContent, record, larkCompanyTableInfo,larkCompanyTableInfo.getId());
robotRequest.setContent(body);
List<LinkedTreeMap<String, String>> people = (List<LinkedTreeMap<String, String>>) fields.get("业务员");
for (int i = 0; i < people.size(); i++) {
LinkedTreeMap<String,String> detail = people.get(i);
LinkedTreeMap<String, String> detail = people.get(i);
robotRequest.setReceiveId(detail.get("id"));
msg = robotRequest;
larkRobotHelper.sendUserMessage(robotRequest);
}
break;
default:
return msg;
}
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;
}
return msg;
}
return record.getString(split[index].trim());
}
private List<LarkCompanyTableInfo> getLarkList() {
@ -226,12 +303,12 @@ public class ScheduledRemindersTask {
public static void main(String[] args) {
String str = "[{avatar_url:https://s1-imfile.feishucdn.com/static-resource/v1/v2_5462d76b-2c9c-405f-8753-149c36bf097g~?image_size:72x72&cut_type:default-face&quality:&format:jpeg&sticker_format:.webp, email:, en_name:于相涌, id:ou_cd09481727e91fce1f12b7b0ed1500d2, name:于相涌}]";
str = str.replace("https:","https")
.replaceAll("\\{","{\"")
.replaceAll(":","\":\"")
.replaceAll(",","\",\"")
.replaceAll("}","}")
.replace("https","https:");
str = str.replace("https:", "https")
.replaceAll("\\{", "{\"")
.replaceAll(":", "\":\"")
.replaceAll(",", "\",\"")
.replaceAll("}", "}")
.replace("https", "https:");
JSONArray objects = JSONArray.parseArray(str);
int i = 0;
}

Loading…
Cancel
Save