diff --git a/lark/pom.xml b/lark/pom.xml index 96a347e..a0bdede 100644 --- a/lark/pom.xml +++ b/lark/pom.xml @@ -16,6 +16,23 @@ org.springframework.boot spring-boot-starter-web + + + com.larksuite.oapi + oapi-sdk + 2.4.24 + + + + cn.hutool + hutool-all + + + + org.springframework.boot + spring-boot-starter-test + test + diff --git a/lark/src/main/java/cn/hezhaohui/lark/Application.java b/lark/src/main/java/cn/hezhaohui/lark/Application.java index ac75828..3491645 100644 --- a/lark/src/main/java/cn/hezhaohui/lark/Application.java +++ b/lark/src/main/java/cn/hezhaohui/lark/Application.java @@ -1,11 +1,12 @@ package cn.hezhaohui.lark; +import cn.hezhaohui.lark.util.LarkUtil; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { - public static void main(String[] args) { + public static void main(String[] args) throws Exception { SpringApplication.run(Application.class, args); } } diff --git a/lark/src/main/java/cn/hezhaohui/lark/constant/AppConstant.java b/lark/src/main/java/cn/hezhaohui/lark/constant/AppConstant.java new file mode 100644 index 0000000..2e34296 --- /dev/null +++ b/lark/src/main/java/cn/hezhaohui/lark/constant/AppConstant.java @@ -0,0 +1,8 @@ +package cn.hezhaohui.lark.constant; + +public class AppConstant { + + public final static String APP_ID = "cli_a875a60984b5500c"; + public final static String APP_SECRET = "toAfmMddNEG9qWsYngs7wch0HwW4vAls"; + public final static String APP_USER_OPEN_ID = "ou_9eb248881be923bad599f342c22a4e7a"; +} diff --git a/lark/src/main/java/cn/hezhaohui/lark/util/LarkUtil.java b/lark/src/main/java/cn/hezhaohui/lark/util/LarkUtil.java new file mode 100644 index 0000000..b042d63 --- /dev/null +++ b/lark/src/main/java/cn/hezhaohui/lark/util/LarkUtil.java @@ -0,0 +1,54 @@ +package cn.hezhaohui.lark.util; + +import cn.hezhaohui.lark.constant.AppConstant; +import cn.hutool.core.util.IdUtil; +import cn.hutool.json.JSONUtil; +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; + +public class LarkUtil { + +// 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 sendText(String content) throws Exception { + // 构建client + Client client = Client.newBuilder(AppConstant.APP_ID, AppConstant.APP_SECRET).build(); + + // Json 包装 + Map text = new HashMap<>(); + text.put("text", content); + String textJson = JSONUtil.toJsonStr(text); + + // 创建请求对象 + CreateMessageReq req = CreateMessageReq.newBuilder() + .receiveIdType("open_id") + .createMessageReqBody(CreateMessageReqBody.newBuilder() + .receiveId(AppConstant.APP_USER_OPEN_ID) + .msgType("text") + .content(textJson) + .uuid(IdUtil.randomUUID()) + .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/src/test/java/cn/hezhaohui/lark/util/LarkUtilTest.java b/lark/src/test/java/cn/hezhaohui/lark/util/LarkUtilTest.java new file mode 100644 index 0000000..664196e --- /dev/null +++ b/lark/src/test/java/cn/hezhaohui/lark/util/LarkUtilTest.java @@ -0,0 +1,12 @@ +package cn.hezhaohui.lark.util; + + +import org.junit.jupiter.api.Test; + +class LarkUtilTest { + + @Test + void sendText() throws Exception { + LarkUtil.sendText("你好"); + } +} \ No newline at end of file diff --git a/pom.xml b/pom.xml index d5a7aed..f0f1de2 100644 --- a/pom.xml +++ b/pom.xml @@ -21,24 +21,21 @@ org.springframework.boot - spring-boot-starter-web - 3.2.2 - - - org.springframework.boot - spring-boot-maven-plugin - 3.2.2 - - - org.springframework.boot - spring-boot-starter-test + spring-boot-dependencies 3.2.2 + pom + import org.projectlombok lombok 1.18.30 + + cn.hutool + hutool-all + 5.8.9 + @@ -48,6 +45,7 @@ org.springframework.boot spring-boot-maven-plugin + 3.2.2