赞
踩
用飞书机器人每天定时给女朋友发今天日期,在一起天数及女朋友所在地天气情况。
后续更新更多定制化好玩的消息内容(距离两个人的生日天数,根据天气温度提醒女朋友加减衣服以及有雨出门带伞,在一起纪念日,及其他有意义的日子提醒)
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 后续完善!!
参考:
能力有限,第一版本拓展性较差,代码全部写死,不够灵活,后续会不断完善。
欢迎有能力的大佬提供优化思路记更多好玩玩法,不胜感激。
时间精力有限,有问题欢迎咨询,有时间就会回复 sunyuan608@gmial.com
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。