✨Feat: 完成 Lark 简单消息接收

This commit is contained in:
2025-10-25 20:40:54 +08:00
parent b900377f4f
commit c50c876a53
2 changed files with 39 additions and 0 deletions
@@ -1,5 +1,6 @@
package com.wonderland.stardewvalley; package com.wonderland.stardewvalley;
import com.wonderland.stardewvalley.lark.EventListener;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
@@ -7,5 +8,6 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
public class Application { public class Application {
public static void main(String[] args) { public static void main(String[] args) {
SpringApplication.run(Application.class, args); SpringApplication.run(Application.class, args);
EventListener.listen();
} }
} }
@@ -0,0 +1,37 @@
package com.wonderland.stardewvalley.lark;
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 com.wonderland.stardewvalley.constant.LarkConstant;
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("p2p_chat_create", 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 listen() {
Client cli = new Client.Builder(LarkConstant.APP_ID, LarkConstant.APP_SECRET)
.eventHandler(EVENT_HANDLER)
.build();
cli.start();
}
}