✨Feat: 实现消息卡片推送

This commit is contained in:
2025-10-29 16:38:56 +08:00
parent e7ce159ec4
commit f964515038
2 changed files with 40 additions and 6 deletions
@@ -1,14 +1,18 @@
package cn.hezhaohui.lark.handler;
import cn.hezhaohui.common.AppConstant;
import com.lark.oapi.core.request.EventReq;
import cn.hezhaohui.common.CardEnum;
import cn.hezhaohui.entity.Weather;
import cn.hezhaohui.lark.client.WeatherClient;
import cn.hezhaohui.lark.util.BeanUtil;
import cn.hezhaohui.lark.util.LarkUtil;
import com.lark.oapi.core.utils.Jsons;
import com.lark.oapi.event.CustomEventHandler;
import com.lark.oapi.event.EventDispatcher;
import com.lark.oapi.service.application.ApplicationService;
import com.lark.oapi.service.application.v6.model.P2BotMenuV6;
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 {
@@ -20,10 +24,15 @@ public class EventListener {
System.out.printf("[ onP2MessageReceiveV1 access ], data: %s\n", Jsons.DEFAULT.toJson(event.getEvent()));
}
})
.onCustomizedEvent("这里填入你要自定义订阅的 event 的 key,例如 out_approval", new CustomEventHandler() {
.onP2BotMenuV6(new ApplicationService.P2BotMenuV6Handler() {
@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));
public void handle(P2BotMenuV6 event) throws Exception {
String eventKey = event.getEvent().getEventKey();
if (eventKey.equals("weather")) {
WeatherClient weatherClient = BeanUtil.getBean(WeatherClient.class);
Weather weather = weatherClient.getWeather();
LarkUtil.sendCard(CardEnum.CARD_WEATHER, weather);
}
}
})
.build();
@@ -0,0 +1,25 @@
package cn.hezhaohui.lark.util;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
@Component
public class BeanUtil implements ApplicationContextAware {
private static ApplicationContext context;
public static <T> T getBean(Class<T> beanClass) {
return context.getBean(beanClass);
}
public static <T> T getBean(String name, Class<T> beanClass) {
return context.getBean(name, beanClass);
}
public static Object getBean(String name) {
return context.getBean(name);
}
@Override
public void setApplicationContext(ApplicationContext appContext) {
context = appContext;
}
}