From d9a527cf37a6811fe0d6f4c65fe723244786df94 Mon Sep 17 00:00:00 2001 From: sunzheng Date: Mon, 5 Aug 2024 10:58:38 +0800 Subject: [PATCH] =?UTF-8?q?add:=E8=87=AA=E5=BB=BA=E6=9C=BA=E5=99=A8?= =?UTF-8?q?=E4=BA=BA=E5=8F=91=E9=80=81=E5=8D=A1=E7=89=87=E6=B6=88=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/FeiShuDockingController.java | 10 ++ .../docking/feishu/dto/BotCardMessageDto.java | 37 +++++++ .../feishu/service/IFeiShuDockingService.java | 8 ++ .../impl/FeiShuDockingServiceImpl.java | 97 ++++++++++++++++++- .../src/main/resources/application-dev.yml | 2 + 5 files changed, 153 insertions(+), 1 deletion(-) create mode 100644 laws-modules-docking/src/main/java/com/jero/modules/docking/feishu/dto/BotCardMessageDto.java diff --git a/laws-modules-docking/src/main/java/com/jero/modules/docking/feishu/controller/FeiShuDockingController.java b/laws-modules-docking/src/main/java/com/jero/modules/docking/feishu/controller/FeiShuDockingController.java index d562f17a..91903f4a 100644 --- a/laws-modules-docking/src/main/java/com/jero/modules/docking/feishu/controller/FeiShuDockingController.java +++ b/laws-modules-docking/src/main/java/com/jero/modules/docking/feishu/controller/FeiShuDockingController.java @@ -3,6 +3,7 @@ package com.jero.modules.docking.feishu.controller; import com.alibaba.fastjson.JSONObject; import com.jero.common.api.vo.Result; import com.jero.modules.docking.feishu.dto.BotApprovalMessageDto; +import com.jero.modules.docking.feishu.dto.BotCardMessageDto; import com.jero.modules.docking.feishu.dto.BotMessageDto; import com.jero.modules.docking.feishu.dto.InstanceDto; import com.jero.modules.docking.feishu.service.IFeiShuDockingService; @@ -109,4 +110,13 @@ public class FeiShuDockingController { public void updateBotMessage(@RequestBody ApprovalsUpdateMessageVo approvalsUpdateMessageVo) { feiShuDockingService.updateBotMessage(approvalsUpdateMessageVo); } + + /** + * 发送Bot卡片消息 + * @return + */ + @PostMapping("/sendCardMessageByBot") + public String sendCardMessageByBot(@RequestBody BotCardMessageDto botCardMessageDto) { + return feiShuDockingService.sendCardMessageByBot(botCardMessageDto); + } } diff --git a/laws-modules-docking/src/main/java/com/jero/modules/docking/feishu/dto/BotCardMessageDto.java b/laws-modules-docking/src/main/java/com/jero/modules/docking/feishu/dto/BotCardMessageDto.java new file mode 100644 index 00000000..eeb7ba98 --- /dev/null +++ b/laws-modules-docking/src/main/java/com/jero/modules/docking/feishu/dto/BotCardMessageDto.java @@ -0,0 +1,37 @@ +package com.jero.modules.docking.feishu.dto; + +import lombok.Data; + +@Data +public class BotCardMessageDto { + + /** + * 标题 + */ + private String title; + + /** + * 接收人的user_id + */ + private String receiveId; + + /** + * 申请人 + */ + private String applicant; + + /** + * 时间 + */ + private String leaveTime; + + /** + * 备注 + */ + private String remarks; + + /** + * 跳转地址 + */ + private String url; +} diff --git a/laws-modules-docking/src/main/java/com/jero/modules/docking/feishu/service/IFeiShuDockingService.java b/laws-modules-docking/src/main/java/com/jero/modules/docking/feishu/service/IFeiShuDockingService.java index b74e9d74..0402581c 100644 --- a/laws-modules-docking/src/main/java/com/jero/modules/docking/feishu/service/IFeiShuDockingService.java +++ b/laws-modules-docking/src/main/java/com/jero/modules/docking/feishu/service/IFeiShuDockingService.java @@ -3,6 +3,7 @@ package com.jero.modules.docking.feishu.service; import com.alibaba.fastjson.JSONObject; import com.jero.common.api.vo.Result; import com.jero.modules.docking.feishu.dto.BotApprovalMessageDto; +import com.jero.modules.docking.feishu.dto.BotCardMessageDto; import com.jero.modules.docking.feishu.dto.BotMessageDto; import com.jero.modules.docking.feishu.dto.InstanceDto; import com.jero.modules.docking.feishu.vo.definition.ApprovalsDefinitionVo; @@ -68,4 +69,11 @@ public interface IFeiShuDockingService { * @return */ void updateBotMessage(ApprovalsUpdateMessageVo approvalsUpdateMessageVo); + + /** + * 发送卡片消息 + * @param botCardMessageDto + */ + String sendCardMessageByBot(BotCardMessageDto botCardMessageDto); + } diff --git a/laws-modules-docking/src/main/java/com/jero/modules/docking/feishu/service/impl/FeiShuDockingServiceImpl.java b/laws-modules-docking/src/main/java/com/jero/modules/docking/feishu/service/impl/FeiShuDockingServiceImpl.java index 6be9c1db..e4fe0139 100644 --- a/laws-modules-docking/src/main/java/com/jero/modules/docking/feishu/service/impl/FeiShuDockingServiceImpl.java +++ b/laws-modules-docking/src/main/java/com/jero/modules/docking/feishu/service/impl/FeiShuDockingServiceImpl.java @@ -17,7 +17,6 @@ import com.jero.common.util.oConvertUtils; import com.jero.modules.activiti.entity.ProcessApprovalRecord; import com.jero.modules.activiti.service.ProcessApprovalRecordService; import com.jero.modules.docking.feishu.common.Constant; -import com.jero.modules.docking.feishu.controller.FeishuBotService; import com.jero.modules.docking.feishu.dto.BotApprovalMessageDto; import com.jero.modules.docking.feishu.dto.BotCardMessageDto; import com.jero.modules.docking.feishu.dto.BotMessageDto; @@ -781,4 +780,100 @@ public class FeiShuDockingServiceImpl implements IFeiShuDockingService { restTemplate.exchange(url, HttpMethod.POST, entity, String.class); } + @Override + public String sendCardMessageByBot(BotCardMessageDto botCardMessageDto) { + String url = env.getProperty("feishu.bot_send_card_message"); + assert url != null; + // 获取token + String accessToken = getAccessToken(); + // 设置请求头 + HttpHeaders headers = new HttpHeaders(); + headers.set("Authorization", "Bearer " + accessToken); + headers.set("Content-Type", "application/json"); + // 构建消息内容 + JSONObject content = buildCardMessageContent(botCardMessageDto.getTitle(), botCardMessageDto.getApplicant(), + botCardMessageDto.getLeaveTime(), botCardMessageDto.getRemarks(), botCardMessageDto.getUrl()); + JSONObject jsonObject = new JSONObject(); + jsonObject.put("receive_id", botCardMessageDto.getReceiveId()); + jsonObject.put("content", JSON.toJSONString(content)); + jsonObject.put("msg_type", "interactive"); + + // 创建 HttpEntity + HttpEntity entity = new HttpEntity<>(jsonObject, headers); + ResponseEntity response = restTemplate.exchange(url, HttpMethod.POST, entity, String.class); + // 处理响应 + JSONObject jsonResponse = JSON.parseObject(response.getBody()); + if ("0".equals((String.valueOf(jsonResponse.get("code"))))) { + JSONObject data = (JSONObject) jsonResponse.get("data"); + return data.getString("message_id"); + } else { + log.error(jsonResponse.get("msg").toString()); + throw new JeroBootException("Bot发送卡片消息失败"); + } + + } + + public JSONObject buildCardMessageContent(String title, String applicant, String leaveTime, String remarks, String url) { + // 配置卡片消息 + JSONObject content = new JSONObject(); + JSONObject config = new JSONObject(); + config.put("wide_screen_mode", true); + content.put("config", config); + + // 设置标题 + JSONObject header = new JSONObject(); + JSONObject headerTitle = new JSONObject(); + headerTitle.put("tag", "plain_text"); + headerTitle.put("content", title); + header.put("title", headerTitle); + content.put("header", header); + + // 构建字段数组 + List fields = new ArrayList<>(); + + fields.add(createField("申请人", applicant, true)); + fields.add(createField("", "", false)); // 空行 + fields.add(createField("时间", leaveTime, false)); + fields.add(createField("", "", false)); // 空行 + fields.add(createField("备注", remarks, true)); + + // 创建 div 标签元素 + JSONObject divElement = new JSONObject(); + divElement.put("tag", "div"); + divElement.put("fields", fields); + // 创建 action 按钮 + JSONObject button = new JSONObject(); + button.put("tag", "button"); + JSONObject buttonText = new JSONObject(); + buttonText.put("tag", "plain_text"); + buttonText.put("content", "查看详情"); + button.put("text", buttonText); + button.put("type", "primary"); + button.put("url", url); + + JSONObject actionElement = new JSONObject(); + actionElement.put("tag", "action"); + actionElement.put("layout", "bisected"); + actionElement.put("actions", JSONArray.parseArray(JSON.toJSONString(Arrays.asList(button)))); + + // 创建 hr 标签元素 + JSONObject hrElement = new JSONObject(); + hrElement.put("tag", "hr"); + + // 构建 elements 数组 + List elements = Arrays.asList(divElement, hrElement, actionElement); + content.put("elements", elements); + + return content; + } + + private JSONObject createField(String label, String value, boolean isShort) { + JSONObject field = new JSONObject(); + field.put("is_short", isShort); + JSONObject text = new JSONObject(); + text.put("tag", "lark_md"); + text.put("content", label + value); + field.put("text", text); + return field; + } } diff --git a/laws-single-startup/src/main/resources/application-dev.yml b/laws-single-startup/src/main/resources/application-dev.yml index 66a0d8ff..faef16d9 100644 --- a/laws-single-startup/src/main/resources/application-dev.yml +++ b/laws-single-startup/src/main/resources/application-dev.yml @@ -593,6 +593,8 @@ feishu: bot_send_approval_message: ${feishu.base_url}/approval/v1/message/send # 飞书更新审批 Bot消息 update_send_approval_message: ${feishu.base_url}/approval/v1/message/update + # 使用Bot发送卡片消息 + bot_send_card_message: ${feishu.base_url}/im/v1/messages?receive_id_type=user_id # 飞书单点登录地址 sign_login_in: ${feishu.base_url}/authen/v1/index?redirect_uri={REDIRECT_URI}&app_id=${feishu.app_id}&state=feishu # 飞书PC端待办地址