飞书卡片消息封装(未测试)
This commit is contained in:
+121
@@ -1,8 +1,10 @@
|
|||||||
package com.jero.modules.feishu.service.impl;
|
package com.jero.modules.feishu.service.impl;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSON;
|
||||||
import com.alibaba.fastjson.JSONObject;
|
import com.alibaba.fastjson.JSONObject;
|
||||||
import com.jero.modules.feishu.service.IFeishuService;
|
import com.jero.modules.feishu.service.IFeishuService;
|
||||||
|
import com.jero.modules.feishu.vo.FeishuMsgVo;
|
||||||
import com.jero.modules.system.util.HttpRequestUtil;
|
import com.jero.modules.system.util.HttpRequestUtil;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
@@ -111,4 +113,123 @@ public class FeishuServiceImpl implements IFeishuService {
|
|||||||
}
|
}
|
||||||
return tokenJson.get("msg").toString();
|
return tokenJson.get("msg").toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 发送卡片消息
|
||||||
|
* @param feishuMsgVo
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public String sendCardMsg(String[] userIds, FeishuMsgVo feishuMsgVo) throws IOException {
|
||||||
|
String token = getTenantAccessToken();
|
||||||
|
// 设置请求头
|
||||||
|
Map<String, String> headerMap = new HashMap<>();
|
||||||
|
headerMap.put("Authorization", "Bearer " + token);
|
||||||
|
headerMap.put("Content-Type", "application/json; charset=utf-8");
|
||||||
|
|
||||||
|
// 示例
|
||||||
|
StringBuilder contentSb = new StringBuilder();
|
||||||
|
contentSb.append("{");
|
||||||
|
contentSb.append("\"elements\": [");
|
||||||
|
// 补充内容
|
||||||
|
if (StrUtil.isNotEmpty(feishuMsgVo.getContent())){
|
||||||
|
contentSb.append("{");
|
||||||
|
contentSb.append("\"tag\": \"div\",");
|
||||||
|
contentSb.append("\"text\": {");
|
||||||
|
contentSb.append("\"content\": " + feishuMsgVo.getContent() + ",");
|
||||||
|
contentSb.append("\"tag\": \"lark_md\"");
|
||||||
|
contentSb.append("}");
|
||||||
|
contentSb.append("},");
|
||||||
|
};
|
||||||
|
// 补充tasktype、project、Initiator等的外层
|
||||||
|
contentSb.append("{");
|
||||||
|
contentSb.append("\"fields\": [");
|
||||||
|
if (StrUtil.isNotEmpty(feishuMsgVo.getTaskType())){
|
||||||
|
contentSb.append("{");
|
||||||
|
contentSb.append("\"is_short\": false,");
|
||||||
|
contentSb.append("\"text\": {");
|
||||||
|
contentSb.append("\"content\": \"**Task Type:** " + feishuMsgVo.getTaskType() + "\",");
|
||||||
|
contentSb.append("\"tag\": \"lark_md\"");
|
||||||
|
contentSb.append("}");
|
||||||
|
contentSb.append("},");
|
||||||
|
}
|
||||||
|
if (StrUtil.isNotEmpty(feishuMsgVo.getRegulationNo())){
|
||||||
|
contentSb.append("{");
|
||||||
|
contentSb.append("\"is_short\": false,");
|
||||||
|
contentSb.append("\"text\": {");
|
||||||
|
contentSb.append("\"content\": \"**Regulation No:** " + feishuMsgVo.getRegulationNo() + "\",");
|
||||||
|
contentSb.append("\"tag\": \"lark_md\"");
|
||||||
|
contentSb.append("}");
|
||||||
|
contentSb.append("},");
|
||||||
|
}
|
||||||
|
if (StrUtil.isNotEmpty(feishuMsgVo.getProject())){
|
||||||
|
contentSb.append("{");
|
||||||
|
contentSb.append("\"is_short\": false,");
|
||||||
|
contentSb.append("\"text\": {");
|
||||||
|
contentSb.append("\"content\": \"**Project:** " + feishuMsgVo.getProject() + "\",");
|
||||||
|
contentSb.append("\"tag\": \"lark_md\"");
|
||||||
|
contentSb.append("}");
|
||||||
|
contentSb.append("},");
|
||||||
|
}
|
||||||
|
if (StrUtil.isNotEmpty(feishuMsgVo.getInitiator())){
|
||||||
|
contentSb.append("{");
|
||||||
|
contentSb.append("\"is_short\": false,");
|
||||||
|
contentSb.append("\"text\": {");
|
||||||
|
contentSb.append("\"content\": \"**Initiator:** " + feishuMsgVo.getInitiator() + "\",");
|
||||||
|
contentSb.append("\"tag\": \"lark_md\"");
|
||||||
|
contentSb.append("}");
|
||||||
|
contentSb.append("},");
|
||||||
|
}
|
||||||
|
if (StrUtil.isNotEmpty(feishuMsgVo.getDueDate())){
|
||||||
|
contentSb.append("{");
|
||||||
|
contentSb.append("\"is_short\": false,");
|
||||||
|
contentSb.append("\"text\": {");
|
||||||
|
contentSb.append("\"content\": \"**Due Date:** " + feishuMsgVo.getDueDate() + "\",");
|
||||||
|
contentSb.append("\"tag\": \"lark_md\"");
|
||||||
|
contentSb.append("}");
|
||||||
|
contentSb.append("},");
|
||||||
|
}
|
||||||
|
contentSb.append("]");
|
||||||
|
contentSb.append("\"tag\": \"div\"");
|
||||||
|
contentSb.append("},");
|
||||||
|
contentSb.append("{\"tag\": \"hr\"},");
|
||||||
|
contentSb.append("{");
|
||||||
|
contentSb.append("\"actions\": [");
|
||||||
|
contentSb.append("{");
|
||||||
|
contentSb.append("\"tag\": \"button\",");
|
||||||
|
contentSb.append("\"text\": {");
|
||||||
|
contentSb.append("\"content\": \"view\",");
|
||||||
|
contentSb.append("\"tag\": \"plain_text\"");
|
||||||
|
contentSb.append("},");
|
||||||
|
contentSb.append("\"type\": \"primary\",");
|
||||||
|
contentSb.append("\"url\": \" "+ feishuMsgVo.getUrl() +" \"");
|
||||||
|
contentSb.append("}");
|
||||||
|
contentSb.append("},");
|
||||||
|
contentSb.append("\"tag\": \"action\"");
|
||||||
|
contentSb.append("}");
|
||||||
|
contentSb.append("],");
|
||||||
|
contentSb.append("\"header\": {");
|
||||||
|
contentSb.append("\"template\": \"green\",");
|
||||||
|
contentSb.append("\"title\": {");
|
||||||
|
contentSb.append("\"content\": \" " + feishuMsgVo.getTitle() + " \",");
|
||||||
|
contentSb.append("\"tag\": \"plain_text\"");
|
||||||
|
contentSb.append("}");
|
||||||
|
contentSb.append("}");
|
||||||
|
contentSb.append("}");
|
||||||
|
|
||||||
|
// 设置请求参数
|
||||||
|
JSONObject paramsMap = new JSONObject();
|
||||||
|
paramsMap.put("user_ids", userIds);
|
||||||
|
paramsMap.put("msg_type", "post");
|
||||||
|
paramsMap.put("content", contentSb.toString());
|
||||||
|
|
||||||
|
// 发送请求给飞书
|
||||||
|
String response = HttpRequestUtil.getResponseOfPOST(batchSendMessageUrl, headerMap, paramsMap.toJSONString());
|
||||||
|
// 获取返回结果
|
||||||
|
JSONObject tokenJson = JSONObject.parseObject(response);
|
||||||
|
if(!tokenJson.get("code").toString().equals("0")) {
|
||||||
|
log.error("请求出现异常:" + tokenJson.get("msg") + " " + tokenJson);
|
||||||
|
}
|
||||||
|
return tokenJson.get("msg").toString();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,28 @@
|
|||||||
|
package com.jero.modules.feishu.vo;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 飞书消息的VO
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class FeishuMsgVo {
|
||||||
|
|
||||||
|
private String title; //标题
|
||||||
|
|
||||||
|
private String content; //内容
|
||||||
|
|
||||||
|
private String taskType; //任务类型
|
||||||
|
|
||||||
|
private String regulationNo; //法规编号
|
||||||
|
|
||||||
|
private String project; //项目名称
|
||||||
|
|
||||||
|
private String initiator; //发起人
|
||||||
|
|
||||||
|
private String dueDate; //截止时间
|
||||||
|
|
||||||
|
private String url; //回调的网址
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user