✨Feat: 实现天气消息卡片
This commit is contained in:
@@ -1,6 +1,5 @@
|
|||||||
package com.wonderland.stardewvalley.ai;
|
package com.wonderland.stardewvalley.ai;
|
||||||
|
|
||||||
import jakarta.annotation.Resource;
|
|
||||||
import org.springframework.ai.chat.client.ChatClient;
|
import org.springframework.ai.chat.client.ChatClient;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
|||||||
@@ -18,4 +18,9 @@ public class BackGroundConstant {
|
|||||||
"耕种 收获提升,户外活动(节日)比例增加,随机事件大幅增加";
|
"耕种 收获提升,户外活动(节日)比例增加,随机事件大幅增加";
|
||||||
public static final String BACKGROUND_WINTER_DESCRIPTION = "“寒冷的季节,也是心灵的沉淀”\n" +
|
public static final String BACKGROUND_WINTER_DESCRIPTION = "“寒冷的季节,也是心灵的沉淀”\n" +
|
||||||
"作息 影响健康值幅度增加,室内活动(节日)比例增加";
|
"作息 影响健康值幅度增加,室内活动(节日)比例增加";
|
||||||
|
|
||||||
|
public static final String BACKGROUND_WEATHER = "按照如下示例根据天气生成" +
|
||||||
|
"示例【晴天】阳光像金色的蜂蜜洒满星露谷!微风轻拂超舒服,快带上工具去田间劳作吧~ \uD83C\uDF31\n" +
|
||||||
|
"记得给作物喝饱水,今天的阳光能量满分,庄稼会长得飞快哦!" +
|
||||||
|
"【注意】你不需要输出天气本身";
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,4 +12,5 @@ public class Weather {
|
|||||||
private String windDirection;
|
private String windDirection;
|
||||||
private String reportTime;
|
private String reportTime;
|
||||||
private String humidity;
|
private String humidity;
|
||||||
|
private String content;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package com.wonderland.stardewvalley.lark;
|
|||||||
|
|
||||||
|
|
||||||
import cn.hutool.core.util.IdUtil;
|
import cn.hutool.core.util.IdUtil;
|
||||||
|
import cn.hutool.json.JSONUtil;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||||
import com.google.gson.JsonParser;
|
import com.google.gson.JsonParser;
|
||||||
@@ -10,11 +11,17 @@ import com.lark.oapi.core.utils.Jsons;
|
|||||||
import com.lark.oapi.service.im.v1.model.CreateMessageReq;
|
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.CreateMessageReqBody;
|
||||||
import com.lark.oapi.service.im.v1.model.CreateMessageResp;
|
import com.lark.oapi.service.im.v1.model.CreateMessageResp;
|
||||||
|
import com.wonderland.stardewvalley.ai.ChatUtil;
|
||||||
import com.wonderland.stardewvalley.constant.LarkConstant;
|
import com.wonderland.stardewvalley.constant.LarkConstant;
|
||||||
|
import com.wonderland.stardewvalley.entity.game.Weather;
|
||||||
|
import com.wonderland.stardewvalley.lark.card.CardTemplate;
|
||||||
|
import jakarta.annotation.Resource;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Service
|
@Service
|
||||||
@@ -23,6 +30,9 @@ public class LarkUtil {
|
|||||||
// 复制该 Demo 后, 需要将 "YOUR_APP_ID", "YOUR_APP_SECRET" 替换为自己应用的 APP_ID, APP_SECRET.
|
// 复制该 Demo 后, 需要将 "YOUR_APP_ID", "YOUR_APP_SECRET" 替换为自己应用的 APP_ID, APP_SECRET.
|
||||||
// 以下示例代码默认根据文档示例值填充,如果存在代码问题,请在 API 调试台填上相关必要参数后再复制代码使用
|
// 以下示例代码默认根据文档示例值填充,如果存在代码问题,请在 API 调试台填上相关必要参数后再复制代码使用
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private ChatUtil chatUtil;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 发送简单的文本消息
|
* 发送简单的文本消息
|
||||||
* @param content 发送的内容
|
* @param content 发送的内容
|
||||||
@@ -62,4 +72,55 @@ public class LarkUtil {
|
|||||||
// 业务数据处理
|
// 业务数据处理
|
||||||
System.out.println(Jsons.DEFAULT.toJson(resp.getData()));
|
System.out.println(Jsons.DEFAULT.toJson(resp.getData()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void sendWeatherCard(Weather weather) 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_WEATHER.getCardId());
|
||||||
|
templateData.put("template_version_name", CardTemplate.CARD_TEMPLATE_WEATHER.getVersion());
|
||||||
|
templateData.put("template_variable", weather);
|
||||||
|
// 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()));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,16 @@
|
|||||||
|
package com.wonderland.stardewvalley.lark.card;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
public enum CardTemplate {
|
||||||
|
CARD_TEMPLATE_WEATHER("AAqxas5etOJDx", "1.0.5");
|
||||||
|
|
||||||
|
private String cardId;
|
||||||
|
private String version;
|
||||||
|
|
||||||
|
CardTemplate(String cardId, String version) {
|
||||||
|
this.cardId = cardId;
|
||||||
|
this.version = version;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,16 +3,22 @@ package com.wonderland.stardewvalley.util;
|
|||||||
import cn.hutool.http.HttpUtil;
|
import cn.hutool.http.HttpUtil;
|
||||||
import cn.hutool.json.JSONObject;
|
import cn.hutool.json.JSONObject;
|
||||||
import cn.hutool.json.JSONUtil;
|
import cn.hutool.json.JSONUtil;
|
||||||
|
import com.wonderland.stardewvalley.ai.ChatUtil;
|
||||||
|
import com.wonderland.stardewvalley.constant.BackGroundConstant;
|
||||||
import com.wonderland.stardewvalley.constant.CityCode;
|
import com.wonderland.stardewvalley.constant.CityCode;
|
||||||
import com.wonderland.stardewvalley.entity.game.Weather;
|
import com.wonderland.stardewvalley.entity.game.Weather;
|
||||||
|
import jakarta.annotation.Resource;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
@Service
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class WeatherUtil {
|
public class WeatherUtil {
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 参考文档: https://lbs.amap.com/api/webservice/guide/api/weatherinfo
|
* 参考文档: https://lbs.amap.com/api/webservice/guide/api/weatherinfo
|
||||||
* URL 格式: https://restapi.amap.com/v3/weather/weatherInfo?parameters
|
* URL 格式: https://restapi.amap.com/v3/weather/weatherInfo?parameters
|
||||||
@@ -61,6 +67,7 @@ public class WeatherUtil {
|
|||||||
.reportTime(weatherData.getStr("reporttime"))
|
.reportTime(weatherData.getStr("reporttime"))
|
||||||
.humidity(weatherData.getStr("humidity"))
|
.humidity(weatherData.getStr("humidity"))
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
log.info("[Weather] Prepared: {}", weather);
|
log.info("[Weather] Prepared: {}", weather);
|
||||||
return weather;
|
return weather;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,13 @@
|
|||||||
package com.wonderland.stardewvalley.lark;
|
package com.wonderland.stardewvalley.lark;
|
||||||
|
|
||||||
|
import com.wonderland.stardewvalley.entity.game.Weather;
|
||||||
|
import com.wonderland.stardewvalley.util.WeatherUtil;
|
||||||
|
import jakarta.annotation.Resource;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
class LarkUtilTest {
|
class LarkUtilTest {
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void sendText() throws Exception {
|
void sendText() throws Exception {
|
||||||
LarkUtil.sendText("\uD83C\uDF31 在这个名为“如果人生是星露谷”的世界中,玩家扮演的是一位现实世界中的普通人 ,在某种“精神投射”或“虚拟现实”中,进入了一个与《星露谷物语》相似的模拟世界。这个世界并非简单的幻想乡,而是对现实生活的重构与映射 ,玩家通过在现实世界中“种田、打工、探索、运动”等行为,影响游戏中的生活节奏与心理状态。\n" +
|
LarkUtil.sendText("\uD83C\uDF31 在这个名为“如果人生是星露谷”的世界中,玩家扮演的是一位现实世界中的普通人 ,在某种“精神投射”或“虚拟现实”中,进入了一个与《星露谷物语》相似的模拟世界。这个世界并非简单的幻想乡,而是对现实生活的重构与映射 ,玩家通过在现实世界中“种田、打工、探索、运动”等行为,影响游戏中的生活节奏与心理状态。\n" +
|
||||||
@@ -15,4 +19,10 @@ class LarkUtilTest {
|
|||||||
"\n" +
|
"\n" +
|
||||||
"\uD83C\uDF3E 放下城市的喧嚣,放下心里的压力,拿起一杆锄头,和我们一起,去往一个叫“星露谷”的地方吧。");
|
"\uD83C\uDF3E 放下城市的喧嚣,放下心里的压力,拿起一杆锄头,和我们一起,去往一个叫“星露谷”的地方吧。");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void sendWeatherCard() throws Exception {
|
||||||
|
Weather weather = WeatherUtil.getWeather();
|
||||||
|
LarkUtil.sendWeatherCard(weather);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,15 +1,21 @@
|
|||||||
package com.wonderland.stardewvalley.util;
|
package com.wonderland.stardewvalley.util;
|
||||||
|
|
||||||
import com.wonderland.stardewvalley.entity.game.Weather;
|
import com.wonderland.stardewvalley.entity.game.Weather;
|
||||||
|
import jakarta.annotation.Resource;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
@SpringBootTest
|
||||||
class WeatherUtilTest {
|
class WeatherUtilTest {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private WeatherUtil weatherUtil;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void getWeather() {
|
void getWeather() {
|
||||||
Weather weather = WeatherUtil.getWeather();
|
Weather weather = weatherUtil.getWeather();
|
||||||
System.out.println(weather);
|
System.out.println(weather);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user