首页 > 编程知识 正文

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

时间:2023-05-04 10:34:16 阅读:272266 作者:3405

采用定时器或者时间监听机制,发送微信模板消息
微信模板消息官方文档


代码实现思路

首先需要一个微信测试号,并配置一个消息模板

第一步:拿到微信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封装 @Datapublic 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;}@Datapublic 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("ke一分钟极速赛车靠谱平台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(); }@Datapublic class WeChatResponse { /** * 0 表示成功 */ Integer errcode; /** * ok 表示成功 */ String errmsg; Long msgid;}

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。