zl程序教程

您现在的位置是:首页 >  Java

当前栏目

# 用飞书来谈恋爱,飞书机器人定时给女朋友问好

2023-03-31 10:46:08 时间

用飞书来谈恋爱,飞书机器人定时给女朋友问好

前言

用飞书机器人每天定时给女朋友发今天日期,在一起天数及女朋友所在地天气情况。

后续更新更多定制化好玩的消息内容(距离两个人的生日天数,根据天气温度提醒女朋友加减衣服以及有雨出门带伞,在一起纪念日,及其他有意义的日子提醒)

0.先看1.1版本效果

image-20220924174532544

技术要求

云服务器(或会Github Action) 、Linux基础命令、Spring Boot简单使用

操作步骤

两个人推荐用飞书建一个企业(不用认证,功能比个人版多许多)

准备工作

1.两个人用飞书建一个群,添加群机器人

image-20220924175651901

image-20220924175853437

image-20220924175910622

保存好这个地址,其他暂时不需要

image-20220924175952199

2.申请高德地图API

去登陆申请高德地图API

image-20220924180615668

image-20220924180738035

image-20220924181045788

image-20220924181026374

如下:

image-20220924181128563

记住你的key

官方天气查询教程

查看高德天气的城市编码

3.创建Spring Boot工程,引入Web依赖

不再赘述!

4.制作飞书卡片

制作卡片

具体思路

5.具体思路(实操)

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 = "{
" +
                    "    "msg_type": "interactive",
" +
                    "    "card": {
" +
                    "
" +
                    "  "config": {
" +
                    "    "wide_screen_mode": true
" +
                    "  },
" +
                    "  "header": {
" +
                    "    "template": "red",
" +
                    "    "title": {
" +
                    "      "content": "uD83DuDD14 mua~亲爱的臭宝贝 uD83CuDF81",
" +
                    "      "tag": "plain_text"
" +
                    "    }
" +
                    "  },
" +
                    "  "i18n_elements": {
" +
                    "    "zh_cn": [
" +
                    "      {
" +
                    "        "tag": "div",
" +
                    "        "text": {
" +
                    "          "content": "**uD83CuDF84 今天是:**" + formatDate + "  " + week + "\n\n**uD83CuDF81 今天是我们在一起的第:" + betweenDay + "天" + "**",
" +
                    "          "tag": "lark_md"
" +
                    "        }
" +
                    "      },
" +
                    "      {
" +
                    "        "tag": "div",
" +
                    "        "text": {
" +
                    "          "content": " " + formatDate + "  " + province + " " + city + "  \n数据发布的时间 :" + formatReporttime + " \n今天天气 : " + "白天 " + dayWeather + "  " + "晚上 " + nightWeather + "\n今天温度 :" + nightTemp + " ~ " + dayTemp + "\n今天风向 : " + "白天 " + dayWind + "  " + "晚上 " + nightWind + "\n\n**祝我的臭宝每天开心,每天爱我的臭宝多一点点 !**",
" +
                    "          "tag": "lark_md"
" +
                    "        }
" +
                    "      }
" +
                    "    ]
" +
                    "  }
" +
                    "}
" +
                    "}";
// 飞书机器人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();

        }
    }

6.发布部署

Maven打包项目。

image-20220924184034046

将打包好的jar包上传到服务器的任意位置

image-20220924184315417

我传到了/www/feishu下

然后在当前目录执行

nohup java -jar penn-0.0.1-SNAPSHOT.jar >msg.log 2>&1 &

如果想杀掉运行中的jar程序,查看进程命令为:

ps -aux | grep java

执行

kill -9 

image-20220924184603412

其他部署方案,Github Action 后续完善!!

7.完整代码

完整代码Github

完整代码蓝奏云

image-20220924213641892

参考:

后记

能力有限,第一版本拓展性较差,代码全部写死,不够灵活,后续会不断完善。

欢迎有能力的大佬提供优化思路记更多好玩玩法,不胜感激。

有问题欢迎咨询,有时间就会回复 sunyuan608@gmial.com

时间精力有限,有偿定制,提供服务器!