1.设置所属行业
在进行模板消息的发送之前,我们需要为我们的公众号设置所属行业。
我们需要往如下url:
https://api.weixin.qq.com/cgi-bin/template/api_set_industry?access_token=ACCESS_TOKEN
发送post请求。
这里直接用test方法设置所属行业:
@Test
void set(){//测试 设置用于发送模板消息的行业
String at = WeChatService.getAccessToken();
String url = "https://api.weixin.qq.com/cgi-bin/template/api_set_industry?access_token="+at;
String data = "{\n" +
" \"industry_id1\":\"1\",\n" +
" \"industry_id2\":\"4\"\n" +
"}";
String result = CheckUtil.post(url,data);
System.out.println(result);
}
设置好行业信息后,需要查看行业信息:
@Test
void get(){//返回设置的行业信息
String at = WeChatService.getAccessToken();
String url = "https://api.weixin.qq.com/cgi-bin/template/get_industry?access_token="+at;
String result = CheckUtil.get(url);
System.out.println(result);
}
返回如下行业信息:
{"primary_industry":{"first_class":"IT科技","second_class":"互联网|电子商务"},"secondary_industry":{"first_class":"IT科技","second_class":"电子技术"}}
2.发送模板消息
在微信官方文档的如下位置下载模板消息的示例:
这里我们用的是比赛模板消息。
提交后自动生成模板ID,这个模板ID后面会用到。
发送模板消息,也是需要以post的方式向微信的服务器发送JSON字符串。
创建 Template 类和 KeyWord 类来封装发送模板消息的JSON数据:
public class Template {
private String touser;
private String template_id;
private String url;
private KeyWord data;
public Template() {
}
public Template(String touser, String template_id, String url) {
this.touser = touser;
this.template_id = template_id;
this.url = url;
}
public String getTouser() {
return touser;
}
public void setTouser(String touser) {
this.touser = touser;
}
public String getTemplate_id() {
return template_id;
}
public void setTemplate_id(String template_id) {
this.template_id = template_id;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public KeyWord getData() {
return data;
}
public void setData(KeyWord data) {
this.data = data;
}
}
public class KeyWord {
private Map<String,String> first = new HashMap<>();
private Map<String,String> keyword1 = new HashMap<>();
private Map<String,String> keyword2 = new HashMap<>();
private Map<String,String> keyword3 = new HashMap<>();
private Map<String,String> keyword4 = new HashMap<>();
public Map<String, String> getFirst() {
return first;
}
public void setFirst(Map<String, String> first) {
this.first = first;
}
public Map<String, String> getKeyword1() {
return keyword1;
}
public void setKeyword1(Map<String, String> keyword1) {
this.keyword1 = keyword1;
}
public Map<String, String> getKeyword2() {
return keyword2;
}
public void setKeyword2(Map<String, String> keyword2) {
this.keyword2 = keyword2;
}
public Map<String, String> getKeyword3() {
return keyword3;
}
public void setKeyword3(Map<String, String> keyword3) {
this.keyword3 = keyword3;
}
public Map<String, String> getKeyword4() {
return keyword4;
}
public void setKeyword4(Map<String, String> keyword4) {
this.keyword4 = keyword4;
}
}
这里我们用test方法测试发送模板消息:
下述代码中的MYOPENID和TEMPLATEID分别是要发送给的用户的openid和要发送模板的模板ID。
@Test
void testTemplate(){//测试用于发送模板消息
Template template = new Template(MYOPENID,TEMPLATEID,"https://www.baidu.com/");
KeyWord keyWord = new KeyWord();
keyWord.getFirst().put("value","你已经成功报名a队 vs b队的比赛!");
keyWord.getFirst().put("color","#173177");
keyWord.getKeyword1().put("value","参赛队伍:a队 vs b队");
keyWord.getKeyword2().put("value","比赛时间:2月28日16:00-18:00");
keyWord.getKeyword3().put("value","比赛地点:c区15号");
keyWord.getKeyword4().put("value","已报名人数:a队8人,b队6人\n" + "请点击查看比赛详情");
template.setData(keyWord);
JSONObject jsonObject = JSONObject.fromObject(template);
System.out.println(jsonObject);
String at = WeChatService.getAccessToken();
String url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+at;
String result = CheckUtil.post(url,jsonObject.toString());
System.out.println(result);
}
发送的模板消息如下: