add:自建机器人发送卡片消息
This commit is contained in:
+10
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
+37
@@ -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;
|
||||
}
|
||||
+8
@@ -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);
|
||||
|
||||
}
|
||||
|
||||
+96
-1
@@ -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<JSONObject> entity = new HttpEntity<>(jsonObject, headers);
|
||||
ResponseEntity<String> 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<JSONObject> 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<JSONObject> 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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user