Compare commits

...
3 Commits
Author SHA1 Message Date
wonder 6c53f352df ✨Feat: 实现事件订阅 2025-10-27 16:33:47 +08:00
wonder 5426df2292 ✨Feat: 实现简单消息发送 2025-10-27 16:11:07 +08:00
wonder 8b2d3a409c 🔧Chore: 项目初始化 2025-10-27 15:02:08 +08:00
8 changed files with 232 additions and 0 deletions
+2
View File
@@ -0,0 +1,2 @@
.idea
*/target
+44
View File
@@ -0,0 +1,44 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>cn.hezhaohui</groupId>
<artifactId>lark-panel</artifactId>
<version>1.0.0</version>
</parent>
<artifactId>lark</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.larksuite.oapi</groupId>
<artifactId>oapi-sdk</artifactId>
<version>2.4.24</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
@@ -0,0 +1,15 @@
package cn.hezhaohui.lark;
import cn.hezhaohui.lark.util.LarkUtil;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import static cn.hezhaohui.lark.handler.EventListener.larkClientStart;
@SpringBootApplication
public class Application {
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
larkClientStart();
}
}
@@ -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";
}
@@ -0,0 +1,38 @@
package cn.hezhaohui.lark.handler;
import cn.hezhaohui.lark.constant.AppConstant;
import com.lark.oapi.core.request.EventReq;
import com.lark.oapi.core.utils.Jsons;
import com.lark.oapi.event.CustomEventHandler;
import com.lark.oapi.event.EventDispatcher;
import com.lark.oapi.service.im.ImService;
import com.lark.oapi.service.im.v1.model.P2MessageReceiveV1;
import com.lark.oapi.ws.Client;
import java.nio.charset.StandardCharsets;
public class EventListener {
// onP2MessageReceiveV1 为接收消息 v2.0;onCustomizedEvent 内的 message 为接收消息 v1.0。
private static final EventDispatcher EVENT_HANDLER = EventDispatcher.newBuilder("", "")
.onP2MessageReceiveV1(new ImService.P2MessageReceiveV1Handler() {
@Override
public void handle(P2MessageReceiveV1 event) throws Exception {
System.out.printf("[ onP2MessageReceiveV1 access ], data: %s\n", Jsons.DEFAULT.toJson(event.getEvent()));
}
})
.onCustomizedEvent("这里填入你要自定义订阅的 event 的 key,例如 out_approval", new CustomEventHandler() {
@Override
public void handle(EventReq event) throws Exception {
System.out.printf("[ onCustomizedEvent access ], type: message, data: %s\n", new String(event.getBody(), StandardCharsets.UTF_8));
}
})
.build();
public static void larkClientStart() {
Client cli = new Client.Builder(AppConstant.APP_ID, AppConstant.APP_SECRET)
.eventHandler(EVENT_HANDLER)
.build();
cli.start();
}
}
@@ -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<String, String> 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()));
}
}
@@ -0,0 +1,12 @@
package cn.hezhaohui.lark.util;
import org.junit.jupiter.api.Test;
class LarkUtilTest {
@Test
void sendText() throws Exception {
LarkUtil.sendText("你好");
}
}
+59
View File
@@ -0,0 +1,59 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- 项目基本信息 -->
<groupId>cn.hezhaohui</groupId>
<artifactId>lark-panel</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>
<name>all-in-one panel</name>
<!-- 子模块 -->
<modules>
<module>lark</module>
</modules>
<!-- 依赖管理 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>3.2.2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.9</version>
</dependency>
</dependencies>
</dependencyManagement>
<!-- 构建配置 -->
<build>
<plugins>
<!-- 编译插件 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>3.2.2</version>
</plugin>
</plugins>
</build>
<!-- Maven 属性 -->
<properties>
<java.version>21</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
</project>