微信给男(女)朋友每天定时发送早安

微信推送,全是自己写的,内容可以定制模版改成自己想发送的文案。数据来源可以是百度天气的数据,因为这样天气预报数据会比较准确。整个程序的代码也不算难,程序中没有用到数据库,服务器是云服务器,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);
    }

 

 

 

需要做的点赞关注然后私信博主或者留言哦!!! 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值