Java实现微信公众号发送模版消息
作者:热水瓶、 发布时间:2021-07-16 17:03:35
微信公众号发送模版消息 背景:如下图,当用户发布需求的时候,公众号自定推送消息。例如:微信支付的时候,公众号会推送支付成功消息
前提:发送模版消息,顾名思义,前提就是需要有模版,那么在哪里配置模版呢?
微信公众号平台–>广告与服务–>模版消息–>我的模版
模版消息是已经申请过的模版,如果里面的模版都不符合自己业务的话,可以到模版库里找,然后添加到「我的模版」。也可以按照自己的需求申请新的模版,一般第二个工作日会审核通过。
在模版详情可以查看模版的格式,下图左边红框是消息最终展示的效果,
右边红框是需要传的参数。
有了模版之后,模版ID就是我们要放进代码里的,复制出来。
消息模版准备好之后,暂时不要写代码奥,查看微信开发文档,看看发送模版都需要哪些参数。
微信开发文档–>基础消息能力–>模版消息接口–「发送模版消息」
查看微信开发文档
发送模版消息
http请求方式: POST https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=ACCESS_TOKEN注:url和miniprogram都是非必填字段,若都不传则模板无跳转;若都传,会优先跳转至小程序。开发者可根据实际需要选择其中一种跳转方式即可。当用户的微信客户端版本不支持跳小程序时,将会跳转至url。
返回码说明
在调用模板消息接口后,会返回JSON数据包。
正常时的返回JSON数据包示例:
{
“errcode”:0,
“errmsg”:“ok”,
“msgid”:200228332
}
发送模版所需参数:
模版ID和openId是必须有的,剩下的就是和自己业务有关了。
上面的内容都搞定之后,就可以开始撸代码了
发送模版微信返回Dto
@Data
public class TemplateMsgResultDto extends ResultState {
/**
* 消息id(发送模板消息)
*/
private String msgid;
}
发送模版微信返回状态
@Data
public class ResultState implements Serializable {
/**
* 状态
*/
private int errcode;
/**
* 信息
*/
private String errmsg;
}
微信模版消息请求参数实体类
@Data
public class WxTemplateMsg {
/**
* 接收者openId
*/
private String touser;
/**
* 模板ID
*/
private String template_id;
/**
* 模板跳转链接
*/
private String url;
// "miniprogram":{ 未加入
// "appid":"xiaochengxuappid12345",
// "pagepath":"index?foo=bar"
// },
/**
* data数据
*/
private TreeMap<String, TreeMap<String, String>> data;
/**
* 参数
*
* @param value 值
* @param color 颜色 可不填
* @return params
*/
public static TreeMap<String, String> item(String value, String color) {
TreeMap<String, String> params = new TreeMap<String, String>();
params.put("value", value);
params.put("color", color);
return params;
}
}
Java封装模版信息代码
public TemplateMsgResultDto noticeTemplate(TemplateMsgVo templateMsgVo) {
// 模版ID
String templateId="XXX";
TreeMap<String, TreeMap<String, String>> params = new TreeMap<>();
//根据具体模板参数组装
params.put("first", WxTemplateMsg.item("恭喜!您的需求已发布成功", "#000000"));
params.put("keyword1", WxTemplateMsg.item(templateMsgVo.getTaskName(), "#000000"));
params.put("keyword2", WxTemplateMsg.item("需求已发布", "#000000"));
params.put("remark", WxTemplateMsg.item("请耐心等待审核", "#000000"));
WxTemplateMsg wxTemplateMsg = new WxTemplateMsg();
// 模版ID
wxTemplateMsg.setTemplate_id(templateId);
// openId
wxTemplateMsg.setTouser(templateMsgVo.getOpenId());
// 关键字赋值
wxTemplateMsg.setData(params);
String data = JsonUtils.ObjectToString(wxTemplateMsg);
return handleSendMsgLog(data);
}
发送模版代码
private TemplateMsgResultDto handleSendMsgLog(String data) {
TemplateMsgResultDto resultDto = new TemplateMsgResultDto();
try {
resultDto = sendTemplateMsg(data);
} catch (Exception exception) {
log.error("发送模版失败", exception);
}
// TODO 可以记录一下发送记录的日志
return resultDto;
}
public TemplateMsgResultDto sendTemplateMsg(String data) throws Exception {
// 获取token
String accessToken = getAccessToken();
// 发送消息
HttpResult httpResult = HttpUtils.stringPostJson(ConstantsPath.SEND_MESSAGE_TEMPLATE_URL + accessToken, data);
return IMJsonUtils.getObject(httpResult.getBody(), TemplateMsgResultDto.class);
}
/**
* 获取全局token
*/
public String getAccessToken() {
String key = ConstantsRedisKey.ADV_WX_ACCESS_TOKEN;
// 从redis缓存中获取token
if (redisCacheManager.get(key) != null) {
return (String) redisCacheManager.get(key);
}
// 获取access_token
String url = String.format(ConstantsPath.WX_ACCESS_TOKEN_URL, appid, secret);
ResponseEntity<String> result = restTemplate.getForEntity(url, String.class);
if (result.getStatusCode() == HttpStatus.OK) {
JSONObject jsonObject = JSON.parseObject(result.getBody());
String accessToken = jsonObject.getString("access_token");
// Long expires_in = jsonObject.getLong("expires_in");
redisCacheManager.set(key, accessToken, 1800);
return accessToken;
}
return null;
}
微信地址常量类
public class ConstantsPath {
/**
* 微信公众号获取全局token
*/
public static final String WX_ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s";
/**
* 微信发送模版消息
*/
public static final String SEND_MESSAGE_TEMPLATE_URL = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=";
}
Json工具类
@Slf4j
public class JsonUtils {
private static ObjectMapper json;
static {
json = new ObjectMapper();
json.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
json.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
}
/**
* 序列化为JSON字符串
*/
public static String ObjectToString(Object object) {
try {
return (json.writeValueAsString(object));
} catch (Exception e) {
log.error("序列化为JSON字符串出错",e);
}
return null;
}
public static <T> T getObject(String jsonString, Class<T> clazz) {
if (StringUtils.isEmpty(jsonString))
return null;
try {
return json.readValue(jsonString, clazz);
} catch (Exception e) {
log.error("将JSON字符串转化为Map出错",e);
return null;
}
}
}
Http工具类
@Component
@Slf4j
public class HttpUtils {
private static String sourcePath;
public static HttpResult stringPostJson(String path, String content) throws Exception{
return stringPost(path, null, content, "utf-8", "utf-8", "application/json");
}
public static HttpResult stringPost(String path, Map<String,String> headerMap, String content, String contentencode, String encode, String contentType) throws Exception{
StringEntity entity = new StringEntity(content, contentencode);
entity.setContentType(contentType);
return post(path, headerMap, entity, encode);
}
private static HttpResult post(String path, Map<String,String> headerMap, HttpEntity entity, String encode){
HttpResult httpResult = new HttpResult();
CloseableHttpClient httpClient = null;
CloseableHttpResponse response = null;
try{
HttpPost httpPost = new HttpPost(getURI(path));
LaxRedirectStrategy redirectStrategy = new LaxRedirectStrategy();
httpClient = HttpClientBuilder.create().setRedirectStrategy(redirectStrategy).build();
RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(120000)
.setConnectTimeout(120000)
.setConnectionRequestTimeout(120000)
.setCircularRedirectsAllowed(true)
.setRedirectsEnabled(true)
.setMaxRedirects(5)
.build();
httpPost.setConfig(requestConfig);
httpPost.setHeader("User-Agent", header);
if(headerMap != null && headerMap.size() > 0){
for(String name:headerMap.keySet()) {
httpPost.addHeader(name, headerMap.get(name));
}
}
httpPost.setEntity(entity);
response = httpClient.execute(httpPost);
httpResult.setStatus(response.getStatusLine().getStatusCode());
if(httpResult.getStatus() == 200){
HttpEntity resEntity = response.getEntity();
httpResult.setBody(EntityUtils.toString(resEntity, encode));
}
}catch(Exception ex){
log.error("post请求出错", ex);
}finally{
try{
if(response != null){
response.close();
}
if(httpClient != null){
httpClient.close();
}
}catch(Exception ex) {
log.error("post请求关闭资源出错", ex);
}
}
return httpResult;
}
}
来源:https://blog.csdn.net/mxs1226/article/details/122731633


猜你喜欢
- 偶然发现有小伙伴错误地使用了Collections.emptyList()方法,这里记录一下。她的使用方式是:public void run
- 目录引言简单遍历筛选符合某属性条件的List集合获取某属性返回新的List集合获取以某属性为key,其他属性或者对应对象为value的Map
- 原因分析@Anysc注解会开启一个新的线程,主线程的Request和子线程是不共享的,所以获取为null在使用springboot的自定带的
- @ModelAttribute在父类、子类的执行顺序被 @ModelAttribute 注解的方法会在Controller每个方法执行之前都
- //Main:using System;using System.Collections.Generic;using System.Linq
- //去title requestWindowFeature(Window.FEATURE_NO_TITLE); //隐藏状态栏 getWin
- 前言前几天多名用户反馈同一个问题,在小新平板上无法上网课,点击上课按钮后就退回到首页了。同事了解了一下发现小新平板现在销量特别好,于是赶紧申
- 一、jaxb是什么 JAXB是Java Architecture for XML Bindi
- 前言前面说过了类的加载机制,里面讲到了类的初始化中时用到了一部分内存管理的知识,这里让我们来看下Java虚拟机是如何管理内存的。先让我们来看
- 一、新建学生节点类Stu_Node节点包含:学号:int num;姓名:String name;性别:String gender;下一个节点
- 使用百度地图出现闪退一般情况下出现闪退是在AndroidManifest.xml文件中未在application标签中配置<meta-
- 本文采用半译方式。在本文中,将会介绍 C# 7.2 中引入的新类型:Span 和 Memory,文章深入研究 Span<T&
- 可能导致问题的原因:1.nacos中的配置文件名不规范,官网有命名规则:“前缀”-&ldqu
- 常用的字符串转date,和日期转字符串的方法,具体内容如下package com.cq2022.zago.base.util; import
- 一、银行存取款1.前言毕竟谁不喜欢钱呢!(不是😅)我看谁不喜欢在知识的海洋中遨游😤!2.描述银行存取款的流程是人们非常熟悉的事情,用户可以在
- Android中实现全屏、无标题栏的两种办法,另附Android系统自带样式的解释实现全屏无标题栏:1.在xml文件中进行配置 Androi
- 前言本文主要介绍的是关于C#中LINQ多条件JOIN时为什么可以使用匿名类的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细
- 一、程序运行环境编译环境:IntelliJ IDEA所需测试文件:PDF、.pfx数字证书及密钥、PDF Jar包(Free Spire.P
- 前言通过前面这篇文章Android串口通讯SerialPort的使用详情已经基本掌握了串口的使用,那么不经想问自己,到底什么才是串口通讯呢?
- 一.应用场景平时在建对象表的时候都会有最后修改时间,最后修改人这两个字段,对于这些大部分表都有的字段,每次在新增和修改的时候都要考虑到这几个