diff --git a/common/pom.xml b/common/pom.xml new file mode 100644 index 0000000..e488d04 --- /dev/null +++ b/common/pom.xml @@ -0,0 +1,20 @@ + + + 4.0.0 + + cn.hezhaohui + lark-panel + 1.0.0 + + + common + + + 21 + 21 + UTF-8 + + + \ No newline at end of file diff --git a/common/src/main/java/cn/hezhaohui/common/AIConstant.java b/common/src/main/java/cn/hezhaohui/common/AIConstant.java new file mode 100644 index 0000000..bdbf706 --- /dev/null +++ b/common/src/main/java/cn/hezhaohui/common/AIConstant.java @@ -0,0 +1,12 @@ +package cn.hezhaohui.common; + +public class AIConstant { + + public static final String SYSTEM_PROMPT = "请根据输入的 [BackGround] 和 [Content] 生成内容"; + + public static final String BACKGROUND_WEATHER = "【要求】请根据传入的今日数据,采用符合的语气,emjoy和符号,带一点想象,用简洁的语句,描述一下想象中的风景,体感,并附带一点小Tips。\n" + + "【输出格式】今天的星露谷....(控制在30字)\\nTips。\n" + + "【输出示例】\n" + + "今天的星露谷天气阴沉,气温舒适,北风轻轻吹拂,风力微弱,湿度较高,仿佛连空气都带着一丝凉意。这种天气最适合在家中泡一杯热茶,或者在庭院里摆上几张藤椅,享受难得的宁静。\n" + + "Tips:可以尝试在这样的天气里种植一些耐阴植物,比如蕨类或苔藓,为你的农场增添一抹绿意哦!\uD83D\uDE09\uD83C\uDF31"; +} diff --git a/lark/src/main/java/cn/hezhaohui/lark/constant/AppConstant.java b/common/src/main/java/cn/hezhaohui/common/AppConstant.java similarity index 88% rename from lark/src/main/java/cn/hezhaohui/lark/constant/AppConstant.java rename to common/src/main/java/cn/hezhaohui/common/AppConstant.java index 2e34296..8578e4a 100644 --- a/lark/src/main/java/cn/hezhaohui/lark/constant/AppConstant.java +++ b/common/src/main/java/cn/hezhaohui/common/AppConstant.java @@ -1,4 +1,4 @@ -package cn.hezhaohui.lark.constant; +package cn.hezhaohui.common; public class AppConstant { diff --git a/common/src/main/java/cn/hezhaohui/common/CardConstant.java b/common/src/main/java/cn/hezhaohui/common/CardConstant.java new file mode 100644 index 0000000..a0a8a14 --- /dev/null +++ b/common/src/main/java/cn/hezhaohui/common/CardConstant.java @@ -0,0 +1,8 @@ +package cn.hezhaohui.common; + +public class CardConstant { + + public static final String CARD_WEATHER_ID = "AAqxas5etOJDx"; + public static final String CARD_WEATHER_VERSION = "1.0.5"; + +} diff --git a/common/src/main/java/cn/hezhaohui/common/WeatherConstant.java b/common/src/main/java/cn/hezhaohui/common/WeatherConstant.java new file mode 100644 index 0000000..9ebc7a9 --- /dev/null +++ b/common/src/main/java/cn/hezhaohui/common/WeatherConstant.java @@ -0,0 +1,8 @@ +package cn.hezhaohui.common; + +public class WeatherConstant { + + public static final String API_BASE_URL = "https://restapi.amap.com/v3/weather/weatherInfo"; + public static final String API_SECRET = "d0f5599ac19a5e4553ba68cd1781d2df"; + public static final String CITY_CODE_WUHAN = "420100"; +} diff --git a/lark/pom.xml b/lark/pom.xml index a0bdede..381b254 100644 --- a/lark/pom.xml +++ b/lark/pom.xml @@ -33,6 +33,12 @@ spring-boot-starter-test test + + cn.hezhaohui + common + 1.0.0 + compile + diff --git a/lark/src/main/java/cn/hezhaohui/lark/Application.java b/lark/src/main/java/cn/hezhaohui/lark/Application.java index 8e467a0..3710db5 100644 --- a/lark/src/main/java/cn/hezhaohui/lark/Application.java +++ b/lark/src/main/java/cn/hezhaohui/lark/Application.java @@ -1,6 +1,5 @@ package cn.hezhaohui.lark; -import cn.hezhaohui.lark.util.LarkUtil; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; diff --git a/lark/src/main/java/cn/hezhaohui/lark/handler/EventListener.java b/lark/src/main/java/cn/hezhaohui/lark/handler/EventListener.java index 5685f9a..a09c1e3 100644 --- a/lark/src/main/java/cn/hezhaohui/lark/handler/EventListener.java +++ b/lark/src/main/java/cn/hezhaohui/lark/handler/EventListener.java @@ -1,6 +1,6 @@ package cn.hezhaohui.lark.handler; -import cn.hezhaohui.lark.constant.AppConstant; +import cn.hezhaohui.common.AppConstant; import com.lark.oapi.core.request.EventReq; import com.lark.oapi.core.utils.Jsons; import com.lark.oapi.event.CustomEventHandler; diff --git a/lark/src/main/java/cn/hezhaohui/lark/util/LarkUtil.java b/lark/src/main/java/cn/hezhaohui/lark/util/LarkUtil.java index b042d63..3dc0e58 100644 --- a/lark/src/main/java/cn/hezhaohui/lark/util/LarkUtil.java +++ b/lark/src/main/java/cn/hezhaohui/lark/util/LarkUtil.java @@ -1,6 +1,6 @@ package cn.hezhaohui.lark.util; -import cn.hezhaohui.lark.constant.AppConstant; +import cn.hezhaohui.common.AppConstant; import cn.hutool.core.util.IdUtil; import cn.hutool.json.JSONUtil; import com.google.gson.JsonParser; @@ -51,4 +51,8 @@ public class LarkUtil { // 业务数据处理 System.out.println(Jsons.DEFAULT.toJson(resp.getData())); } + + public static void sendCard(String cardId, String dataJson) { + // TODO + } } diff --git a/pom.xml b/pom.xml index f0f1de2..eaee4e6 100644 --- a/pom.xml +++ b/pom.xml @@ -14,6 +14,8 @@ lark + weather + common diff --git a/weather/pom.xml b/weather/pom.xml new file mode 100644 index 0000000..b5ab855 --- /dev/null +++ b/weather/pom.xml @@ -0,0 +1,46 @@ + + + 4.0.0 + + cn.hezhaohui + lark-panel + 1.0.0 + + + weather + + + + org.springframework.ai + spring-ai-starter-model-openai + 1.0.3 + + + org.projectlombok + lombok + + + cn.hutool + hutool-all + 5.8.27 + + + cn.hezhaohui + common + 1.0.0 + + + org.springframework.boot + spring-boot-starter-test + + + + + 21 + 21 + UTF-8 + + + \ No newline at end of file diff --git a/weather/src/main/java/cn/hezhaohui/Application.java b/weather/src/main/java/cn/hezhaohui/Application.java new file mode 100644 index 0000000..c792b2d --- /dev/null +++ b/weather/src/main/java/cn/hezhaohui/Application.java @@ -0,0 +1,11 @@ +package cn.hezhaohui; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class Application { + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } +} diff --git a/weather/src/main/java/cn/hezhaohui/entity/Weather.java b/weather/src/main/java/cn/hezhaohui/entity/Weather.java new file mode 100644 index 0000000..65411a5 --- /dev/null +++ b/weather/src/main/java/cn/hezhaohui/entity/Weather.java @@ -0,0 +1,16 @@ +package cn.hezhaohui.entity; + +import lombok.Builder; +import lombok.Data; + +@Data +@Builder +public class Weather { + private String weather; + private String temperature; + private String windPower; + private String windDirection; + private String reportTime; + private String humidity; + private String content; +} diff --git a/weather/src/main/java/cn/hezhaohui/service/ChatService.java b/weather/src/main/java/cn/hezhaohui/service/ChatService.java new file mode 100644 index 0000000..7cd61ac --- /dev/null +++ b/weather/src/main/java/cn/hezhaohui/service/ChatService.java @@ -0,0 +1,30 @@ +package cn.hezhaohui.service; + +import cn.hezhaohui.common.AIConstant; +import lombok.extern.slf4j.Slf4j; +import org.springframework.ai.chat.client.ChatClient; +import org.springframework.stereotype.Service; + +@Slf4j +@Service +public class ChatService { + + private final ChatClient chatClient; + + public ChatService(ChatClient.Builder chatClientBuilder) { + this.chatClient = chatClientBuilder.build(); + } + + + public String doChat(String background, String content) { + String ans = chatClient + .prompt() + .system(AIConstant.SYSTEM_PROMPT) + .user("[BackGround]: " + background + "\n" + "[Content]: " + content) + .call() + .content(); + log.info(ans); + return ans; + } + +} diff --git a/weather/src/main/java/cn/hezhaohui/util/SpringContextUtil.java b/weather/src/main/java/cn/hezhaohui/util/SpringContextUtil.java new file mode 100644 index 0000000..34be83b --- /dev/null +++ b/weather/src/main/java/cn/hezhaohui/util/SpringContextUtil.java @@ -0,0 +1,21 @@ +package cn.hezhaohui.util; + +import org.springframework.beans.BeansException; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; +import org.springframework.stereotype.Service; + +@Service +public class SpringContextUtil implements ApplicationContextAware { + private static ApplicationContext context; + @Override + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { + context = applicationContext; + } + public static T getBean(Class beanClass) { + return context.getBean(beanClass); + } + public static Object getBean(String beanName) { + return context.getBean(beanName); + } +} diff --git a/weather/src/main/java/cn/hezhaohui/util/WeatherUtil.java b/weather/src/main/java/cn/hezhaohui/util/WeatherUtil.java new file mode 100644 index 0000000..7360aca --- /dev/null +++ b/weather/src/main/java/cn/hezhaohui/util/WeatherUtil.java @@ -0,0 +1,64 @@ +package cn.hezhaohui.util; + +import cn.hezhaohui.common.AIConstant; +import cn.hezhaohui.entity.Weather; +import cn.hezhaohui.service.ChatService; +import cn.hutool.http.HttpUtil; +import cn.hutool.json.JSONObject; +import cn.hutool.json.JSONUtil; +import lombok.extern.slf4j.Slf4j; + +import static cn.hezhaohui.common.WeatherConstant.*; + +@Slf4j +public class WeatherUtil { + /** + * 参考文档: https://lbs.amap.com/api/webservice/guide/api/weatherinfo + * URL 格式: https://restapi.amap.com/v3/weather/weatherInfo?parameters + * + */ + + + + private static String createUrl() { + String url = API_BASE_URL + + "?key=" + + API_SECRET + + "&city=" + + CITY_CODE_WUHAN + + "&extensions=base"; + log.info("[Weather] URL: {}", url); + return url; + } + + public static Weather getWeather() { + String url = createUrl(); + String ans = HttpUtil.get(url); + log.info("[Weather] Response: {}", ans); + JSONObject obj = JSONUtil.parseObj(ans); + log.info("[Weather] Obj: {}", obj); + // 检查返回状态码 + String status = obj.getStr("status"); + if (!"1".equals(status)) { + String info = obj.getStr("info", "Unknown Error"); + String errorCode = obj.getStr("infocode", "N/A"); + log.error("[Weather] Request failed with code: {}, error: {}", errorCode, info); + return null; + } + // 获取天气信息数组 + JSONObject weatherData = obj.getJSONArray("lives").getJSONObject(0); + Weather weather = Weather.builder() + .weather(weatherData.getStr("weather")) + .temperature(weatherData.getStr("temperature")) + .windDirection(weatherData.getStr("winddirection")) + .windPower(weatherData.getStr("windpower")) + .reportTime(weatherData.getStr("reporttime")) + .humidity(weatherData.getStr("humidity")) + .build(); + String content = SpringContextUtil.getBean(ChatService.class).doChat(AIConstant.BACKGROUND_WEATHER, weather.toString()); + weather.setContent(content); + log.info("[Weather] Prepared: {}", weather); + return weather; + } + +} diff --git a/weather/src/main/resources/application.yml b/weather/src/main/resources/application.yml new file mode 100644 index 0000000..42cb511 --- /dev/null +++ b/weather/src/main/resources/application.yml @@ -0,0 +1,9 @@ +spring: + ai: + openai: + api-key: sk-ihsqnxhnzmgsaojdkxomcmgdypdfpzojymfndfklgzfchlgz + base-url: https://api.siliconflow.cn + chat: + options: + model: Pro/Qwen/Qwen2.5-7B-Instruct + temperature: 0.7 diff --git a/weather/src/test/java/cn/hezhaohui/service/ChatServiceTest.java b/weather/src/test/java/cn/hezhaohui/service/ChatServiceTest.java new file mode 100644 index 0000000..b8f1c17 --- /dev/null +++ b/weather/src/test/java/cn/hezhaohui/service/ChatServiceTest.java @@ -0,0 +1,18 @@ +package cn.hezhaohui.service; + +import cn.hezhaohui.common.AIConstant; +import jakarta.annotation.Resource; +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE) +class ChatServiceTest { + + @Resource + private ChatService chatService; + + @Test + void chat() { + chatService.doChat(AIConstant.BACKGROUND_WEATHER, "今天下雨"); + } +} \ No newline at end of file diff --git a/weather/src/test/java/cn/hezhaohui/util/WeatherUtilTest.java b/weather/src/test/java/cn/hezhaohui/util/WeatherUtilTest.java new file mode 100644 index 0000000..88534ee --- /dev/null +++ b/weather/src/test/java/cn/hezhaohui/util/WeatherUtilTest.java @@ -0,0 +1,15 @@ +package cn.hezhaohui.util; + +import cn.hezhaohui.entity.Weather; +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE) +class WeatherUtilTest { + + @Test + void getWeather() { + Weather weather = WeatherUtil.getWeather(); + System.out.println(weather); + } +} \ No newline at end of file