微信公众号-发送模板消息

官方文档地址:https://developers.weixin.qq.com/doc/offiaccount/Message_Management/Template_Message_Interface.html#5

也可以参考此网址:https://blog.csdn.net/zhuzhezhuzhe1/article/details/83927016

 

微信公众号-发送模板消息

 

 

 

申请的模板样式如上图,按照此模板样式开发

 

1、定时任务,每天早晨6点发送

2、查询要发送的数据

3、循环List数据,发送模板内容

/*** 发送模板消息
* @param wechatDto wechatDto
* @return JSONObject
* @throws BusinessException
*/
public JSONObject sendTemplateMessage(WechatDTO wechatDto) throws BusinessException {
logger.debug("进入了sendTemplateMessage方法");
try {
String template_id = "";

String access_token = this.getToken();
String url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + access_token;

JSONObject data = new JSONObject();

JSONObject first = new JSONObject();
first.put("value", "该订单****未接单");

// 订单编号
JSONObject keyword1 = new JSONObject();
keyword1.put("value", wechatDto.getOrderNum());

// 用户姓名
JSONObject keyword2 = new JSONObject();
keyword2.put("value", wechatDto.getUserName());

// 用户手机号
JSONObject keyword3 = new JSONObject();
keyword3.put("value", wechatDto.getPhone());

// 用户预约时间
JSONObject keyword4 = new JSONObject();
keyword4.put("value", wechatDto.getTimeCustomer());

// 用户地址
JSONObject keyword5 = new JSONObject();
keyword5.put("value", wechatDto.getAddress());

JSONObject remark = new JSONObject();
remark.put("value", "请尽快处理!");

data.put("first", first);
data.put("keyword1", keyword1);
data.put("keyword2", keyword2);
data.put("keyword3", keyword3);
data.put("keyword4", keyword4);
data.put("keyword5", keyword5);
data.put("remark", remark);

// List<NameValuePair> params = new ArrayList<>();
// params.add(new BasicNameValuePair("touser", wechatDto.getOpenid()));
// params.add(new BasicNameValuePair("template_id", template_id));
// params.add(new BasicNameValuePair("data", data.toString()));
JSONObject json = new JSONObject();
json.put("touser", wechatDto.getOpenid());
json.put("template_id", template_id);
json.put("data", data);

//String responseJson = HttpClientUtils.executeByPOST(url, params);
String responseJson = this.executeByPOST(url, json.toJSONString());
JSONObject result = JSON.parseObject(responseJson);
result.put("data", data.toString());
result.put("template_id", template_id);
return result;
} catch (Exception ex) {
if (ex instanceof BusinessException) {
throw new BusinessException(ex);
} else {
logger.error(ex.getMessage());
throw new BusinessException("数据异常:" + ex.getMessage(), 500, null);
}
}
}
// 获取access_token
private String getToken() throws BusinessException {
try {
String url = String.format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s", wechatAppId, wechatAppSecret);
String responseJson = HttpClientUtils.executeByGET(url);
JSONObject result = JSONObject.parseObject(responseJson);
return result.getString("access_token");
} catch (Exception ex) {
if (ex instanceof BusinessException) {
throw new BusinessException(ex);
} else {
logger.error(ex.getMessage());
throw new BusinessException("数据异常:" + ex.getMessage(), 500, null);
}
}
}
//private String executeByPOST(String url, List<NameValuePair> params) {
private String executeByPOST(String url, String params) {
HttpClient httpclient = HttpClientUtils.getHttpClient();
new ArrayList();
HttpPost post = new HttpPost(url);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseJson = null;

try {
if (params != null) {
// 中文乱码问题 ContentType.create("application/json", "UTF-8")

       //HTTPClient进行body传参,要使用StringEntity,而不要使用UrlEncodedFormEntity

       //原因:UrlEncodedFormEntity会以字符串键值对形式传给后台,即:{"a":"value1", "b":"value2"},传给java方法,接收到的参数是:a=value1&b=value2,即它不支持json参数传递;

       而StringEntity传参,后台接收到的依然是 {"a":"value1", "b":"value2"},即StringEntity能传递json,当然,如果你传递的就是一个普通的字符串,StringEntity也是支持的。

      //post.setEntity(new UrlEncodedFormEntity(params));
      post.setEntity(new StringEntity(params, ContentType.create("application/json", "UTF-8")));
}

responseJson = (String)httpclient.execute(post, responseHandler);
logger.info("HttpClient POST请求结果:" + responseJson);
} catch (ClientProtocolException var12) {
var12.printStackTrace();
logger.info("HttpClient POST请求异常:" + var12.getMessage());
} catch (IOException var13) {
var13.printStackTrace();
} finally {
httpclient.getConnectionManager().closeExpiredConnections();
httpclient.getConnectionManager().closeIdleConnections(30L, TimeUnit.SECONDS);
}

return responseJson;
}


注意:

微信公众号-发送模板消息

 

 

传参的值,截图中红色部分

要注意的是  格式要完全一样   比如 时间  如果传一个  2020-11-09 22:23:34是不行的 因为标题不能包含空格

要严格按照 模板里给的格式传值 !!

 

上一篇:记录一次*Error的发生


下一篇:个人如何接入微信支付宝收款?站长付个人收款如何接入?