From 6c53f352dff7ac5e9b1a2fd2603bb4e4a219e73c Mon Sep 17 00:00:00 2001 From: Wonder Date: Mon, 27 Oct 2025 16:33:47 +0800 Subject: [PATCH] =?UTF-8?q?=20=E2=9C=A8Feat:=20=E5=AE=9E=E7=8E=B0=E4=BA=8B?= =?UTF-8?q?=E4=BB=B6=E8=AE=A2=E9=98=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/cn/hezhaohui/lark/Application.java | 3 ++ .../hezhaohui/lark/handler/EventListener.java | 38 +++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 lark/src/main/java/cn/hezhaohui/lark/handler/EventListener.java diff --git a/lark/src/main/java/cn/hezhaohui/lark/Application.java b/lark/src/main/java/cn/hezhaohui/lark/Application.java index 3491645..8e467a0 100644 --- a/lark/src/main/java/cn/hezhaohui/lark/Application.java +++ b/lark/src/main/java/cn/hezhaohui/lark/Application.java @@ -4,9 +4,12 @@ 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(); } } diff --git a/lark/src/main/java/cn/hezhaohui/lark/handler/EventListener.java b/lark/src/main/java/cn/hezhaohui/lark/handler/EventListener.java new file mode 100644 index 0000000..5685f9a --- /dev/null +++ b/lark/src/main/java/cn/hezhaohui/lark/handler/EventListener.java @@ -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(); + } + +}