✨Feat: 实现消息卡片推送
This commit is contained in:
+1
-1
@@ -1,3 +1,3 @@
|
|||||||
.idea
|
.idea
|
||||||
*.iml
|
*.iml
|
||||||
/target
|
target
|
||||||
@@ -3,4 +3,6 @@ package cn.hezhaohui.lark.constant;
|
|||||||
public class AppConstant {
|
public class AppConstant {
|
||||||
public static final String APP_ID = "cli_a860966d8eb69013";
|
public static final String APP_ID = "cli_a860966d8eb69013";
|
||||||
public static final String APP_SECRET = "srCSYZQDaglgSkLSs5MGrfEsCW4fCQE2";
|
public static final String APP_SECRET = "srCSYZQDaglgSkLSs5MGrfEsCW4fCQE2";
|
||||||
|
public static final String TEMPATE_ID = "AAqxBOlIDzI20";
|
||||||
|
public static final String TEMPLATE_VERSION = "1.0.0";
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,18 @@
|
|||||||
|
package cn.hezhaohui.lark.entity;
|
||||||
|
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@Builder
|
||||||
|
public class CardData {
|
||||||
|
private String ai;
|
||||||
|
private String link;
|
||||||
|
private String msg;
|
||||||
|
private String repo;
|
||||||
|
private String subtitle;
|
||||||
|
private String tag;
|
||||||
|
private String title;
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -2,6 +2,7 @@ package cn.hezhaohui.lark.util;
|
|||||||
|
|
||||||
import cn.hezhaohui.lark.constant.AppConstant;
|
import cn.hezhaohui.lark.constant.AppConstant;
|
||||||
import cn.hezhaohui.lark.constant.UserConstant;
|
import cn.hezhaohui.lark.constant.UserConstant;
|
||||||
|
import cn.hezhaohui.lark.entity.CardData;
|
||||||
import com.google.gson.JsonParser;
|
import com.google.gson.JsonParser;
|
||||||
import com.lark.oapi.Client;
|
import com.lark.oapi.Client;
|
||||||
import com.lark.oapi.core.utils.Jsons;
|
import com.lark.oapi.core.utils.Jsons;
|
||||||
@@ -57,4 +58,63 @@ public class Lark {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// SDK 使用文档:https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/server-side-sdk/java-sdk-guide/preparations
|
||||||
|
// 复制该 Demo 后, 需要将 "YOUR_APP_ID", "YOUR_APP_SECRET" 替换为自己应用的 APP_ID, APP_SECRET.
|
||||||
|
// 以下示例代码默认根据文档示例值填充,如果存在代码问题,请在 API 调试台填上相关必要参数后再复制代码使用
|
||||||
|
|
||||||
|
|
||||||
|
public static void sendCardMessage(CardData cardData) throws Exception {
|
||||||
|
// 构建client
|
||||||
|
Client client = Client.newBuilder(AppConstant.APP_ID, AppConstant.APP_SECRET).build();
|
||||||
|
|
||||||
|
// 生成uuid
|
||||||
|
String uuid = UUID.randomUUID().toString();
|
||||||
|
|
||||||
|
// 创建卡片内容
|
||||||
|
Map<String, String> cardContent = new HashMap<>();
|
||||||
|
cardContent.put("ai", cardData.getAi());
|
||||||
|
cardContent.put("link", cardData.getLink());
|
||||||
|
cardContent.put("msg", cardData.getMsg());
|
||||||
|
cardContent.put("repo", cardData.getRepo());
|
||||||
|
cardContent.put("subtitle", cardData.getSubtitle());
|
||||||
|
cardContent.put("tag", cardData.getTag());
|
||||||
|
cardContent.put("title", cardData.getTitle());
|
||||||
|
// String jsonCardContent = Jsons.DEFAULT.toJson(cardContent);
|
||||||
|
|
||||||
|
Map<String, Object> templateData = new HashMap<>();
|
||||||
|
templateData.put("template_id", AppConstant.TEMPATE_ID);
|
||||||
|
templateData.put("template_version_name", AppConstant.TEMPLATE_VERSION);
|
||||||
|
templateData.put("template_variable", cardContent);
|
||||||
|
// String jsonCardContent = Jsons.DEFAULT.toJson(templateData);
|
||||||
|
|
||||||
|
Map<String, Object> jsonContent = new HashMap<>();
|
||||||
|
jsonContent.put("data", templateData);
|
||||||
|
jsonContent.put("type", "template");
|
||||||
|
String json = Jsons.DEFAULT.toJson(jsonContent);
|
||||||
|
|
||||||
|
// 创建请求对象
|
||||||
|
CreateMessageReq req = CreateMessageReq.newBuilder()
|
||||||
|
.receiveIdType("open_id")
|
||||||
|
.createMessageReqBody(CreateMessageReqBody.newBuilder()
|
||||||
|
.receiveId(UserConstant.USER_OPEN_ID)
|
||||||
|
.msgType("interactive")
|
||||||
|
.content(json)
|
||||||
|
.uuid(uuid)
|
||||||
|
.build())
|
||||||
|
.build();
|
||||||
|
|
||||||
|
// 发起请求
|
||||||
|
CreateMessageResp resp = client.im().v1().message().create(req);
|
||||||
|
|
||||||
|
// 处理服务端错误
|
||||||
|
if(!resp.success()) {
|
||||||
|
System.out.println(String.format("code:%s,msg:%s,reqId:%s, resp:%s",
|
||||||
|
resp.getCode(), resp.getMsg(), resp.getRequestId(), Jsons.createGSON(true, false).toJson(JsonParser.parseString(new String(resp.getRawResponse().getBody(), StandardCharsets.UTF_8)))));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 业务数据处理
|
||||||
|
System.out.println(Jsons.DEFAULT.toJson(resp.getData()));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package cn.hezhaohui.lark.util;
|
package cn.hezhaohui.lark.util;
|
||||||
|
|
||||||
|
import cn.hezhaohui.lark.entity.CardData;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
public class LarkTest {
|
public class LarkTest {
|
||||||
@@ -9,4 +10,20 @@ public class LarkTest {
|
|||||||
public void sendTextMessage() throws Exception {
|
public void sendTextMessage() throws Exception {
|
||||||
Lark.sendTextMessage("测试消息");
|
Lark.sendTextMessage("测试消息");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSendCardMessage() throws Exception{
|
||||||
|
CardData cardData = CardData.builder()
|
||||||
|
.ai("待开发")
|
||||||
|
.tag("PUSH")
|
||||||
|
.msg("[1d7e6a]✨Feat: 实现文本消息发送\n" +
|
||||||
|
"[c5e3bb]\uD83D\uDD27Chore: 构建 Maven 项目框架")
|
||||||
|
.title("[Gitea] 仓库状态变化")
|
||||||
|
.repo("lark-webhook-sender")
|
||||||
|
.link("http://47.121.181.112:3000/wonder/lark-webhook-sender")
|
||||||
|
.subtitle("Ciallo~(∠・ω< )⌒★")
|
||||||
|
.build();
|
||||||
|
Lark.sendCardMessage(cardData);
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -32,6 +32,11 @@
|
|||||||
<artifactId>junit</artifactId>
|
<artifactId>junit</artifactId>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
Reference in New Issue
Block a user