Compare commits

..
6 Commits
Author SHA1 Message Date
wonder e6111188c1 ✨Feat: 菜单板块 2025-10-26 16:34:16 +08:00
wonder ea8c18b18b ✨Feat: 垂钓板块 2025-10-26 16:21:31 +08:00
wonder 5ddf362834 ✨Feat: 耕种板块 2025-10-26 16:18:33 +08:00
wonder dbee3a5b12 ✨Feat: 实现菜单推送 2025-10-26 16:07:38 +08:00
wonder 2372b7ebae 🔧Chore: 部分代码补充 2025-10-26 15:32:39 +08:00
wonder d4afe0e923 ✨Feat: 完成天气卡片 2025-10-26 14:35:08 +08:00
7 changed files with 362 additions and 8 deletions
@@ -19,8 +19,9 @@ public class BackGroundConstant {
public static final String BACKGROUND_WINTER_DESCRIPTION = "“寒冷的季节,也是心灵的沉淀”\n" +
"作息 影响健康值幅度增加,室内活动(节日)比例增加";
public static final String BACKGROUND_WEATHER = "按照如下示例根据天气生成" +
"示例【晴天】阳光像金色的蜂蜜洒满星露谷!微风轻拂超舒服,快带上工具去田间劳作吧~ \uD83C\uDF31\n" +
"记得给作物喝饱水,今天的阳光能量满分,庄稼会长得飞快哦!" +
"【注意】你不需要输出天气本身";
public static final String BACKGROUND_WEATHER = "【要求】请根据传入的今日数据,采用符合的语气,emjoy和符号,带一点想象,用简洁的语句,描述一下想象中的风景,体感,并附带一点小Tips。\n" +
"【输出格式】今天的星露谷....(控制在30字)\\nTips。\n" +
"【输出示例】\n" +
"今天的星露谷天气阴沉,气温舒适,北风轻轻吹拂,风力微弱,湿度较高,仿佛连空气都带着一丝凉意。这种天气最适合在家中泡一杯热茶,或者在庭院里摆上几张藤椅,享受难得的宁静。\n" +
"Tips:可以尝试在这样的天气里种植一些耐阴植物,比如蕨类或苔藓,为你的农场增添一抹绿意哦!\uD83D\uDE09\uD83C\uDF31";
}
@@ -0,0 +1,20 @@
package com.wonderland.stardewvalley.handle;
import com.wonderland.stardewvalley.entity.game.Weather;
import com.wonderland.stardewvalley.util.lark.LarkUtil;
import com.wonderland.stardewvalley.util.weather.WeatherUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;
@Component
public class WeatherHandle {
@Resource
private LarkUtil larkUtil;
public void sendCard() throws Exception {
Weather weather = WeatherUtil.getWeather();
larkUtil.sendWeatherCard(weather);
}
}
@@ -4,7 +4,12 @@ import lombok.Getter;
@Getter
public enum CardTemplate {
CARD_TEMPLATE_WEATHER("AAqxas5etOJDx", "1.0.5");
CARD_TEMPLATE_WEATHER("AAqxas5etOJDx", "1.0.5"),
CARD_TEMPLATE_FARM("AAqxaYo9rbXPa", "1.0.8"),
CARD_TEMPLATE_FISH("AAqxauQ2FQuSF", "1.0.4"),
CARD_TEMPLATE_MINE("AAqxarffytU2e", "1.0.4"),
CARD_TEMPLATE_HELP("AAqxaCRidel6g", "1.0.5"),
CARD_TEMPLATE_LUCK("AAqxaScwjaUB3", "1.0.2");
private String cardId;
private String version;
@@ -0,0 +1,25 @@
package com.wonderland.stardewvalley.util.bean;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
@Component
public class SpringContextUtil 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) throws BeansException {
context = appContext;
}
}
@@ -8,6 +8,8 @@ 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.common.constant.LarkConstant;
import com.wonderland.stardewvalley.handle.WeatherHandle;
import com.wonderland.stardewvalley.util.bean.SpringContextUtil;
public class EventListener {
@@ -30,6 +32,22 @@ public class EventListener {
.onP2BotMenuV6(new ApplicationService.P2BotMenuV6Handler() {
@Override
public void handle(P2BotMenuV6 event) throws Exception {
String eventKey = event.getEvent().getEventKey();
if (eventKey.equals("weather")) {
WeatherHandle weatherHandle = SpringContextUtil.getBean(WeatherHandle.class);
weatherHandle.sendCard();
} else if (eventKey.equals("farm")) {
LarkUtil.sendFarmCard();
} else if (eventKey.equals("fish")) {
LarkUtil.sendFishCard();
} else if (eventKey.equals("mine")) {
LarkUtil.sendMineCard();
} else if (eventKey.equals("help")) {
LarkUtil.sendHelpCard();
} else if (eventKey.equals("luck")) {
LarkUtil.sendLuckCard();
}
System.out.printf("[ onP2BotMenuV6 access ], data: %s\n", Jsons.DEFAULT.toJson(event.getEvent()));
}
})
@@ -10,10 +10,11 @@ import com.lark.oapi.core.utils.Jsons;
import com.lark.oapi.service.im.v1.model.CreateMessageReq;
import com.lark.oapi.service.im.v1.model.CreateMessageReqBody;
import com.lark.oapi.service.im.v1.model.CreateMessageResp;
import com.wonderland.stardewvalley.service.ai.ChatUtil;
import com.wonderland.stardewvalley.common.constant.BackGroundConstant;
import com.wonderland.stardewvalley.common.constant.LarkConstant;
import com.wonderland.stardewvalley.entity.game.Weather;
import com.wonderland.stardewvalley.lark.card.CardTemplate;
import com.wonderland.stardewvalley.service.ai.ChatUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
@@ -29,6 +30,10 @@ public class LarkUtil {
// 复制该 Demo 后, 需要将 "YOUR_APP_ID", "YOUR_APP_SECRET" 替换为自己应用的 APP_ID, APP_SECRET.
// 以下示例代码默认根据文档示例值填充,如果存在代码问题,请在 API 调试台填上相关必要参数后再复制代码使用
@Resource
private ChatUtil chatUtil;
/**
* 发送简单的文本消息
* @param content 发送的内容
@@ -69,7 +74,7 @@ public class LarkUtil {
System.out.println(Jsons.DEFAULT.toJson(resp.getData()));
}
public static void sendWeatherCard(Weather weather) throws Exception {
public void sendWeatherCard(Weather weather) throws Exception {
// 构建client
Client client = Client.newBuilder(LarkConstant.APP_ID, LarkConstant.APP_SECRET).build();
@@ -82,6 +87,11 @@ public class LarkUtil {
// 构建卡片:对于不同的卡片,只需要给出不同的 模板id 版本号 变量字段
String content = chatUtil.doChat(BackGroundConstant.BACKGROUND_WEATHER, weather.toString());
weather.setContent(content);
log.info("[LarkUtil] Set Content via LLM: {}", content);
Map<String, Object> templateData = new HashMap<>();
templateData.put("template_id", CardTemplate.CARD_TEMPLATE_WEATHER.getCardId());
templateData.put("template_version_name", CardTemplate.CARD_TEMPLATE_WEATHER.getVersion());
@@ -119,4 +129,274 @@ public class LarkUtil {
// 业务数据处理
System.out.println(Jsons.DEFAULT.toJson(resp.getData()));
}
public static void sendFarmCard() throws Exception {
// 构建client
Client client = Client.newBuilder(LarkConstant.APP_ID, LarkConstant.APP_SECRET).build();
// 生成uuid
String uuid = IdUtil.randomUUID().toString();
// 卡片参数
// String textParam = JSONUtil.toJsonStr(weather);
// log.info(textParam);
// 构建卡片:对于不同的卡片,只需要给出不同的 模板id 版本号 变量字段
Map<String, Object> templateData = new HashMap<>();
templateData.put("template_id", CardTemplate.CARD_TEMPLATE_FARM.getCardId());
templateData.put("template_version_name", CardTemplate.CARD_TEMPLATE_FARM.getVersion());
// templateData.put("template_variable", "");
// String jsonCardContent = Jsons.DEFAULT.toJson(templateData);
Map<String, Object> jsonContent = new HashMap<>();
jsonContent.put("data", templateData);
jsonContent.put("type", "template");
String json = Jsons.DEFAULT.toJson(jsonContent);
log.info(json);
// 创建请求对象
CreateMessageReq req = CreateMessageReq.newBuilder()
.receiveIdType("open_id")
.createMessageReqBody(CreateMessageReqBody.newBuilder()
.receiveId(LarkConstant.USER_OPEN_ID)
.msgType("interactive")
.content(json)
.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()));
}
public static void sendFishCard() throws Exception {
// 构建client
Client client = Client.newBuilder(LarkConstant.APP_ID, LarkConstant.APP_SECRET).build();
// 生成uuid
String uuid = IdUtil.randomUUID().toString();
// 卡片参数
// String textParam = JSONUtil.toJsonStr(weather);
// log.info(textParam);
// 构建卡片:对于不同的卡片,只需要给出不同的 模板id 版本号 变量字段
Map<String, Object> templateData = new HashMap<>();
templateData.put("template_id", CardTemplate.CARD_TEMPLATE_FISH.getCardId());
templateData.put("template_version_name", CardTemplate.CARD_TEMPLATE_FISH.getVersion());
// templateData.put("template_variable", "");
// String jsonCardContent = Jsons.DEFAULT.toJson(templateData);
Map<String, Object> jsonContent = new HashMap<>();
jsonContent.put("data", templateData);
jsonContent.put("type", "template");
String json = Jsons.DEFAULT.toJson(jsonContent);
log.info(json);
// 创建请求对象
CreateMessageReq req = CreateMessageReq.newBuilder()
.receiveIdType("open_id")
.createMessageReqBody(CreateMessageReqBody.newBuilder()
.receiveId(LarkConstant.USER_OPEN_ID)
.msgType("interactive")
.content(json)
.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()));
}
public static void sendMineCard() throws Exception {
// 构建client
Client client = Client.newBuilder(LarkConstant.APP_ID, LarkConstant.APP_SECRET).build();
// 生成uuid
String uuid = IdUtil.randomUUID().toString();
// 卡片参数
// String textParam = JSONUtil.toJsonStr(weather);
// log.info(textParam);
// 构建卡片:对于不同的卡片,只需要给出不同的 模板id 版本号 变量字段
Map<String, Object> templateData = new HashMap<>();
templateData.put("template_id", CardTemplate.CARD_TEMPLATE_MINE.getCardId());
templateData.put("template_version_name", CardTemplate.CARD_TEMPLATE_MINE.getVersion());
// templateData.put("template_variable", "");
// String jsonCardContent = Jsons.DEFAULT.toJson(templateData);
Map<String, Object> jsonContent = new HashMap<>();
jsonContent.put("data", templateData);
jsonContent.put("type", "template");
String json = Jsons.DEFAULT.toJson(jsonContent);
log.info(json);
// 创建请求对象
CreateMessageReq req = CreateMessageReq.newBuilder()
.receiveIdType("open_id")
.createMessageReqBody(CreateMessageReqBody.newBuilder()
.receiveId(LarkConstant.USER_OPEN_ID)
.msgType("interactive")
.content(json)
.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()));
}
public static void sendHelpCard() throws Exception {
// 构建client
Client client = Client.newBuilder(LarkConstant.APP_ID, LarkConstant.APP_SECRET).build();
// 生成uuid
String uuid = IdUtil.randomUUID().toString();
// 卡片参数
// String textParam = JSONUtil.toJsonStr(weather);
// log.info(textParam);
// 构建卡片:对于不同的卡片,只需要给出不同的 模板id 版本号 变量字段
Map<String, Object> templateData = new HashMap<>();
templateData.put("template_id", CardTemplate.CARD_TEMPLATE_HELP.getCardId());
templateData.put("template_version_name", CardTemplate.CARD_TEMPLATE_HELP.getVersion());
// templateData.put("template_variable", "");
// String jsonCardContent = Jsons.DEFAULT.toJson(templateData);
Map<String, Object> jsonContent = new HashMap<>();
jsonContent.put("data", templateData);
jsonContent.put("type", "template");
String json = Jsons.DEFAULT.toJson(jsonContent);
log.info(json);
// 创建请求对象
CreateMessageReq req = CreateMessageReq.newBuilder()
.receiveIdType("open_id")
.createMessageReqBody(CreateMessageReqBody.newBuilder()
.receiveId(LarkConstant.USER_OPEN_ID)
.msgType("interactive")
.content(json)
.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()));
}
public static void sendLuckCard() throws Exception {
// 构建client
Client client = Client.newBuilder(LarkConstant.APP_ID, LarkConstant.APP_SECRET).build();
// 生成uuid
String uuid = IdUtil.randomUUID().toString();
// 卡片参数
// String textParam = JSONUtil.toJsonStr(weather);
// log.info(textParam);
// 构建卡片:对于不同的卡片,只需要给出不同的 模板id 版本号 变量字段
Map<String, Object> templateData = new HashMap<>();
templateData.put("template_id", CardTemplate.CARD_TEMPLATE_LUCK.getCardId());
templateData.put("template_version_name", CardTemplate.CARD_TEMPLATE_LUCK.getVersion());
// templateData.put("template_variable", "");
// String jsonCardContent = Jsons.DEFAULT.toJson(templateData);
Map<String, Object> jsonContent = new HashMap<>();
jsonContent.put("data", templateData);
jsonContent.put("type", "template");
String json = Jsons.DEFAULT.toJson(jsonContent);
log.info(json);
// 创建请求对象
CreateMessageReq req = CreateMessageReq.newBuilder()
.receiveIdType("open_id")
.createMessageReqBody(CreateMessageReqBody.newBuilder()
.receiveId(LarkConstant.USER_OPEN_ID)
.msgType("interactive")
.content(json)
.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()));
}
}
@@ -3,10 +3,15 @@ package com.wonderland.stardewvalley.lark;
import com.wonderland.stardewvalley.entity.game.Weather;
import com.wonderland.stardewvalley.util.weather.WeatherUtil;
import com.wonderland.stardewvalley.util.lark.LarkUtil;
import jakarta.annotation.Resource;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class LarkUtilTest {
@Resource
private LarkUtil larkUtil;
@Test
void sendText() throws Exception {
@@ -23,6 +28,6 @@ class LarkUtilTest {
@Test
void sendWeatherCard() throws Exception {
Weather weather = WeatherUtil.getWeather();
LarkUtil.sendWeatherCard(weather);
larkUtil.sendWeatherCard(weather);
}
}