微信公众号模板消息配置和发送

采用定时器或者时间监听机制,发送微信模板消息
微信模板消息官方文档
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

代码实现思路

首先需要一个微信测试号,并配置一个消息模板
在这里插入图片描述
在这里插入图片描述

第一步:拿到微信appid和secret获取到access_token,然后得到我们请求微信发送模板消息的接口
第二步:封装发送的json数据,需要接受者的openid(唯一标识用户的id),模板id(templatedId),传输的数据(data)

上代码

一:根据模板封装一个map对象

    HashMap<String, String> map = new HashMap<>();
    map.put("openId", "oOTJU60hq-mQ9tGKurMuAPFcFIxg");
    map.put("template_id", "-d7t3A4gBhejn0j-feResggYBxKJIRuPdUFE-GZE2eE");
		
    map.put("first", "尊敬的助学者,您有一张卡券即将过期失效");
    map.put("keyword1",9999);
    map.put("keyword2", 120 + "元");
    map.put("keyword3","9999年09月09日");
    String remark = "请及时使用!";
    map.put("remark", remark);

二:map对象和openId,templateId封装

@Data
public class TemplateData {
    private Map<String,String> first;
    private Map<String,String> keyword1;
    private Map<String,String> keyword2;
    private Map<String,String> keyword3;
    private Map<String,String> keyword4;
    private Map<String,String> remark;
}

@Data
public class TemplateMessage {
    //openId
    private String touser;
    //template_id
    private String template_id;
    //color--非必须
    private String topcolor;
    //data
    public TemplateData data;
    //url
    public String url;
}

    // 封装
    TemplateData templateData = new TemplateData();
    HashMap<String, String> first = new HashMap<>();
    HashMap<String, String> keyword1 = new HashMap<>();
    HashMap<String, String> keyword2 = new HashMap<>();
    HashMap<String, String> keyword3 = new HashMap<>();
    HashMap<String, String> remark = new HashMap<>();
    
    // 此map来自上面封装的map对象
    first.put("value", map.get("first"));
    keyword1.put("value", map.get("keyword1"));
    keyword2.put("value", map.get("keyword2"));
    keyword3.put("value", map.get("keyword3"));
    remark.put("value", map.get("remark"));
    
    templateData.setFirst(first);
    templateData.setKeyword1(keyword1);
    templateData.setKeyword2(keyword2);
    templateData.setKeyword3(keyword3);
    templateData.setKeyword4(keyword4);
    templateData.setRemark(remark);

    TemplateMessage templateMessage = new TemplateMessage();
    templateMessage.setTouser(map.get("openId"));
    templateMessage.setTemplate_id(map.get("template_id"));
    templateMessage.setData(templateData);

三:向用户发送模板消息

需要两个参数,一个是需要发送的数据,一个是请求微信的接口

获取微信发送模板消息的接口
String urlPOST = https://api.weixin.qq.com/cgi-bin/message/template/send?access_token= 你自己的token"
开始发送消息

  /**
  * @param templateMessage 第二步封装的数据
  * @param   urlPOST  带上自己的accesstoken 的请求路径
  */
    public void send (TemplateMessage templateMessage, String urlPOST) {
        log.info("[微信模板发送]");
        //将对象转成JSON字符串
        String message =new ObjectMapper().writeValueAsString(templateMessage) ;
        log.info("[模板发送内容:]{}",message);
        //响应头设置参数: JSON字符串|字符集
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.parseMediaType("application/json;charset=UTF-8"));
        //这里需要一个方法,向url发送POST请求
        WeChatResponse body = HttpRequestUtil.weChatpost(urlPOST, message, headers); // 见下面代码
        log.info("[模板发送返回值:]{}",body);
    }

    //请求微信模板消息发送接口
    public static WeChatResponse weChatpost(String url, String params, HttpHeaders headers) {
        RestTemplate client = new RestTemplate();
        HttpMethod method = HttpMethod.POST;
        if (headers == null) {
            headers = new HttpHeaders();
        }
        //将请求头部和参数合成一个请求
        HttpEntity<String> entity = new HttpEntity<>(params, headers);
        //执行HTTP请求,将返回的结构使用ResultVO类格式化
        ResponseEntity<WeChatResponse> response = client.exchange(url, method, entity, WeChatResponse.class);
        return response.getBody();
    }


@Data
public class WeChatResponse {
    /**
     * 0 表示成功
     */
    Integer errcode;
    /**
     * ok 表示成功
     */
    String errmsg;

    Long msgid;
}
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值