Compare commits
3
Commits
a904db5d06
...
1d7e6a9d6e
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1d7e6a9d6e | ||
|
|
c5e3bb47a8 | ||
|
|
c5d3b9b996 |
@@ -0,0 +1,3 @@
|
|||||||
|
.idea
|
||||||
|
*.iml
|
||||||
|
/target
|
||||||
@@ -1,3 +1,31 @@
|
|||||||
# lark-webhook-sender
|
# lark-webhook-sender
|
||||||
|
|
||||||
🤖 基于飞书 Webhook 实现仓库代码更新 | AI 摘要
|
🤖 基于飞书 Webhook 实现仓库代码更新 | AI 摘要
|
||||||
|
|
||||||
|
> [飞书开放平台 - 服务端API - 事件概述](https://open.feishu.cn/document/server-docs/event-subscription-guide/overview)
|
||||||
|
|
||||||
|
```mermaid
|
||||||
|
sequenceDiagram
|
||||||
|
participant Gitea as Gitea 仓库
|
||||||
|
participant Service as 你的服务 (Java)
|
||||||
|
participant AISummary as AI摘要服务
|
||||||
|
participant Lark as 飞书机器人
|
||||||
|
|
||||||
|
Gitea->>Service: 1. Git push事件触发
|
||||||
|
Service->>Service: 2. 验证Gitea请求(必须!)
|
||||||
|
Service-->>Service: 2.1 检查 X-Gitea-Event: push
|
||||||
|
Service-->>Service: 2.2 用 SECRET 校验 signature (HMAC-SHA256)
|
||||||
|
alt 验证通过
|
||||||
|
Service->>Service: 3. 解析Gitea Payload
|
||||||
|
Service-->>Service: 3.1 示例:
|
||||||
|
Service-->>Service: { "ref": "main", "commits": [{"message": "feat: ..."}] }
|
||||||
|
Service->>AISummary: 4. 调用AI摘要服务
|
||||||
|
AISummary->>Service: 4.1 返回总结结果
|
||||||
|
Service->>Lark: 5. 发送飞书机器人通知
|
||||||
|
Lark->>Lark: 5.1 格式化:
|
||||||
|
Lark->>Lark: { "msg_type": "text", "content": "【代码更新】feat: ... [AI总结]" }
|
||||||
|
Service-->>Gitea: 6. 返回 HTTP 200
|
||||||
|
else 验证失败
|
||||||
|
Service-->>Gitea: 6. 返回 HTTP 401
|
||||||
|
end
|
||||||
|
```
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
<?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-webhook-sender</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<artifactId>gitea-webhook</artifactId>
|
||||||
|
|
||||||
|
<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,29 @@
|
|||||||
|
<?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-webhook-sender</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<artifactId>lark-sender</artifactId>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.source>21</maven.compiler.source>
|
||||||
|
<maven.compiler.target>21</maven.compiler.target>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<!-- https://mvnrepository.com/artifact/com.larksuite.oapi/oapi-sdk -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.larksuite.oapi</groupId>
|
||||||
|
<artifactId>oapi-sdk</artifactId>
|
||||||
|
<version>2.4.16</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</project>
|
||||||
@@ -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";
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
package cn.hezhaohui.lark.constant;
|
||||||
|
|
||||||
|
public class UserConstant {
|
||||||
|
public static final String USER_OPEN_ID = "ou_728072d781317a402a0ba6b6d30390a6";
|
||||||
|
}
|
||||||
@@ -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<String, String> 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()));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package cn.hezhaohui.lark.util;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class LarkTest {
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void sendTextMessage() throws Exception {
|
||||||
|
Lark.sendTextMessage("测试消息");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
<?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-webhook-sender</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
<packaging>pom</packaging>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-parent</artifactId>
|
||||||
|
<version>3.2.2</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<modules>
|
||||||
|
<module>lark-sender</module>
|
||||||
|
<module>gitea-webhook</module>
|
||||||
|
</modules>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.source>21</maven.compiler.source>
|
||||||
|
<maven.compiler.target>21</maven.compiler.target>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>junit</groupId>
|
||||||
|
<artifactId>junit</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
</project>
|
||||||
Reference in New Issue
Block a user