diff --git a/lark-sender/pom.xml b/lark-sender/pom.xml index 41691ae..b432e89 100644 --- a/lark-sender/pom.xml +++ b/lark-sender/pom.xml @@ -18,4 +18,12 @@ UTF-8 + + + + com.larksuite.oapi + oapi-sdk + 2.4.16 + + \ No newline at end of file diff --git a/lark-sender/src/main/java/cn/hezhaohui/lark/constant/AppConstant.java b/lark-sender/src/main/java/cn/hezhaohui/lark/constant/AppConstant.java new file mode 100644 index 0000000..f10f0fd --- /dev/null +++ b/lark-sender/src/main/java/cn/hezhaohui/lark/constant/AppConstant.java @@ -0,0 +1,6 @@ +package cn.hezhaohui.lark.constant; + +public class AppConstant { + public static final String APP_ID = "cli_a860966d8eb69013"; + public static final String APP_SECRET = "srCSYZQDaglgSkLSs5MGrfEsCW4fCQE2"; +} diff --git a/lark-sender/src/main/java/cn/hezhaohui/lark/constant/UserConstant.java b/lark-sender/src/main/java/cn/hezhaohui/lark/constant/UserConstant.java new file mode 100644 index 0000000..e216c2d --- /dev/null +++ b/lark-sender/src/main/java/cn/hezhaohui/lark/constant/UserConstant.java @@ -0,0 +1,5 @@ +package cn.hezhaohui.lark.constant; + +public class UserConstant { + public static final String USER_OPEN_ID = "ou_728072d781317a402a0ba6b6d30390a6"; +} diff --git a/lark-sender/src/main/java/cn/hezhaohui/lark/util/Lark.java b/lark-sender/src/main/java/cn/hezhaohui/lark/util/Lark.java new file mode 100644 index 0000000..feaa0ba --- /dev/null +++ b/lark-sender/src/main/java/cn/hezhaohui/lark/util/Lark.java @@ -0,0 +1,60 @@ +package cn.hezhaohui.lark.util; + +import cn.hezhaohui.lark.constant.AppConstant; +import cn.hezhaohui.lark.constant.UserConstant; +import com.google.gson.JsonParser; +import com.lark.oapi.Client; +import com.lark.oapi.core.utils.Jsons; +import com.lark.oapi.service.im.v1.model.*; + +import java.nio.charset.StandardCharsets; +import java.util.HashMap; +import java.util.Map; +import java.util.UUID; + + +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 sendTextMessage(String content) throws Exception { + // 构建client + Client client = Client.newBuilder(AppConstant.APP_ID, AppConstant.APP_SECRET).build(); + + // 生成uuid + String uuid = UUID.randomUUID().toString(); + + Map messageContent = new HashMap<>(); + messageContent.put("text", content); + String jsonContent = Jsons.DEFAULT.toJson(messageContent); + + // 创建请求对象 + CreateMessageReq req = CreateMessageReq.newBuilder() + .receiveIdType("open_id") + .createMessageReqBody(CreateMessageReqBody.newBuilder() + .receiveId(UserConstant.USER_OPEN_ID) + .msgType("text") + .content(jsonContent) + .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())); + } + + +} diff --git a/lark-sender/src/test/java/cn/hezhaohui/lark/util/LarkTest.java b/lark-sender/src/test/java/cn/hezhaohui/lark/util/LarkTest.java new file mode 100644 index 0000000..789289a --- /dev/null +++ b/lark-sender/src/test/java/cn/hezhaohui/lark/util/LarkTest.java @@ -0,0 +1,12 @@ +package cn.hezhaohui.lark.util; + +import org.junit.Test; + +public class LarkTest { + + + @Test + public void sendTextMessage() throws Exception { + Lark.sendTextMessage("测试消息"); + } +} \ No newline at end of file diff --git a/pom.xml b/pom.xml index 8b60ee1..4e69b99 100644 --- a/pom.xml +++ b/pom.xml @@ -9,6 +9,12 @@ 1.0-SNAPSHOT pom + + org.springframework.boot + spring-boot-starter-parent + 3.2.2 + + lark-sender gitea-webhook @@ -20,4 +26,12 @@ UTF-8 + + + junit + junit + test + + + \ No newline at end of file