赞
踩
据说这个功能最近在抖音上很火,我没有抖音,没有看到。 但是我在掘金和CSDN上看了,相关案例确实很多,但是大家都是借助于了微信服务号,在我看来,效果很不佳。
其实我原来的初衷是这样的,每天定时给群里推送新闻,这个原因在之前的文章中也提到过,这边就不在细说了,然后实现了给群中推送之后,就想着,能不能每天给指定人发送消息,网上搜了搜,果然有,各种实现方式五花八门,但是都没看上,于是就自己找了个,自己写。
网上最多的可能就是利用微信服务号给推送信息,如图所示:
缺点如下:
介于此,我就自己找了一个路子去做,昨晚刚写完,测了测,效果还是比较不错的。如下图所示:
(有点肉麻~)
下面我们来看看是如何实现的。
20
块钱开个会员(20块钱一点都不多)假使你的准备工作除4、5之外,都已经做完了,下面我们来看看如何从代码层面来实现。
api
,获取自己想要的数据,封装起来controller
类中添加定时任务,实现定时发送关键代码如下所示:
- /**
- * 获取每日早安寄语
- *
- */
- public static String getGoodMorning() {
- String path = "zaoan/index";
- //调用每日简报的接口
- String body = HttpUtil
- .createGet(Constants.TIAN_XING_API + path + "?key=" + TIAN_KEY)
- .execute()
- .body();
- JSONObject jsonObjectResult = JSON.parseObject(body);
- Integer code = jsonObjectResult.getInteger("code");
- if (code != 200) {
- log.error("请求每日新闻的接口出错,出错信息:" + body);
- return "一切都是最好的安排~";
- }
- log.info("请求每日新闻的接口成功:" + body);
- JSONObject resultObject = jsonObjectResult.getJSONObject("result");
- return resultObject.getString("content");
- }
- 复制代码
- /**
- * 获取每日土味情话
- *
- */
- public static String getLoveWords(){
- String path = "saylove/index";
- //调用每日简报的接口
- String body = HttpUtil
- .createGet(Constants.TIAN_XING_API + path + "?key=" + TIAN_KEY)
- .execute()
- .body();
- JSONObject jsonObjectResult = JSON.parseObject(body);
- Integer code = jsonObjectResult.getInteger("code");
- if(code!=200){
- log.error("请求每日新闻的接口出错,出错信息:"+body);
- return "一切都是最好的安排~";
- }
- JSONObject resultObject = jsonObjectResult.getJSONObject("result");
- log.info("请求每日新闻的接口成功:"+resultObject);
- return resultObject.getString("content");
- }
- 复制代码
-
- /**
- * 获取天气预报
- *
- */
- public static JSONObject getWeather(){
- String path = "tianqi/index";
- //调用每日简报的接口
- String body = HttpUtil
- .createGet(Constants.TIAN_XING_API + path + "?key=" + TIAN_KEY+"&type=1&city=370100")
- .execute()
- .body();
- JSONObject jsonObjectResult = JSON.parseObject(body);
- Integer code = jsonObjectResult.getInteger("code");
- if(code!=200){
- log.error("请求每日新闻的接口出错,出错信息:"+body);
- return null;
- }
-
- return jsonObjectResult.getJSONObject("result");
- /*
- * {
- "code": 200,
- "msg": "success",
- "result": {
- "date": "2022-12-19",
- "week": "星期一",
- "province": "上海",
- "area": "上海",
- "areaid": "101020100",
- "weather": "晴",
- "weatherimg": "qing.png",
- "weathercode": "qing",
- "real": "3℃",
- "lowest": "2℃",
- "highest": "9℃",
- "wind": "北风",
- "windspeed": "0",
- "windsc": "0级",
- "sunrise": "06:47",
- "sunset": "16:54",
- "moonrise": "",
- "moondown": "",
- "pcpn": "0",
- "uv_index": "0",
- "aqi": "54",
- "quality": "良",
- "vis": "18",
- "humidity": "48",
- "alarmlist": [
-
- ],
- "tips": "晴天紫外线等级较高,外出注意补水防晒。天气凉,适宜着一到两件羊毛衫、大衣、毛套装、皮夹克等春秋着装;年老体弱者宜着风衣加羊毛衫等厚型春秋着装。空气质量较好,可以参与各类户外活动。疫情防控不松懈,出门请佩戴口罩。"
- }
- }
- * */
- }
- 复制代码
- /**
- * 获取汉字的今天周几
- * @param dateTime
- * @return
- * @throws ParseException
- */
- public static String getDaysByWeekChina(String dateTime) throws ParseException {
- Calendar cal = Calendar.getInstance();
- SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
- Date date = dateFormat.parse(dateTime);
- cal.setTime(date);
- int day = cal.get(Calendar.DAY_OF_WEEK);
- day = day - 1;
- //周的汉字
- String week = "";
- switch (day) {
- case 0:
- week = "日";
- break;
- case 1:
- week = "一";
- break;
- case 2:
- week = "二";
- break;
- case 3:
- week = "三";
- break;
- case 4:
- week = "四";
- break;
- case 5:
- week = "五";
- break;
- case 6:
- week = "六";
- break;
- }
- return week;
- }
- 复制代码
-
- /**
- * 得到二个日期间的间隔天数
- *
- * @param date1
- * @param date2
- * @return
- */
- public static int getDayByTwoDay(String date1, String date2) {
- SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
- Long day = 0L;
- try {
- java.util.Date date = myFormatter.parse(date1);
- java.util.Date mydate = myFormatter.parse(date2);
- day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);
- } catch (Exception e) {
- return 0;
- }
- return day.intValue();
- }
- 复制代码
-
- /**
- * 每天将指定信息推送到微信个人
- */
- @Override
- public void sendWechatToUser() {
- String url = WEI_MI_SHU_API + "/openapi/v1/chat/contact";
- WeiZhiMiShuVO weiZhiMiShuVO = new WeiZhiMiShuVO();
- //发送文字信息
- weiZhiMiShuVO.setType(1);
- try {
- //拿到发送的消息列表
- SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日");
- SimpleDateFormat format_h = new SimpleDateFormat("yyyy-MM-dd");
- //今天的日期
- String toDate = format_h.format(new Date());
- //相恋的日期
- String lian = "2022-03-28";
- //两个日期相差的天数
- int cha = DateUtils.getDayByTwoDay(toDate, lian);
- String todayDate = format.format(new Date());
-
- //根据阳历计算农历
- Calendar today = Calendar.getInstance();
- String week = DateUtils.getDaysByWeekChina(toDate);
- today.setTime(DateUtils.chineseDateFormat.parse(todayDate));
- DateUtils lunar = new DateUtils(today);
- String nongli = lunar.toString();
- nongli = nongli.substring(nongli.indexOf("年") + 1);
- StringBuilder sb = new StringBuilder();
- sb.append("亲爱的老婆~早\n\n")
- .append("充满希望的一天又开始了,今天也要一直开心,不能生我气气~")
- .append("\n\n今天是:")
- .append(todayDate)
- .append(",星期")
- .append(week)
- .append(",农历")
- .append(nongli)
- .append("\n")
- .append("是我们相爱的第:")
- .append(cha)
- .append("天")
- .append("\n\n")
-
- ;
- JSONObject tianqiObject = TianXingApiUtil.getWeather();
- sb.append("目前济南天气:")
- .append(tianqiObject.getString("weather"))
- .append("\n")
- .append("当前气温:")
- .append(tianqiObject.getString("real"))
- .append("\n")
- .append("今天最高温度:")
- .append(tianqiObject.getString("highest"))
- .append("\n")
- .append("最低温度:")
- .append(tianqiObject.getString("lowest"))
- .append("\n")
- .append("风向:")
- .append(tianqiObject.getString("wind"))
- .append("\n")
- .append("空气质量:")
- .append(tianqiObject.getString("quality"))
- .append("\n")
- .append("空气质量指数:")
- .append(tianqiObject.getString("aqi"))
- .append("\n")
- .append("小提示:")
- .append(tianqiObject.getString("tips"));
-
- //土味情话
- sb.append("\n\n")
- .append("爱你一日言:")
- .append(TianXingApiUtil.getLoveWords())
- .append("\n\n【早安寄语】")
- .append(goodMorning());
- weiZhiMiShuVO.setContent(sb.toString());
- for (String str : SEND_USER) {
- JSONObject object = new JSONObject();
- object.put("apiKey", WEI_MI_SHU_KEY);
- //好友昵称
- object.put("name", str);
- //好友备注
- object.put("alias", str);
- object.put("message", weiZhiMiShuVO);
- String body = HttpUtil.createPost(url)
- .body(JSON.toJSONString(object))
- .execute()
- .body();
- System.out.println("传递的参数是:" + JSON.toJSONString(object));
- System.out.println("发送成功了吗?" + body);
- }
-
- } catch (ParseException e) {
- e.printStackTrace();
- }
- }
-
- 复制代码
说明:
WeiZhiMiShuVO
类可以参考文章:java实现给微信群中定时推送消息SEND_USER
是要发给微信好友的好友昵称,不是备注哈。然后我们在控制器controller
中调用该方法即可实现:
- /**
- * 发送消息给指定的人
- */
-
- @GetMapping(value = "/sendWechatToUser")
- @Scheduled(cron ="0 0 8 * * ?")
- public void sendWechatToUser(){
- articleService.sendWechatToUser();
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。