飞书机器人提醒 动态消息模板-可读取列值进行替换

于相涌/robot_optimize
YXY 2 years ago
parent b5e158fedd
commit 1cea2d94ce

@ -0,0 +1,20 @@
package com.ruoyi.common.constant;
/**
* @author yuxiangyong
* @create 2023-05-06 23:26
*/
public class EnumsConstants {
/**
*
* @see com.ruoyi.common.enums.ConfigurationSubTypeEnum
*/
public static final String SCHEDULE_REMINDER_TIME = "time";
public static final String SCHEDULE_REMINDER_FORMULA = "formula";
/**
*
*/
public static final String SCHEDULE_REMINDER_ROW_RELATION_TYPE = "time";
}

@ -3,7 +3,10 @@ package com.ruoyi.common.utils;
import java.lang.management.ManagementFactory; import java.lang.management.ManagementFactory;
import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date; import java.util.Date;
import org.apache.commons.lang3.time.DateFormatUtils; import org.apache.commons.lang3.time.DateFormatUtils;
/** /**
@ -11,8 +14,7 @@ import org.apache.commons.lang3.time.DateFormatUtils;
* *
* @author ruoyi * @author ruoyi
*/ */
public class DateUtils extends org.apache.commons.lang3.time.DateUtils public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
{
public static String YYYY = "yyyy"; public static String YYYY = "yyyy";
public static String YYYY_MM = "yyyy-MM"; public static String YYYY_MM = "yyyy-MM";
@ -22,6 +24,7 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss"; public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss"; public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
public static String YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm";
private static String[] parsePatterns = { private static String[] parsePatterns = {
"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM", "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
@ -33,8 +36,7 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
* *
* @return Date() * @return Date()
*/ */
public static Date getNowDate() public static Date getNowDate() {
{
return new Date(); return new Date();
} }
@ -43,44 +45,34 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
* *
* @return String * @return String
*/ */
public static String getDate() public static String getDate() {
{
return dateTimeNow(YYYY_MM_DD); return dateTimeNow(YYYY_MM_DD);
} }
public static final String getTime() public static final String getTime() {
{
return dateTimeNow(YYYY_MM_DD_HH_MM_SS); return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
} }
public static final String dateTimeNow() public static final String dateTimeNow() {
{
return dateTimeNow(YYYYMMDDHHMMSS); return dateTimeNow(YYYYMMDDHHMMSS);
} }
public static final String dateTimeNow(final String format) public static final String dateTimeNow(final String format) {
{
return parseDateToStr(format, new Date()); return parseDateToStr(format, new Date());
} }
public static final String dateTime(final Date date) public static final String dateTime(final Date date) {
{
return parseDateToStr(YYYY_MM_DD, date); return parseDateToStr(YYYY_MM_DD, date);
} }
public static final String parseDateToStr(final String format, final Date date) public static final String parseDateToStr(final String format, final Date date) {
{
return new SimpleDateFormat(format).format(date); return new SimpleDateFormat(format).format(date);
} }
public static final Date dateTime(final String format, final String ts) public static final Date dateTime(final String format, final String ts) {
{ try {
try
{
return new SimpleDateFormat(format).parse(ts); return new SimpleDateFormat(format).parse(ts);
} } catch (ParseException e) {
catch (ParseException e)
{
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
@ -88,8 +80,7 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
/** /**
* // 2018/08/08 * // 2018/08/08
*/ */
public static final String datePath() public static final String datePath() {
{
Date now = new Date(); Date now = new Date();
return DateFormatUtils.format(now, "yyyy/MM/dd"); return DateFormatUtils.format(now, "yyyy/MM/dd");
} }
@ -97,8 +88,7 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
/** /**
* // 20180808 * // 20180808
*/ */
public static final String dateTime() public static final String dateTime() {
{
Date now = new Date(); Date now = new Date();
return DateFormatUtils.format(now, "yyyyMMdd"); return DateFormatUtils.format(now, "yyyyMMdd");
} }
@ -106,18 +96,13 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
/** /**
* *
*/ */
public static Date parseDate(Object str) public static Date parseDate(Object str) {
{ if (str == null) {
if (str == null)
{
return null; return null;
} }
try try {
{
return parseDate(str.toString(), parsePatterns); return parseDate(str.toString(), parsePatterns);
} } catch (ParseException e) {
catch (ParseException e)
{
return null; return null;
} }
} }
@ -125,8 +110,7 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
/** /**
* *
*/ */
public static Date getServerStartDate() public static Date getServerStartDate() {
{
long time = ManagementFactory.getRuntimeMXBean().getStartTime(); long time = ManagementFactory.getRuntimeMXBean().getStartTime();
return new Date(time); return new Date(time);
} }
@ -134,8 +118,7 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
/** /**
* *
*/ */
public static String getDatePoor(Date endDate, Date nowDate) public static String getDatePoor(Date endDate, Date nowDate) {
{
long nd = 1000 * 24 * 60 * 60; long nd = 1000 * 24 * 60 * 60;
long nh = 1000 * 60 * 60; long nh = 1000 * 60 * 60;
long nm = 1000 * 60; long nm = 1000 * 60;
@ -152,4 +135,14 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
// long sec = diff % nd % nh % nm / ns; // long sec = diff % nd % nh % nm / ns;
return day + "天" + hour + "小时" + min + "分钟"; return day + "天" + hour + "小时" + min + "分钟";
} }
public static String ldt2str(LocalDateTime time, String pattern) {
DateTimeFormatter fmt = DateTimeFormatter.ofPattern(pattern);
return time.format(fmt);
}
public static LocalDateTime str2ldt(String time, String pattern) {
DateTimeFormatter fmt = DateTimeFormatter.ofPattern(pattern);
return LocalDateTime.parse(time,fmt);
}
} }

@ -1,7 +1,9 @@
package com.ruoyi.flyingbook.LarkHelper.script; package com.ruoyi.flyingbook.LarkHelper.script;
import com.ruoyi.common.constant.EnumsConstants;
import com.ruoyi.common.enums.ConfigurationSubTypeEnum; import com.ruoyi.common.enums.ConfigurationSubTypeEnum;
import com.ruoyi.common.enums.TimeUnitEnum; import com.ruoyi.common.enums.TimeUnitEnum;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.flyingbook.domain.LarkTableConfiguration; import com.ruoyi.flyingbook.domain.LarkTableConfiguration;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
@ -30,13 +32,21 @@ public class LarkTableFilterScriptHelper {
default: default:
break; break;
} }
String range = fromTime.getMinute() >= 30 ? ">=" : "<";
StringBuilder sb = new StringBuilder("AND("); StringBuilder sb = new StringBuilder("AND(");
sb.append(String.format("YEAR(CurrentValue.[%s]) = %s",paramCode,fromTime.getYear())).append(","); String param3 = larkTableConfiguration.getParam3();
sb.append(String.format("MONTH(CurrentValue.[%s]) = %s",paramCode,fromTime.getMonth().getValue())).append(","); if (param3 == null || EnumsConstants.SCHEDULE_REMINDER_TIME.equals(param3)){
sb.append(String.format("DAY(CurrentValue.[%s]) = %s",paramCode,fromTime.getDayOfMonth())).append(","); //按时间格式查询
sb.append(String.format("HOUR(CurrentValue.[%s]) = %s",paramCode,fromTime.getHour())).append(","); sb.append(String.format("YEAR(CurrentValue.[%s]) = %s",paramCode,fromTime.getYear())).append(",");
sb.append(String.format("MINUTE(CurrentValue.[%s]) %s 30",paramCode,range)); sb.append(String.format("MONTH(CurrentValue.[%s]) = %s",paramCode,fromTime.getMonth().getValue())).append(",");
sb.append(String.format("DAY(CurrentValue.[%s]) = %s",paramCode,fromTime.getDayOfMonth())).append(",");
sb.append(String.format("HOUR(CurrentValue.[%s]) = %s",paramCode,fromTime.getHour())).append(",");
sb.append(String.format("MINUTE(CurrentValue.[%s]) = %s",paramCode,fromTime.getMinute()));
}else if (EnumsConstants.SCHEDULE_REMINDER_FORMULA.equals(param3)){
//按公式格式查询
String timeStr = DateUtils.ldt2str(fromTime, larkTableConfiguration.getParam4());
sb.append(String.format("CurrentValue.[%s] = \"%s\"",paramCode,timeStr));
}
sb.append(")"); sb.append(")");
return sb.toString(); return sb.toString();
} }

@ -6,7 +6,9 @@ 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.lark.oapi.service.contact.v3.model.GetUserRespBody;
import com.ruoyi.common.constant.EnumsConstants;
import com.ruoyi.common.enums.*; import com.ruoyi.common.enums.*;
import com.ruoyi.common.utils.DateUtils;
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.LarkUserHelper;
@ -77,7 +79,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, 5, 4, 20, 0); // LocalDateTime now = LocalDateTime.of(2023, 4, 24, 8, 30);
for (LarkCompanyTableInfo larkCompanyTableInfo : larkList) { for (LarkCompanyTableInfo larkCompanyTableInfo : larkList) {
try { try {
List<LarkTableConfiguration> larkConfiguration = this.getLarkConfiguration(larkCompanyTableInfo); List<LarkTableConfiguration> larkConfiguration = this.getLarkConfiguration(larkCompanyTableInfo);
@ -86,15 +88,8 @@ public class ScheduledRemindersTask {
if (subType == null) { if (subType == null) {
throw new RuntimeException(String.format("tableId:%s 配置子类:%s 无对应策略处理逻辑", larkTableConfiguration.getLarkTableId(), larkTableConfiguration.getSubConfigType())); throw new RuntimeException(String.format("tableId:%s 配置子类:%s 无对应策略处理逻辑", larkTableConfiguration.getLarkTableId(), larkTableConfiguration.getSubConfigType()));
} }
if (ConfigurationSubTypeEnum.REMINDER_AROUND.equals(subType)) { //按类型执行消息分配动作
String script = LarkTableFilterScriptHelper.buildTimeRange(larkTableConfiguration, ConfigurationSubTypeEnum.REMINDER_BEFORE, RANGE_KEY, now); this.allocateSendMessage(subType,now,larkTableConfiguration,logList,larkCompanyTableInfo);
this.sendMessage(script, larkCompanyTableInfo, logList, larkTableConfiguration, now);
script = LarkTableFilterScriptHelper.buildTimeRange(larkTableConfiguration, ConfigurationSubTypeEnum.REMINDER_AFTER, RANGE_KEY, 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);
}
} }
} catch (Exception e) { } catch (Exception e) {
log.error("ScheduledRemindersTask#scheduledReminders error request:{}", JSONObject.toJSONString(larkCompanyTableInfo), e); log.error("ScheduledRemindersTask#scheduledReminders error request:{}", JSONObject.toJSONString(larkCompanyTableInfo), e);
@ -107,6 +102,25 @@ public class ScheduledRemindersTask {
log.info("scheduledRemindersTask end"); log.info("scheduledRemindersTask end");
} }
private void allocateSendMessage(ConfigurationSubTypeEnum subType,LocalDateTime now,LarkTableConfiguration larkTableConfiguration,List<EventLog> logList,LarkCompanyTableInfo larkCompanyTableInfo){
String script = null;
switch (subType){
case REMINDER_AROUND:
script = LarkTableFilterScriptHelper.buildTimeRange(larkTableConfiguration, ConfigurationSubTypeEnum.REMINDER_BEFORE, RANGE_KEY, 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);
break;
case REMINDER_BEFORE:
case REMINDER_AFTER:
script = LarkTableFilterScriptHelper.buildTimeRange(larkTableConfiguration, subType, RANGE_KEY, now);
this.sendMessage(script, larkCompanyTableInfo, logList, larkTableConfiguration, now);
break;
default:
break;
}
}
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; String pageToken = null;
LarkTableRequest request = this.buildRequest(larkCompanyTableInfo, script); LarkTableRequest request = this.buildRequest(larkCompanyTableInfo, script);
@ -126,7 +140,7 @@ public class ScheduledRemindersTask {
for (AppTableRecord item : items) { 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(fields.get(RANGE_KEY),larkTableConfiguration);
if ((localDateTime.getHour() == now.getHour()) && localDateTime.getMinute() == now.getMinute()) { if ((localDateTime.getHour() == now.getHour()) && localDateTime.getMinute() == now.getMinute()) {
continue; continue;
} }
@ -141,7 +155,20 @@ public class ScheduledRemindersTask {
} while (StringUtils.isNotBlank(pageToken)); } while (StringUtils.isNotBlank(pageToken));
} }
private LocalDateTime changeLocalDateTime(Double timestamp) { private LocalDateTime changeLocalDateTime(Object timestampObj,LarkTableConfiguration larkTableConfiguration) {
String param3 = larkTableConfiguration.getParam3();
if (StringUtils.isBlank(param3) || EnumsConstants.SCHEDULE_REMINDER_TIME.equals(param3)){
return double2LocalDateTime(timestampObj);
}else if (EnumsConstants.SCHEDULE_REMINDER_FORMULA.equals(param3)){
List<LinkedTreeMap> bodyList = (ArrayList<LinkedTreeMap>) timestampObj;
LinkedTreeMap map = bodyList.get(0);
return DateUtils.str2ldt(String.valueOf(map.get("text")),larkTableConfiguration.getParam4());
}
return null;
}
private LocalDateTime double2LocalDateTime(Object timestampObj){
Double timestamp = (Double) timestampObj;
BigDecimal bigDecimal = new BigDecimal(timestamp.toString()); BigDecimal bigDecimal = new BigDecimal(timestamp.toString());
long time = bigDecimal.longValue(); long time = bigDecimal.longValue();
Instant instant = Instant.ofEpochMilli(time); Instant instant = Instant.ofEpochMilli(time);
@ -206,6 +233,10 @@ public class ScheduledRemindersTask {
} }
for (LarkTableRowRelation entry : rowRelationList) { for (LarkTableRowRelation entry : rowRelationList) {
String msg = recursiveValue(record, entry.getFromId(), request); String msg = recursiveValue(record, entry.getFromId(), request);
if (EnumsConstants.SCHEDULE_REMINDER_ROW_RELATION_TYPE.equals(entry.getRemark())){
LocalDateTime localDateTime = this.double2LocalDateTime(Double.valueOf(msg));
msg = DateUtils.ldt2str(localDateTime, DateUtils.YYYY_MM_DD_HH_MM_SS);
}
template = template.replaceAll(entry.getToId(), msg); template = template.replaceAll(entry.getToId(), msg);
} }
return template; return template;

Loading…
Cancel
Save