首页 > 其他分享 >详解Spring Boot如何实现每日给女朋友微信推送早安问候语和天气预报浪漫教程

详解Spring Boot如何实现每日给女朋友微信推送早安问候语和天气预报浪漫教程

时间:2023-09-16 10:01:42浏览次数:33  
标签:templateMessage String Spring Boot weather static 问候语 new public

  • 每天早上可以给指定的微信用户推送消息,经过公众号
  • 可以使用第三方接口丰富推送的消息内容
  • 百度天气api:添加天气信息推送
  • 天行数据api:添加美句、彩虹屁等语句推送
  • 通过后台计算纪念日推送
  • ......
  • 效果图
  • 详解Spring Boot如何实现每日给女朋友微信推送早安问候语和天气预报浪漫教程_json


技术栈点

  • spring boot实现后台
  • 微信测试账号的申请
  • 微信模版推送的配置
  • 对接百度天气api
  • 对接彩虹屁api
  • 对接优美句子api

源码开放

Gitee:https://gitee.com/cvzhanshi-ursula/wechatpush

GitHub:https://github.com/cvzhanshi-ursula/wechatpush

② 注册微信测试账号,编辑推送模板

  • 使用微信扫码登录此网站https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login就能得到一个微信公众号测试号
  • 推送消息需要用到的信息
  • 详解Spring Boot如何实现每日给女朋友微信推送早安问候语和天气预报浪漫教程_spring_02


  • 用户扫码关注得到用户的id
  • 详解Spring Boot如何实现每日给女朋友微信推送早安问候语和天气预报浪漫教程_json_03


  • 编辑消息模板
  • 详解Spring Boot如何实现每日给女朋友微信推送早安问候语和天气预报浪漫教程_spring_04


  • 此步骤的模板id、用户微信号id、以及自己的appID、appsecret是后续推送所需要用到的

项目推荐:基于SpringBoot2.x、SpringCloud和SpringCloudAlibaba企业级系统架构底层框架封装,解决业务开发时常见的非功能性需求,防止重复造轮子,方便业务快速开发和企业技术栈框架统一管理。引入组件化的思想实现高内聚低耦合并且高度可配置化,做到可插拔。严格控制包依赖和统一版本管理,做到最少化依赖。注重代码规范和注释,非常适合个人学习和企业使用

Github地址:https://github.com/plasticene/plasticene-boot-starter-parent

Gitee地址:https://gitee.com/plasticene3/plasticene-boot-starter-parent

微信公众号Shepherd进阶笔记

交流探讨qun:Shepherd_126

③ 使用spring boot 做后台开发,并且与第三方对接

使用第三方接口——控制台 | 百度地图开放平台 (baidu.com)

  • 在百度地图开放平台注册账号,并且到控制台中的应用创建一个应用(其中应用AK是推送需要使用到的
  • 详解Spring Boot如何实现每日给女朋友微信推送早安问候语和天气预报浪漫教程_spring_05


  • 设置ip白名单为0.0.0.0/0

使用第三方接口——天行数据TianAPI - 开发者API数据平台

  • 进去注册账号选择需要的句子接口使用就行
  • 每个接口都有实例代码,直接使用就行
  • 详解Spring Boot如何实现每日给女朋友微信推送早安问候语和天气预报浪漫教程_json_06


  • 此案例使用了彩虹屁以及英语一句话两种

spring boot后台开发

  • 创建spring boot项目,创建教程
  • 导入需要的依赖
<properties>
    <java.version>1.8</java.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <spring-boot.version>2.3.7.RELEASE</spring-boot.version>
</properties>

<dependencies>
    <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.20</version>
        <scope>provided</scope>
    </dependency>

    <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>2.0.7</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.github.binarywang/weixin-java-mp -->
    <dependency>
        <groupId>com.github.binarywang</groupId>
        <artifactId>weixin-java-mp</artifactId>
        <version>3.3.0</version>
    </dependency>
</dependencies>
  • 编写对接百度天气api 的工具类
    天气的实体类
/**
 * @author cVzhanshi
 * @create 2022-08-04 2215
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Weather {
    String wd_night;
    String date;
    String high;
    String week;
    String text_night;
    String wd_day;
    String low;
    String wc_night;
    String text_day;
    String wc_day;
    // 当前天气
    String text_now;
    // 当前温度
    String temp;
    // 风级大小
    String wind_class;
    // 风向
    String wind_dir;
}
/**
 * @author cVzhanshi
 * @create 2022-08-04 22:02
 */
public class WeatherUtils {
    public static void main(String[] args) {
        System.out.println(getWeather());
    }
    public static Weather getWeather(){
        RestTemplate restTemplate = new RestTemplate();
        Map<String,String> map = new HashMap<String,String>();
        map.put("district_id","320583"); // 地方行政代码
        map.put("data_type","all");//这个是数据类型
        map.put("ak","自己的应用AK");
        String res = restTemplate.getForObject(
                "https://api.map.baidu.com/weather/v1/?district_id={district_id}&data_type={data_type}&ak={ak}",
                String.class,
                map);
        JSONObject json = JSONObject.parseObject(res);
        JSONArray forecasts = json.getJSONObject("result").getJSONArray("forecasts");
        List<Weather> weathers = forecasts.toJavaList(Weather.class);
        JSONObject now = json.getJSONObject("result").getJSONObject("now");
        Weather weather = weathers.get(0);
        weather.setText_now(now.getString("text"));
        weather.setTemp(now.getString("temp"));
        weather.setWind_class(now.getString("wind_class"));
        weather.setWind_dir(now.getString("wind_dir"));
        return weather;
    }
}
  • 编写对接天行数据(彩虹屁)api的工具类
/**
 * @author cVzhanshi
 * @create 2022-08-04 22:58
 */
public class CaiHongPiUtils {
    public static String getCaiHongPi() {
        String httpUrl = "http://api.tianapi.com/caihongpi/index?key=接口的key";
        BufferedReader reader = null;
        String result = null;
        StringBuffer sbf = new StringBuffer();

        try {
            URL url = new URL(httpUrl);
            HttpURLConnection connection = (HttpURLConnection) url
                    .openConnection();
            connection.setRequestMethod("GET");
            InputStream is = connection.getInputStream();
            reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            String strRead = null;
            while ((strRead = reader.readLine()) != null) {
                sbf.append(strRead);
                sbf.append("\r\n");
            }
            reader.close();
            result = sbf.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        JSONObject jsonObject = JSONObject.parseObject(result);
        JSONArray newslist = jsonObject.getJSONArray("newslist");
        String content = newslist.getJSONObject(0).getString("content");
        return content;
    }

    public static Map<String,String> getEnsentence() {
        String httpUrl = "http://api.tianapi.com/ensentence/index?key=接口的key";
        BufferedReader reader = null;
        String result = null;
        StringBuffer sbf = new StringBuffer();
        try {
            URL url = new URL(httpUrl);
            HttpURLConnection connection = (HttpURLConnection) url
                    .openConnection();
            connection.setRequestMethod("GET");
            InputStream is = connection.getInputStream();
            reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            String strRead = null;
            while ((strRead = reader.readLine()) != null) {
                sbf.append(strRead);
                sbf.append("\r\n");
            }
            reader.close();
            result = sbf.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        JSONObject jsonObject = JSONObject.parseObject(result);
        JSONArray newslist = jsonObject.getJSONArray("newslist");
        String en = newslist.getJSONObject(0).getString("en");
        String zh = newslist.getJSONObject(0).getString("zh");
        Map<String, String> map = new HashMap<>();
        map.put("zh",zh);
        map.put("en",en);
        return map;
    }
}
  • 编写计算纪念日的工具类
public class JiNianRiUtils {

    public static int getLianAi(){
        return calculationLianAi("2022-12-11");
    }
    public static int getBirthday_Jo(){
        try {
            return calculationBirthday("2009-03-09");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }
    public static int getBirthday_Hui(){
        try {
            return calculationBirthday("2020-01-11");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }

 // 计算生日天数
    public static int calculationBirthday(String clidate) throws ParseException {
        SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cToday = Calendar.getInstance(); // 存今天
        Calendar cBirth = Calendar.getInstance(); // 存生日
        cBirth.setTime(myFormatter.parse(clidate)); // 设置生日
        cBirth.set(Calendar.YEAR, cToday.get(Calendar.YEAR)); // 修改为本年
        int days;
        if (cBirth.get(Calendar.DAY_OF_YEAR) < cToday.get(Calendar.DAY_OF_YEAR)) {
            // 生日已经过了,要算明年的了
            days = cToday.getActualMaximum(Calendar.DAY_OF_YEAR) - cToday.get(Calendar.DAY_OF_YEAR);
            days += cBirth.get(Calendar.DAY_OF_YEAR);
        } else {
            // 生日还没过
            days = cBirth.get(Calendar.DAY_OF_YEAR) - cToday.get(Calendar.DAY_OF_YEAR);
        }
        // 输出结果
        if (days == 0) {
            return 0;
        } else {
            return days;
        }
    }
 
    // 计算天数
    public static int calculationLianAi(String date) {
        DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        int day = 0;
        try {
            long time = System.currentTimeMillis() - simpleDateFormat.parse(date).getTime();
            day = (int) (time / 86400000L);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return day;
    }
}
  • 编写推送类
/**
 * @author cVzhanshi
 * @create 2022-08-04 21:09
 */
public class Pusher {

    public static void main(String[] args) {
        push();
    }
    private static String appId = "xx";
    private static String secret = "xx";



    public static void push(){
        //1,配置
        WxMpInMemoryConfigStorage wxStorage = new WxMpInMemoryConfigStorage();
        wxStorage.setAppId(appId);
        wxStorage.setSecret(secret);
        WxMpService wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(wxStorage);
        //2,推送消息
        WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                .toUser("用户微信id") 
                .templateId("消息模板id")
                .build();
        //3,如果是正式版发送模版消息,这里需要配置你的信息
        Weather weather = WeatherUtils.getWeather();
        Map<String, String> map = CaiHongPiUtils.getEnsentence();
        templateMessage.addData(new WxMpTemplateData("riqi",weather.getDate() + "  "+ weather.getWeek(),"#00BFFF"));
        templateMessage.addData(new WxMpTemplateData("tianqi",weather.getText_now(),"#00FFFF"));
        templateMessage.addData(new WxMpTemplateData("low",weather.getLow() + "","#173177"));
        templateMessage.addData(new WxMpTemplateData("temp",weather.getTemp() + "","#EE212D"));
        templateMessage.addData(new WxMpTemplateData("high",weather.getHigh()+ "","#FF6347" ));
        templateMessage.addData(new WxMpTemplateData("windclass",weather.getWind_class()+ "","#42B857" ));
        templateMessage.addData(new WxMpTemplateData("winddir",weather.getWind_dir()+ "","#B95EA3" ));
        templateMessage.addData(new WxMpTemplateData("caihongpi",CaiHongPiUtils.getCaiHongPi(),"#FF69B4"));
        templateMessage.addData(new WxMpTemplateData("lianai",JiNianRiUtils.getLianAi()+"","#FF1493"));
        templateMessage.addData(new WxMpTemplateData("shengri1",JiNianRiUtils.getBirthday_Jo()+"","#FFA500"));
        templateMessage.addData(new WxMpTemplateData("shengri2",JiNianRiUtils.getBirthday_Hui()+"","#FFA500"));
        templateMessage.addData(new WxMpTemplateData("en",map.get("en") +"","#C71585"));
        templateMessage.addData(new WxMpTemplateData("zh",map.get("zh") +"","#C71585"));
        String beizhu = "❤";
        if(JiNianRiUtils.getLianAi() % 365 == 0){
            beizhu = "今天是恋爱" + (JiNianRiUtils.getLianAi() / 365) + "周年纪念日!";
        }
        if(JiNianRiUtils.getBirthday_Jo()  == 0){
            beizhu = "今天是生日,生日快乐呀!";
        }
        if(JiNianRiUtils.getBirthday_Hui()  == 0){
            beizhu = "今天是生日,生日快乐呀!";
        }
        templateMessage.addData(new WxMpTemplateData("beizhu",beizhu,"#FF0000"));

        try {
            System.out.println(templateMessage.toJson());
            System.out.println(wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage));
        } catch (Exception e) {
            System.out.println("推送失败:" + e.getMessage());
            e.printStackTrace();
        }
    }
}
  • 编写定时任务
@SpringBootApplication
@EnableScheduling // 开启定时任务
public class WechatpushApplication {

    public static void main(String[] args) {
        SpringApplication.run(WechatpushApplication.class, args);
    }

 // 定时
    @Scheduled(cron = "0 30 7 * * ?")
    public void goodMorning(){
        Pusher.push();
    }
}

④ 部署

  • 把项目打包成jar包
  • 详解Spring Boot如何实现每日给女朋友微信推送早安问候语和天气预报浪漫教程_json_07


  • 把jar包上传到服务器并且运行起来
# 运行命令
nohup java -jar test.jar >temp.txt &

标签:templateMessage,String,Spring,Boot,weather,static,问候语,new,public
From: https://blog.51cto.com/yangshaoping/7491380

相关文章

  • spring依赖注入单例模式下(默认都是单例),类变量(实例变量)线程安全问题
    java变量是程序中最基本的存储单元,其要素包括变量名,变量类型和作用域。Java的变量类型有:   成员变量类中的变量(独立于方法之外的变量)   局部变量类的方法中的变量。而java类的成员变量又有俩种:   静态变量(类变量):独立于方法之外的变量,用static修饰。   实例变......
  • 在springboot中处理UDP流
    配置: <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-integration</artifactId></dependency><dependency><groupId>org.springframework.integration</gr......
  • springboot+vue导出本地可执行文件
    1、前端页面增加下载链接<ahref="http://localhost:80/system/download"download="xxx.exe">下载地址</a>2、后端读取文件下载//下载文件@GetMapping("/system/download")publicvoiddownload(HttpServletResponseresponse){S......
  • mac版本Spring5.0源码环境搭建
    下载spring5.0版本代码链接是:https://github.com/spring-projects/spring-framework.git装gradle,使用的版本是8.3版本链接是:https://gradle.org/next-steps/?version=8.3&format=bin有错误提示:/Users/wangyu/work/code/spring-framework/buildSrc/src/main/java/org/springfra......
  • Docker+harbor+rancher2.6.3部署springboot项目
    1、在pom的文件中添加以下配置<build><finalName>${project.artifactId}</finalName><plugins><plugin><groupId>com.spotify</groupId><artifactId>docker-maven-plugin</artifactId......
  • 深入理解Spring MVC框架及其工作原理
    SpringMVC是一种基于Java的Web应用程序开发框架,它提供了一种模型-视图-控制器(MVC)的架构模式,用于构建灵活、可扩展且高效的Web应用程序。本文将深入探讨SpringMVC框架的各个组件和工作原理。介绍SpringMVCSpringMVC是SpringFramework的一个模块,用于开发Web应用程序。它基于经......
  • 解决SpringBoot Async异步方法获取不到Security Context
     SecurityContextHolder.setStrategyName(SecurityContextHolder.MODE_INHERITABLETHREADLOCAL);这样设置的话很不安全,不废话,直接上代码,改造一下AsyncConfig就可以了,线程也安全/***@description:线程池的配置*/@ConfigurationpublicclassAsyncConfig{privates......
  • Spring Boot + minio 实现高性能存储服务,So Easy~!
    什么是minio引用官网:MinIO是根据GNUAffero通用公共许可证v3.0发布的高性能对象存储。它与AmazonS3云存储服务兼容。使用MinIO构建用于机器学习,分析和应用程序数据工作负载的高性能基础架构。官网地址:https://min.io/文档地址:https://docs.min.io/一.使用docker搭......
  • 【规范】SpringBoot接口返回结果及异常统一处理,这样封装才优雅
    前言......
  • Spring整合DWR comet 实现无刷新 多人聊天室
    用dwr的comet(推)来实现简单的无刷新多人聊天室,comet是长连接的一种。通常我们要实现无刷新,一般会使用到Ajax。Ajax应用程序可以使用两种基本的方法解决这一问题:一种方法是浏览器每隔若干秒时间向服务器发出轮询以进行更新,另一种方法是服务器始终打开与浏览器的连接并在数据可用时......