栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

公众号可以主动发送模版消息(微信公众号模板消息)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

公众号可以主动发送模版消息(微信公众号模板消息)

最近看了一个地推的公众号,每天都会给你推送好几条地推需要的模板消息,好一段时间没有做公众号开发了,最近刚申请了个服务号,刚好可以拿来开发。模板消息需要服务号才可以,申请服务号的话需要企业营业执照,个人的话是没有办法申请的。下面来分享一下我的开发过程。

开发步骤 1.微信公众号服务号 2.准备好 APPID 跟AppSecret 3.开通模板消息,申请一个模板,获取模板ID 4.获取ACCESS_TOKEN 5.获取关注公众号的用户列表 6.选择需要发送的用户,并推送消息 一、资料申请 1.1、获取 APPID 跟AppSecret


1.2、获取 模板消息ID

找不到的话,就在新功能上面去添加;

二、开发 2.1、编写yml配置文件
#公众号配置
wechat:
    appid:  APPID
    appkey: APP密钥
    messageId: 模板ID
2.2、编写配置类
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
@Data
public class WechatConfig {
    @Value("${wechat.appid}")
    private String appid;
    @Value("${wechat.appkey}")
    private String appkey;
}
2.3、获取ACCESS_TOKEN

这里有点类似登录吧,就是用咱们的APPID跟APP密钥送到官方接口那边去验证身份,如果没问题人家就给你返回一个token,方便后面调用接口可以传,让官方接口知道是谁发过来的。token是临时的,有效期是2个小时.

2.3.1、导入工具类 (hutool)maven依赖
		
			cn.hutool
			hutool-all
			${hutool.version}
		
2.3.2、导入工具类 (hutool)maven依赖
@Component
public class ConfigurationService {
    private String accessTokenUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&";

    @Autowired
    private WechatConfig wechatConfig;

    public JSonObject getAccessToken() {
        String requestUrl = accessTokenUrl + "appid=" + wechatConfig.getAppid() + "&secret=" + wechatConfig.getAppkey();
        String resp = HttpUtil.get(requestUrl);
        JSonObject result = JSONUtil.parseObj(resp);
        System.out.println("获取access_token:" + resp);

        return result;
    }
}

2.4、获取关注你公众号的用户列表

因为我们在发送模板消息的时候,需要填推送的对象(openid),
所以我们需要先查询出用户,再一个个推送。

    

    public JSonObject getUserList(String accessToken) {
        String requestUrl = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=" + accessToken + "&next_openid=";
        String resp = HttpUtil.get(requestUrl);
        JSonObject result = JSONUtil.parseObj(resp);
        System.out.println("用户列表:" + resp);
        return result;
    }

next_openid=这个可以为空,代表从头查起。也可以放上某个用户的openid,发送请求的到时候,用来告诉官方接口从哪个用户开始查起。因为查询一次,最多只能查一万条数据,假设你有几万个用户,那么你就需要查询好几次了,就需要用到这个参数。

2.5、发送模板消息 2.5.1、新建消息配置类

因为我是将把配置写到数据库里面了,方便后面添加发送任务。大家不一定要做这部操作哈。!

@Data
@TableName("template_msg")
public class TemplateMsgEntity implements Serializable {
    private static final long serialVersionUID = 1L;

    
    @TableId
    private Integer id;
    
    private String tTitle;
    
    private String tKeyword1;
    
    private String tKeyword2;
    
    private String tKeyword3;
    
    private String tKeyword4;
    
    private String tRemark;
    
    private String tUrl;
    
    private String tCode;
    
    private int tStatus;
}

模板发送

  public JSonObject sendMsg(TemplateMsgEntity messageVo, String token, String openId) {
        String requestUrl = " https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="  + token;
        Map content=new HashMap<>();
        JSonObject data = JSONUtil.createObj();
        data.put("first",new JSonObject().put("value",messageVo.getTTitle()));
        data.put("keyword1",new JSonObject().put("value",messageVo.getTKeyword1()));
        data.put("keyword2",new JSonObject().put("value",messageVo.getTKeyword2()));
        data.put("keyword3",new JSonObject().put("value",messageVo.getTKeyword3()));
        data.put("keyword4",new JSonObject().put("value",messageVo.getTKeyword4()));
        data.put("remark",new JSonObject().put("value",messageVo.getTRemark()));
        
        content.put("touser",openId);
        content.put("url",messageVo.getTUrl());
        content.put("template_id","jbgHt8W8RNpRN2KZwvAMty40iiZU2sa9dqnFXOsCvqw");
        content.put("data",data);
        String resp = HttpUtil.post(requestUrl,JSONUtil.parseFromMap(content).toString());
        System.out.println(content.toString());
        System.out.println(JSONUtil.parseFromMap(content));
        JSonObject result = JSONUtil.parseObj(resp);
        System.out.println("发送消息:" + resp);
        return result;
    }

接口字段要对应着来。
官方文档:https://mp.weixin.qq.com/advanced/tmplmsg?action=faq&token=1066457355&lang=zh_CN

有个需要注意的地方就是,模板封装好之后,推送到官方提供的接口时,数据类型是String类型哦,别整个对象就过来,那样解析不了的。

三、测试
  
    @GetMapping( "/sedMsg")
    public JSonObject sedMsg(){
      JSonObject accessToken = configurationService.getAccessToken();
      String token=accessToken.getStr("access_token");
       //获取用户列表
        JSonObject userList = configurationService.getUserList(token);
        JSonArray openids = userList.getJSonObject("data").getJSonArray("openid");
        System.out.println(openids.toArray());
        TemplateMsgEntity messageVo=new TemplateMsgEntity();
        messageVo.setTTitle("标题");
        messageVo.setTKeyword1("测试1");
        messageVo.setTKeyword2("测试2");
        messageVo.setTKeyword3("测试3");
        messageVo.setTKeyword4("测试4");
        messageVo.setTRemark("remark");
        for (Object openid:openids) {
            JSonObject resp = configurationService.sendMsg(messageVo,token,openid.toString());

        }
        return null;
    }

四、优化

由于ACCESS_TOKEN只有两个小时就过期,所以我们应该专门写一个服务,或者一个定时间任务,隔一段时间就去读取数据库中记录的access_token,并且将写入access_token的时间跟现在的时间进行比较,大概超过117分钟时,就重新刷新写到数据中去。思路是这样的,大家可以自己写哈,方式很多。

@Configuration
@EnableScheduling
public class MessageTask {
    @Autowired
    SysConfigService sysConfigService;
    @Autowired
    ConfigurationService configurationService;
    @Autowired
    TemplateMsgService templateMsgService;
    
    @Scheduled(fixedRate = 120000)
    public void tokenTask(){
        System.out.println("定时任务开启:");
        //获取token
        String token = sysConfigService.getValue("ACCESS_TOKEN");
        
        JSonObject json = JSONUtil.parseObj(token);
        String expires_date = json.getStr("expires_date");
        //token创建的时间
        DateTime parse = DateUtil.parse(expires_date);
        //跟现在的时间对比是否大于7000s 
        long between = DateUtil.between(parse, new DateTime(), DateUnit.MINUTE);
        System.out.println("时间比较:"+between);
        //快过期了,重新刷新token
        if (between>115){ 
            JSonObject accessToken = configurationService.getAccessToken();
            if (accessToken.getStr("expires_in")!=null&&accessToken.getStr("expires_in").equals("7200")){
                accessToken.put("expires_date",DateUtil.now().toString());
                sysConfigService.updatevalueByKey("ACCESS_TOKEN",accessToken.toString());
                System.out.println("token更新了:"+accessToken.toString());
            }
            
        }

    }
   
}

希望能帮助到大家

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/773231.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号