用飛書機器人每天定時給女朋友發今天日期,在一起天數及女朋友所在地天氣情況。
後續更新更多客製化化好玩的訊息內容(距離兩個人的生日天數,根據天氣溫度提醒女朋友加減衣服以及有雨出門帶傘,在一起紀念日,及其他有意義的日子提醒)
0.先看1.1版本效果
雲伺服器(或會Github Action) 、Linux基礎命令、Spring Boot簡單使用
兩個人推薦用飛書建一個企業(不用認證,功能比個人版多許多)
準備工作
儲存好這個地址,其他暫時不需要
如下:
記住你的key
不再贅述!
具體思路
1.引入依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- hutool-->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.5</version>
</dependency>
<!-- lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
2.完整思路
@RequestMapping("/timingAt8")
public void timingAt8() {
log.info("定時任務" + DateUtil.formatDateTime(new Date()));
// 在一起時間
String beginDate = "2022-99-19";
Date date1 = DateUtil.parse(beginDate);
Date now = new Date();
long betweenDay = DateUtil.between(date1, now, DateUnit.DAY);
System.out.println("今天是和XX在一起的第" + betweenDay + "天");
// 高德地圖API 查詢天氣情況
// https://restapi.amap.com/v3/weather/weatherInfo?city=自己地區編碼&key=高德地圖的key&extensions=all"
String tunLiuUrl = "https://restapi.amap.com/v3/weather/weatherInfo?city=110000&key=150ecc8f5e61733315fb113889c8b1ec&extensions=all";
String result2 = HttpUtil.get(tunLiuUrl, CharsetUtil.CHARSET_UTF_8);
// 字串轉JSON
// 在這裡(https://www.json.cn/json/jsononline.html)將result2 轉成 GaodeResult
GaodeResult gaodeResult = JSONUtil.toBean(result2, GaodeResult.class);
if (SUCCESS.equals(gaodeResult.getStatus())) {
System.out.println("查詢成功--");
List<Forecasts> forecasts = gaodeResult.getForecasts();
List<Casts> casts = forecasts.get(0).getCasts();
String province = forecasts.get(0).getProvince();
String city = forecasts.get(0).getCity();
Date reporttime = forecasts.get(0).getReporttime();
String formatReporttime = DateUtil.format(reporttime, "yyyy.MM.dd HH:mm:ss");
System.out.println("今天天氣---");
Casts live = casts.get(0);
Date date = live.getDate();
String formatDate = DateUtil.format(date, "yyyy-MM-dd");
System.out.println("----------" + formatDate);
String week = "星期" + live.getWeek();
String dayWeather = live.getDayweather();
String nightWeather = live.getNightweather();
String dayTemp = live.getDaytemp() + "度";
String nightTemp = live.getNighttemp() + "度";
String dayWind = live.getDaywind();
String nightWind = live.getNightwind();
String daypower = live.getDaypower();
String nightPower = live.getNightpower();
System.out.println("今天是 :" + formatDate + " " + week);
System.out.println("今天是和臭寶在一起的第" + betweenDay + "天");
System.out.println("今天天氣 :" + "白天 " + dayWeather + " " + "晚上 " + nightWeather);
System.out.println("今天溫度 :" + "白天 " + dayTemp + " " + "晚上 " + nightTemp);
System.out.println("今天風向 :" + "白天 " + dayWind + " " + "晚上 " + nightWind);
// 飛書卡片
String json = "{\n" +
" \"msg_type\": \"interactive\",\n" +
" \"card\": {\n" +
"\n" +
" \"config\": {\n" +
" \"wide_screen_mode\": true\n" +
" },\n" +
" \"header\": {\n" +
" \"template\": \"red\",\n" +
" \"title\": {\n" +
" \"content\": \"\uD83D\uDD14 mua~親愛的臭寶貝 \uD83C\uDF81\",\n" +
" \"tag\": \"plain_text\"\n" +
" }\n" +
" },\n" +
" \"i18n_elements\": {\n" +
" \"zh_cn\": [\n" +
" {\n" +
" \"tag\": \"div\",\n" +
" \"text\": {\n" +
" \"content\": \"**\uD83C\uDF84 今天是:**" + formatDate + " " + week + "\\n\\n**\uD83C\uDF81 今天是我們在一起的第:" + betweenDay + "天" + "**\",\n" +
" \"tag\": \"lark_md\"\n" +
" }\n" +
" },\n" +
" {\n" +
" \"tag\": \"div\",\n" +
" \"text\": {\n" +
" \"content\": \" " + formatDate + " " + province + " " + city + " \\n資料釋出的時間 :" + formatReporttime + " \\n今天天氣 : " + "白天 " + dayWeather + " " + "晚上 " + nightWeather + "\\n今天溫度 :" + nightTemp + " ~ " + dayTemp + "\\n今天風向 : " + "白天 " + dayWind + " " + "晚上 " + nightWind + "\\n\\n**祝我的臭寶每天開心,每天愛我的臭寶多一點點 !**\",\n" +
" \"tag\": \"lark_md\"\n" +
" }\n" +
" }\n" +
" ]\n" +
" }\n" +
"}\n" +
"}";
// 飛書機器人webhook
String feishuUrl = "https://open.feishu.cn/open-apis/bot/v2/hook/5371db79-7cb2-45ef-bac1-161e2a714XXX";
String result3 = HttpRequest
.post(feishuUrl)
.body(json)
.execute().body();
}
}
Maven打包專案。
將打包好的jar包上傳到伺服器的任意位置
我傳到了/www/feishu下
然後在當前目錄執行
nohup java -jar penn-0.0.1-SNAPSHOT.jar >msg.log 2>&1 &
如果想殺掉執行中的jar程式,檢視程序命令為:
ps -aux | grep java
執行
kill -9
其他部署方案,Github Action 後續完善!!
參考:
能力有限,第一版本拓展性較差,程式碼全部寫死,不夠靈活,後續會不斷完善。
歡迎有能力的大佬提供優化思路記更多好玩玩法,不勝感激。
有問題歡迎諮詢,有時間就會回覆 [email protected]
時間精力有限,有償客製化,提供伺服器!