飞书机器人提醒 切换为机器人一对一私聊发送消息
continuous-integration/drone/push Build is passing Details

于相涌/robot
YXY 1 year ago
parent 212a22f17a
commit f0e6ec42ec

@ -0,0 +1,59 @@
package com.ruoyi.flyingbook.LarkHelper;
import com.lark.oapi.service.im.v1.enums.CreateMessageReceiveIdTypeEnum;
import com.lark.oapi.service.im.v1.model.CreateMessageReq;
import com.lark.oapi.service.im.v1.model.CreateMessageReqBody;
import com.lark.oapi.service.im.v1.model.CreateMessageResp;
import com.lark.oapi.service.im.v1.model.CreateMessageRespBody;
import com.ruoyi.flyingbook.domain.lark.LarkRobotRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
/**
* @author yuxiangyong
* @create 2023-03-12 18:10
*/
@Slf4j
@Component
public class LarkRobotHelper extends LarkHelper{
public static void main(String[] args) throws Exception {
LarkRobotHelper robotHelper = new LarkRobotHelper();
LarkRobotRequest larkRobotRequest = new LarkRobotRequest(appId, secret, "ou_cd09481727e91fce1f12b7b0ed1500d2", "text", "{\"text\":\"test content\"}");
CreateMessageRespBody resp = robotHelper.sendUserMessage(larkRobotRequest);
int i = 0;
}
/**
*
* @return
*/
public CreateMessageRespBody sendUserMessage(LarkRobotRequest request) {
try {
CreateMessageResp createMessageResp = buildClient(request)
.im()
.message()
.create(
CreateMessageReq.newBuilder()
.createMessageReqBody(CreateMessageReqBody.newBuilder()
.msgType(request.getMsgType())
.content(request.getContent())
.receiveId(request.getReceiveId())
.uuid(request.getUuid())
.build())
.receiveIdType(CreateMessageReceiveIdTypeEnum.OPEN_ID)
.build()
);
if (createMessageResp.getCode() == 0){
return createMessageResp.getData();
}else {
throw new RuntimeException(createMessageResp.getMsg());
}
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
}
}
}

@ -22,10 +22,10 @@ public class LarkTableFilterScriptHelper {
Long minute = getMinute(larkTableConfiguration); Long minute = getMinute(larkTableConfiguration);
switch (subType) { switch (subType) {
case REMINDER_BEFORE: case REMINDER_BEFORE:
fromTime = fromTime.minusMinutes(minute); fromTime = fromTime.plusMinutes(minute);
break; break;
case REMINDER_AFTER: case REMINDER_AFTER:
fromTime = fromTime.plusMinutes(minute); fromTime = fromTime.minusMinutes(minute);
break; break;
default: default:
break; break;

@ -0,0 +1,46 @@
package com.ruoyi.flyingbook.domain.lark;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Date;
import java.util.Map;
/**
* @author yuxiangyong
* @create 2023-03-18 1:05
*/
@Data
@NoArgsConstructor
public class LarkRobotRequest extends LarkRequest{
/**
*
*/
private String uuid;
/**
*
*/
private String content;
/**
*
*/
private String msgType;
/**
* id
*/
private String receiveId;
public LarkRobotRequest(String appId, String appSecret) {
super(appId, appSecret);
}
public LarkRobotRequest(String appId, String appSecret, String receiveId,String msgType,String content) {
super(appId, appSecret);
this.receiveId = receiveId;
this.msgType = msgType;
this.content = content;
this.uuid = String.valueOf(new Date().getTime());
}
}

@ -7,12 +7,14 @@ 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.ruoyi.common.enums.*; import com.ruoyi.common.enums.*;
import com.ruoyi.flyingbook.LarkHelper.LarkRobotHelper;
import com.ruoyi.flyingbook.LarkHelper.LarkTableHelper; import com.ruoyi.flyingbook.LarkHelper.LarkTableHelper;
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.EventLog;
import com.ruoyi.flyingbook.domain.LarkCompanyTableInfo; import com.ruoyi.flyingbook.domain.LarkCompanyTableInfo;
import com.ruoyi.flyingbook.domain.LarkTableConfiguration; import com.ruoyi.flyingbook.domain.LarkTableConfiguration;
import com.ruoyi.flyingbook.domain.LarkTemplate; import com.ruoyi.flyingbook.domain.LarkTemplate;
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.mapper.EventLogMapper;
import com.ruoyi.flyingbook.mapper.LarkTableConfigurationMapper; import com.ruoyi.flyingbook.mapper.LarkTableConfigurationMapper;
@ -25,7 +27,10 @@ import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.math.BigDecimal;
import java.time.Instant;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
@ -52,6 +57,8 @@ public class ScheduledRemindersTask {
private LarkTableConfigurationMapper larkTableConfigurationMapper; private LarkTableConfigurationMapper larkTableConfigurationMapper;
@Autowired @Autowired
private EventLogMapper eventLogMapper; private EventLogMapper eventLogMapper;
@Autowired
private LarkRobotHelper larkRobotHelper;
/** /**
* *
@ -71,7 +78,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, 22, 10, 0); // LocalDateTime now = LocalDateTime.of(2023, 4, 23, 20, 0);
for (LarkCompanyTableInfo larkCompanyTableInfo : larkList) { for (LarkCompanyTableInfo larkCompanyTableInfo : larkList) {
try { try {
List<LarkTableConfiguration> larkConfiguration = this.getLarkConfiguration(larkCompanyTableInfo); List<LarkTableConfiguration> larkConfiguration = this.getLarkConfiguration(larkCompanyTableInfo);
@ -82,12 +89,12 @@ public class ScheduledRemindersTask {
} }
if (ConfigurationSubTypeEnum.REMINDER_AROUND.equals(subType)) { if (ConfigurationSubTypeEnum.REMINDER_AROUND.equals(subType)) {
String script = LarkTableFilterScriptHelper.buildTimeRange(larkTableConfiguration, ConfigurationSubTypeEnum.REMINDER_BEFORE, RANGE_KEY, now); String script = LarkTableFilterScriptHelper.buildTimeRange(larkTableConfiguration, ConfigurationSubTypeEnum.REMINDER_BEFORE, RANGE_KEY, now);
this.sendMessage(script, larkCompanyTableInfo, logList,larkTableConfiguration); this.sendMessage(script, larkCompanyTableInfo, logList,larkTableConfiguration,now);
script = LarkTableFilterScriptHelper.buildTimeRange(larkTableConfiguration, ConfigurationSubTypeEnum.REMINDER_AFTER, RANGE_KEY, now); script = LarkTableFilterScriptHelper.buildTimeRange(larkTableConfiguration, ConfigurationSubTypeEnum.REMINDER_AFTER, RANGE_KEY, now);
this.sendMessage(script, larkCompanyTableInfo, logList,larkTableConfiguration); this.sendMessage(script, larkCompanyTableInfo, logList,larkTableConfiguration,now);
} else { } else {
String script = LarkTableFilterScriptHelper.buildTimeRange(larkTableConfiguration, subType, RANGE_KEY, now); String script = LarkTableFilterScriptHelper.buildTimeRange(larkTableConfiguration, subType, RANGE_KEY, now);
this.sendMessage(script, larkCompanyTableInfo, logList,larkTableConfiguration); this.sendMessage(script, larkCompanyTableInfo, logList,larkTableConfiguration,now);
} }
} }
} catch (Exception e) { } catch (Exception e) {
@ -101,12 +108,12 @@ public class ScheduledRemindersTask {
log.info("scheduledRemindersTask end"); log.info("scheduledRemindersTask end");
} }
private void sendMessage(String script, LarkCompanyTableInfo larkCompanyTableInfo, List<EventLog> logList,LarkTableConfiguration larkTableConfiguration) { 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);
LarkTemplate template = this.getTemplate(larkCompanyTableInfo,larkTableConfiguration.getId()); LarkTemplate template = this.getTemplate(larkCompanyTableInfo,larkTableConfiguration.getId());
do { do {
JSONObject msg = new JSONObject(); Object msg = new JSONObject();
ListAppTableRecordRespBody respBody = larkTableHelper.listTableRecord(request); ListAppTableRecordRespBody respBody = larkTableHelper.listTableRecord(request);
if (respBody.getPageToken().equals(pageToken)){ if (respBody.getPageToken().equals(pageToken)){
return; return;
@ -116,8 +123,12 @@ public class ScheduledRemindersTask {
for (AppTableRecord item : respBody.getItems()) { for (AppTableRecord item : respBody.getItems()) {
try { try {
Map<String, Object> fields = item.getFields(); Map<String, Object> fields = item.getFields();
LocalDateTime localDateTime = changeLocalDateTime((Double) fields.get(RANGE_KEY));
if ((localDateTime.getHour() == now.getHour() )&& localDateTime.getMinute() == now.getMinute()){
continue;
}
msg = this.buildMsg(larkCompanyTableInfo, fields, template); msg = this.buildMsg(larkCompanyTableInfo, fields, template);
OkHttpHelper.post(larkCompanyTableInfo.getToAppToken(), msg.toString());
logList.add(new EventLog(larkCompanyTableInfo.getId(), EventOperateType.SCHEDULE_REMINDER.getCode(), msg.toString())); logList.add(new EventLog(larkCompanyTableInfo.getId(), EventOperateType.SCHEDULE_REMINDER.getCode(), msg.toString()));
} catch (Exception e) { } catch (Exception e) {
log.error("ScheduledRemindersTask#scheduledReminders error request tableId:{} msg:{}", JSONObject.toJSONString(larkCompanyTableInfo), msg.toString(), e); log.error("ScheduledRemindersTask#scheduledReminders error request tableId:{} msg:{}", JSONObject.toJSONString(larkCompanyTableInfo), msg.toString(), e);
@ -127,6 +138,14 @@ public class ScheduledRemindersTask {
} while (StringUtils.isNotBlank(pageToken)); } while (StringUtils.isNotBlank(pageToken));
} }
private LocalDateTime changeLocalDateTime(Double timestamp){
BigDecimal bigDecimal = new BigDecimal(timestamp.toString());
long time = bigDecimal.longValue();
Instant instant = Instant.ofEpochMilli(time);
ZoneId zone = ZoneId.systemDefault();
return LocalDateTime.ofInstant(instant, zone);
}
private LarkTemplate getTemplate(LarkCompanyTableInfo larkCompanyTableInfo,Long configurationId) { private LarkTemplate getTemplate(LarkCompanyTableInfo larkCompanyTableInfo,Long configurationId) {
LarkTemplate larkTemplate = new LarkTemplate(); LarkTemplate larkTemplate = new LarkTemplate();
larkTemplate.setTemplateType(TemplateTypeEnum.SCHEDULED_REMINDER.getCode()); larkTemplate.setTemplateType(TemplateTypeEnum.SCHEDULED_REMINDER.getCode());
@ -155,26 +174,28 @@ public class ScheduledRemindersTask {
return configurationList; return configurationList;
} }
private JSONObject buildMsg(LarkCompanyTableInfo larkCompanyTableInfo, Map<String, Object> fields, LarkTemplate template) { private Object buildMsg(LarkCompanyTableInfo larkCompanyTableInfo, Map<String, Object> fields, LarkTemplate template) {
JSONObject msg = new JSONObject(); Object msg = "";
TemplateSubTypeEnum templateSubTypeEnum = TemplateSubTypeEnum.getByCode(template.getSubTemplateType()); TemplateSubTypeEnum templateSubTypeEnum = TemplateSubTypeEnum.getByCode(template.getSubTemplateType());
if (templateSubTypeEnum == null) { if (templateSubTypeEnum == null) {
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());
switch (templateSubTypeEnum) { switch (templateSubTypeEnum) {
case TEXT: case TEXT:
msg.put("msg_type", templateSubTypeEnum.getCode()); robotRequest.setMsgType(templateSubTypeEnum.getCode());
String templateContent = template.getTemplateContent(); 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("业务员"); List<LinkedTreeMap<String,String>> people = (List<LinkedTreeMap<String,String>>)fields.get("业务员");
StringBuilder sb = new StringBuilder();
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);
sb.append(String.format("<at user_id = \"%s\">%s</at>", detail.get("id"), detail.get("name"))); robotRequest.setReceiveId(detail.get("id"));
msg = robotRequest;
larkRobotHelper.sendUserMessage(robotRequest);
} }
templateContent = templateContent.replaceAll("#name", String.valueOf(fields.get("学生姓名")));
JSONObject content = new JSONObject();
content.put("text", sb.append(templateContent).toString());
msg.put("content", content);
break; break;
default: default:
break; break;

Loading…
Cancel
Save