微信公众号发送模版消息 Java实现

微信公众号发送模版消息

背景:

当用户发布任务的时候,公众号会自动推送消息通知。例如我们都熟悉的场景:微信支付的时候,公众号会推送支付成功消息。
微信公众号发送模版消息 Java实现_第1张图片

申请模版:

模版消息,顾名思义,就是有模版的消息,那么要模版干嘛呢?模版是从哪来呢?
发送消息需要有固定的格式,我们可以在微信公众号平台上配置模版。

微信公众号平台–>广告与服务–>模版消息–>我的模版
「我的模版」列表里的是已经申请的模版,如果里面的模版格式都不符合自己业务,可以到模版库里找,然后添加到「我的模版」。也可以按照自己的需求申请新的模版,一般第二个工作日会审核通过。

https://mp.weixin.qq.com/
微信公众号发送模版消息 Java实现_第2张图片

打开模版详情,查看模版的格式,下图左边红框是消息最终展示的效果,
右边红框是需要传的参数。
微信公众号发送模版消息 Java实现_第3张图片
有了模版之后,模版ID就是我们要放进代码里的,把模版ID复制出来。

发送模版消息接口文档:

消息模版准备好之后,暂时不要写代码奥,查看微信开发文档,看看发送模版都需要哪些参数。

微信开发文档–>基础消息能力–>模版消息接口–「发送模版消息」
https://developers.weixin.qq.com/doc/offiaccount/Message_Management/Template_Message_Interface.html
微信公众号发送模版消息 Java实现_第4张图片

微信开发文档参数介绍

发送模版消息

http请求方式: POST 请求地址: https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=ACCESS_TOKEN
微信公众号发送模版消息 Java实现_第5张图片

注:
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;
    /**
     * 消息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工具类

package com.demo.advertiser.common.utils;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cglib.beans.BeanMap;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@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工具类

package com.demo.advertiser.common.utils;

import com.google.common.base.Splitter;
import com.demo.advertiser.common.utils.component.HttpResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.LaxRedirectStrategy;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URI;
import java.net.URL;
import java.net.URLDecoder;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@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;
    }
}
package com.demo.advertiser.common.utils.component;

public class HttpResult {
	private Integer status = 601;
	private String body = "";
	
	public Integer getStatus() {
		return status;
	}
	
	public void setStatus(Integer status) {
		this.status = status;
	}
	
	public String getBody() {
		return body;
	}
	
	public void setBody(String body) {
		this.body = body;
	}
}

你可能感兴趣的:(java,微信公众号,模版消息)