微信推送,全是自己写的,内容可以定制模版改成自己想发送的文案。数据来源可以是百度天气的数据,因为这样天气预报数据会比较准确。整个程序的代码也不算难,程序中没有用到数据库,服务器是云服务器,jar包一跑就可以了,每天定时定点发送消息。
看一下效果:
所用知识点
- 1. springboot实现java后台
- 2.微信测试账号的申请
- 3.申请百度天气的开发者账号
- 4.接入百度天气api
- 5.微信模版推送的配置
相关核心代码:
public static String getCaiHongPi() {
//默认彩虹屁
String str = "阳光落在屋里,爱你藏在心里";
try {
JSONObject jsonObject = JSONObject.parseObject(HttpUtil.getUrl(url+key).replace("XXX", name));
if (jsonObject.getIntValue("code") == 200) {
str = jsonObject.getJSONArray("newslist").getJSONObject(0).getString("content");
}
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
/**
* 载入金句库
*/
static {
InputStream inputStream = CaiHongPi.class.getClassLoader().getResourceAsStream("jinju.txt");
try (BufferedReader br = new BufferedReader(new InputStreamReader(inputStream))) {
String str = "";
String temp = "";
while ((temp = br.readLine()) != null) {
if (!StringUtils.isEmpty(temp)) {
str = str + "\r\n" + temp;
} else {
jinJuList.add(str);
str = "";
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static String doPost(String url, String data) { CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(connectionManager).setKeepAliveStrategy(myStrategy).setDefaultRequestConfig(RequestConfig.custom().setStaleConnectionCheckEnabled(true).build()).build(); HttpPost httpPost = new HttpPost(url); RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(10000).setConnectTimeout(20000).setConnectionRequestTimeout(10000).build(); httpPost.setConfig(requestConfig); String context = ""; if (data != null && data.length() > 0) { StringEntity body = new StringEntity(data, "utf-8"); httpPost.setEntity(body); } httpPost.addHeader("Content-Type", "application/json"); CloseableHttpResponse response = null; try { response = httpClient.execute(httpPost); HttpEntity entity = response.getEntity(); context = EntityUtils.toString(entity, "UTF-8"); } catch (Exception var16) { var16.getStackTrace(); } finally { try { response.close(); httpPost.abort(); } catch (Exception var15) { var15.getStackTrace(); } } return context; }
@Scheduled(cron = "0 0 8 * * ?")
public void goodMorning(){
pusher.push(openId);
}
需要做的点赞关注然后私信博主或者留言哦!!!