微信每日早安问候

1 先注册一个微信公众平台的测试账号

image-20220830161707310

2 使用微信扫测试号二维码并关注,列表里面的微信号就是openid

image-20220830161647214

3 新建一个消息模板

image-20220830162158033

 模板参考
 {{date.DATA}}
 早安 宝贝!
 今日寄言:{{content.DATA}}
 English:{{note.DATA}}
 每日一笑: {{jock.DATA}}

4 使用idea 新建一个springboot工程,java版本选8

image-20220830162631268

  • 引入需要的依赖

         <!-- 阿里JSON解析器 -->
        <dependency>
            <groupId>com.alibaba.fastjson2</groupId>
            <artifactId>fastjson2</artifactId>
            <version>2.0.12</version>
        </dependency>
        <!-- hutool工具类-->
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.8.5</version>
        </dependency>

5 开始发消息的代码编写

 参考文档 https://developers.weixin.qq.com/doc/offiaccount/Basic_Information/Get_access_token.html
 参考代码
 public static String getToken() {
         String data = HttpUtil.get("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=你的appid&secret=你的secret");
         final JSONObject jsonObject = JSON.parseObject(data);
         return jsonObject.getString("access_token");
    }
  • 第二步 获取每日一句,使用的是金山词霸的每日一句接口

 参考代码
 public static Map<String, String> getOneDay() {
         String data = HttpUtil.get("http://open.iciba.com/dsapi/");
         final JSONObject jsonObject = JSON.parseObject(data);
         final String content = jsonObject.getString("content");
         final String note = jsonObject.getString("note");
         final Map<String, String> map = new HashMap<>(3);
         map.put("content", content);
         map.put("note", note);
         return map;
    }
 参考代码
 public static String getJoke() {
         String data = HttpUtil.get("https://www.mxnzp.com/api/jokes/list/random?app_id=sqnmugmnspqditkh&app_secret=akw0SkhCOVoweWhWT3FQQ3dJNHgxUT09");
         final JSONObject jsonObject = JSON.parseObject(data);
         return jsonObject.getJSONArray("data").getJSONObject(0).getString("content");
    }
  • 第四步 给关注了该测试账号的微信发消息

 参考文档
 https://developers.weixin.qq.com/doc/offiaccount/Message_Management/Template_Message_Interface.html#%E5%8F%91%E9%80%81%E6%A8%A1%E6%9D%BF%E6%B6%88%E6%81%AF
 参考代码
 public static void sendOneDay() {
         System.out.println("========================定时任务开始执行==========================");
         final String token = getToken();
         final Map<String, String> oneDay = getOneDay();
         String url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + token;
         JSONObject json = new JSONObject();
         JSONObject json1 = new JSONObject();
         json.put("touser", "你的发送的微信的openid");
         json.put("template_id", "你的消息模板id");
         json.put("data", json1);
 
         JSONObject json2 = new JSONObject();
         json1.put("note",json2);
         json2.put("value",oneDay.get("content"));
         json2.put("color","#00BFFF");
 
         JSONObject json3 = new JSONObject();
         json3.put("value",oneDay.get("note"));
         json3.put("color","#FF69B4");
         json1.put("content",json3);
 
         JSONObject json4 = new JSONObject();
         json1.put("jock",json4);
         json4.put("value",getJoke());
         json4.put("color","#FF69B4");
         String host = HttpRequest.post(url)
                .body(json.toJSONString())
                .execute().body();
         System.out.println("========================每日一句任务执行结束,打印接口返回值==========================");
         System.out.println(LocalDateTime.now());
         System.out.println(host);
 
    }
  • 第五步 测试消息是是否发送成功

测试消息发送 可以使用自己的微信关注该账号,给自己发消息
参考测试代码
    public static void main(String[] args) {
        sendOneDay();
    }
  • 第六步 测试完成后 加上定时任务 让消息定时推送

cron表达式在线生成网站 http://cron.ciding.cc/
添加定时任务的方法
1 启动类添加@EnableScheduling
2 消息发送类添加@Component
3 在需要定时执行的方法上添加@Scheduled(cron = "0 0 9 * * ? ")
  • 第六步 将代码打包 发送到服务器启动即可(该步骤不详细描述)

推荐一个大佬写的更加完善的一个版本https://www.bilibili.com/video/BV1VP41157Z7?spm_id_from=333.999.0.0&vd_source=90d2765424c7998e9aa4aeafebebd81b

posted @ 2022-08-30 17:09  小猫爱哭鬼  阅读(521)  评论(0编辑  收藏  举报