微信每日天气推送
一、编辑推送模板
二、Springboot 后台
目录结构:
1.依赖
1 <properties> 2 <java.version>1.8</java.version> 3 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 4 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 5 <spring-boot.version>2.3.7.RELEASE</spring-boot.version> 6 </properties> 7 8 <dependencies> 9 <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok --> 10 <dependency> 11 <groupId>org.projectlombok</groupId> 12 <artifactId>lombok</artifactId> 13 <version>1.18.20</version> 14 <scope>provided</scope> 15 </dependency> 16 17 <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson --> 18 <dependency> 19 <groupId>com.alibaba</groupId> 20 <artifactId>fastjson</artifactId> 21 <version>2.0.7</version> 22 </dependency> 23 24 <dependency> 25 <groupId>org.springframework.boot</groupId> 26 <artifactId>spring-boot-starter-web</artifactId> 27 </dependency> 28 29 <dependency> 30 <groupId>org.springframework.boot</groupId> 31 <artifactId>spring-boot-starter-test</artifactId> 32 <scope>test</scope> 33 <exclusions> 34 <exclusion> 35 <groupId>org.junit.vintage</groupId> 36 <artifactId>junit-vintage-engine</artifactId> 37 </exclusion> 38 </exclusions> 39 </dependency> 40 41 <!-- https://mvnrepository.com/artifact/com.github.binarywang/weixin-java-mp --> 42 <dependency> 43 <groupId>com.github.binarywang</groupId> 44 <artifactId>weixin-java-mp</artifactId> 45 <version>3.3.0</version> 46 </dependency> 47 </dependencies>
2.实体类
1 package com.ang.wxpushdaily.entity; 2 3 import lombok.AllArgsConstructor; 4 import lombok.Data; 5 import lombok.NoArgsConstructor; 6 7 /** 8 * @author ncwuDA 9 * @createtime 2022-08-25 10 */ 11 @Data 12 @AllArgsConstructor 13 @NoArgsConstructor 14 public class Weather { 15 String area; 16 String date; 17 String week; 18 // 当前天气 19 String weather; 20 // 当前温度 21 String real; 22 // 最低温度 23 String lowest; 24 // 最高温度 25 String highest; 26 // 风级大小 27 String windsc; 28 // 风向 29 String wind; 30 // 提示 31 String tips; 32 33 }
3.工具类
1)发送请求工具类
1 package com.ang.wxpushdaily.utils; 2 3 import org.apache.http.HttpEntity; 4 import org.apache.http.client.methods.CloseableHttpResponse; 5 import org.apache.http.client.methods.HttpGet; 6 import org.apache.http.impl.client.CloseableHttpClient; 7 import org.apache.http.impl.client.HttpClients; 8 import org.apache.http.util.EntityUtils; 9 10 import java.io.IOException; 11 12 /** 13 * @author ncwuDA 14 * @createtime 2022-08-25 15 */ 16 public class HttpsClientUtils { 17 18 /** 19 * 发送get请求 20 * 21 * @param url 22 */ 23 public static String sendGet(String url) throws IOException { 24 CloseableHttpClient httpClient = HttpClients.createDefault(); 25 HttpGet httpGet = new HttpGet(url); 26 CloseableHttpResponse response = httpClient.execute(httpGet); 27 String resp; 28 try { 29 HttpEntity entity = response.getEntity(); 30 resp = EntityUtils.toString(entity, "utf-8"); 31 EntityUtils.consume(entity); 32 } finally { 33 response.close(); 34 } 35 return resp; 36 } 37 }
2)获取天气工具类
1 package com.ang.wxpushdaily.utils; 2 3 import com.alibaba.fastjson.JSONArray; 4 import com.alibaba.fastjson.JSONObject; 5 import com.ang.wxpushdaily.entity.Weather; 6 import org.springframework.web.client.RestTemplate; 7 8 import java.io.IOException; 9 import java.util.HashMap; 10 import java.util.List; 11 import java.util.Map; 12 13 /** 14 * @author ncwuDA 15 * @createtime 2022-08-25 16 */ 17 public class WeatherUtils { 18 19 public static Weather getWeather() throws IOException { 20 String city = "XXX"; // 地区 21 String key = "********************"; // APIKEY 22 String res = HttpsClientUtils.sendGet("http://api.tianapi.com/tianqi/index?key=" + key + "&&city=" + city); 23 JSONObject json = JSONObject.parseObject(res); 24 assert json != null; 25 JSONArray newsList = json.getJSONArray("newslist"); 26 List<Weather> weathers = newsList.toJavaList(Weather.class); 27 28 return weathers.get(0); 29 } 30 }
4.推送配置
1 package com.ang.wxpushdaily.main; 2 3 import com.ang.wxpushdaily.entity.Weather; 4 import com.ang.wxpushdaily.utils.WeatherUtils; 5 import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage; 6 import me.chanjar.weixin.mp.api.WxMpService; 7 import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl; 8 import me.chanjar.weixin.mp.bean.template.WxMpTemplateData; 9 import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage; 10 11 import java.io.IOException; 12 13 /** 14 * @author ncwuDA 15 * @createtime 2022-08-25 16 */ 17 public class Pusher { 18 19 private static final String appId = "wxa*************"; 20 private static final String secret = "******379bf043f0a******"; 21 22 public static void push() throws IOException { 23 // 配置测试号信息 24 WxMpInMemoryConfigStorage wxStorage = new WxMpInMemoryConfigStorage(); 25 wxStorage.setAppId(appId); 26 wxStorage.setSecret(secret); 27 WxMpService wxMpService = new WxMpServiceImpl(); 28 wxMpService.setWxMpConfigStorage(wxStorage); 29 // 配置模板信息 30 WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder() 31 .toUser("**********370BAK1WfKEbU") // 用户id 32 .templateId("***************Mi78CXvSI8g7osgaWR5lYiWqRE") //模板id 33 .build(); 34 // 配置模版推送消息 35 Weather weather = WeatherUtils.getWeather(); 36 templateMessage.addData(new WxMpTemplateData("date", weather.getDate() + " " + weather.getWeek() + "\n", "#9932CD")); 37 templateMessage.addData(new WxMpTemplateData("weather", weather.getArea() + " " + weather.getWeather() + "\n", "#9932CD")); 38 templateMessage.addData(new WxMpTemplateData("real", weather.getReal() + "\n", "#EE212D")); 39 templateMessage.addData(new WxMpTemplateData("lowest", weather.getLowest() + " ", "#EE212D")); 40 templateMessage.addData(new WxMpTemplateData("highest", weather.getHighest() + "\n", "#EE212D")); 41 templateMessage.addData(new WxMpTemplateData("wind", weather.getWind() + " ", "#B95EA3")); 42 templateMessage.addData(new WxMpTemplateData("windsc", weather.getWindsc() + "\n\n", "#B95EA3")); 43 templateMessage.addData(new WxMpTemplateData("tips", weather.getTips() + "", "#8E2323")); 44 45 try { 46 System.out.println(templateMessage.toJson()); 47 System.out.println(wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage)); 48 } catch (Exception e) { 49 System.out.println("推送失败:" + e.getMessage()); 50 e.printStackTrace(); 51 } 52 } 53 }
5.启动定时
package com.ang.wxpushdaily; import com.ang.wxpushdaily.main.Pusher; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.Scheduled; import java.io.IOException; @SpringBootApplication @EnableScheduling // 开启定时任务 public class WxPushDailyApplication { public static void main(String[] args) { SpringApplication.run(WxPushDailyApplication.class, args); } // 定时 @Scheduled(cron = "0 0 6 * * ?") public void goodMorning() throws IOException { Pusher.push(); } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本