diff --git a/src/main/java/com/wonderland/stardewvalley/Application.java b/src/main/java/com/wonderland/stardewvalley/Application.java index 5474a78..c2c9a6f 100644 --- a/src/main/java/com/wonderland/stardewvalley/Application.java +++ b/src/main/java/com/wonderland/stardewvalley/Application.java @@ -1,5 +1,6 @@ package com.wonderland.stardewvalley; +import com.wonderland.stardewvalley.lark.EventListener; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @@ -7,5 +8,6 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); + EventListener.listen(); } } diff --git a/src/main/java/com/wonderland/stardewvalley/lark/EventListener.java b/src/main/java/com/wonderland/stardewvalley/lark/EventListener.java new file mode 100644 index 0000000..fae678f --- /dev/null +++ b/src/main/java/com/wonderland/stardewvalley/lark/EventListener.java @@ -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(); + } +}