效果
以测试公众号为例
- 点击进入 测试公众号
- 官方教程
- 点击新增目标模板
- 代码
WxTemplate 类
package com.project.voicememoassistant.model;
import java.util.Map;
public class WxTemplate {
/**
* 模板消息id
*/
private String template_id;
/**
* 用户openId
*/
private String touser;
/**
* URL置空,则在发送后,点击模板消息会进入一个空白页面(ios),或无法点击(android)
*/
private String url;
/**
* 标题颜色
*/
private String topcolor;
/**
* 详细内容
*/
private Map<String,TemplateData> data;
public String getTemplate_id() {
return template_id;
}
public void setTemplate_id(String template_id) {
this.template_id = template_id;
}
public String getTouser() {
return touser;
}
public void setTouser(String touser) {
this.touser = touser;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getTopcolor() {
return topcolor;
}
public void setTopcolor(String topcolor) {
this.topcolor = topcolor;
}
public Map<String, TemplateData> getData() {
return data;
}
public void setData(Map<String, TemplateData> data) {
this.data = data;
}
}
TemplateData 类
package com.project.voicememoassistant.model;
public class TemplateData {
private String value;
private String color;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
}
发送方法
openId就是关注的用户列表中的微信号
package com.project.voicememoassistant.tools;
import com.project.voicememoassistant.model.TemplateData;
import com.project.voicememoassistant.model.WxTemplate;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.client.RestTemplate;
import java.util.HashMap;
import java.util.Map;
public class WechatSendTemplate {
public static String appID = "这里写上自己的APPID";
public static String appsecret = "这里写上自己的APPsecret";
/***
* 提醒事件查询
* @param openId
* @param keyValue1 待完成目标的内容
*/
public static void sendTemplate(String openId,String keyValue1){
String token = getAccessToken();
String url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+token;
WxTemplate wxTemplate = new WxTemplate();
wxTemplate.setTemplate_id("这里写上你之前新建的模板id");
wxTemplate.setTouser(openId);
wxTemplate.setUrl("https://cn.bing.com/");
wxTemplate.setTopcolor("#FF0000");
Map<String, TemplateData> map = new HashMap<>();
TemplateData first = new TemplateData();
first.setColor("#868586");
first.setValue("这里填写你要发送的模板信息");
map.put("first", first);
TemplateData keyword1 = new TemplateData();
keyword1.setColor("#173177");
keyword1.setValue(keyValue1);
map.put("keyword1",keyword1);
wxTemplate.setData(map);
JSONObject json = JSONObject.fromObject(wxTemplate);//将java对象转换为json对象
RestTemplate restTemplate = new RestTemplate();
Object forObject = restTemplate.postForObject(url,json,Object.class);
System.out.println(forObject);//打印是否发送成功
}
/**
* 网页授权access_token
*/
public static String getAccessToken() {
RestTemplate restTemplate = new RestTemplate();
String access_token_url = "https://api.weixin.qq.com/cgi-bin/token?appid="+appID+"&secret="+appsecret+"&grant_type=client_credential";
Object forObject = restTemplate.getForObject(access_token_url, Object.class);
JSONObject jsonToken = JSONObject.fromObject(forObject);
String token = null;
if (StringUtils.isNotBlank(String.valueOf(jsonToken))) {
token = jsonToken.getString("access_token");
}
return token;
}
}
可能需要pom.xml上加的依赖
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib-ext-spring</artifactId>
<version>1.0.2</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.11</version>
</dependency>
- 可以用springboot的定时方式来发送