消息模板
登录公众号,选择【模板消息】(没有此菜单可在“添加功能插件”中添加)
点击【模板库】或者【从模板库中添加】进行模板选择
新建自定义模板
如果在模板库中未找到合适的模板,可点击【帮助我们完善模版库】进行模板自定义
添加模板到我的模板库
将选择的模板或者自定义且审核通过的模板添加到我的模板库中,获取模板ID,待发送模板消息使用
发送模板消息
public static void main(String[] args) throws IOException {
String s = PushMessageUtil.pushMessage();
System.out.println(s);
}
package com.litte.util;
import com.litte.entity.templateMessage.TemplateMessage;
import com.litte.entity.templateMessage.WxTemplate;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
/**
* @Description:
* @Author: Mr.jkx
* @date: 2019/6/5
*/
public class PushMessageUtil {
private static final Logger LOGGER = LoggerFactory.getLogger(PushMessageUtil.class);
// 获取ACCESS_TOKEN请求路径
private static final String ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";
// 发送模板消息请求路径
private static final String SEND_TEMPLATE_MESSAGE_URL = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=ACCESS_TOKEN";
// 模板消息ID
private static final String TEMPLATE_ID = "giCW9ftS_f9hvHRWuQKR6-YfTSPEiUZlB84Wrb4acgg";
/**
* @Description:
* @Author: Mr.jkx
* @date: 2019/6/5
*/
public static String pushMessage() throws IOException {
// 请求获取ACCESS_TOKEN,APPSECRET:开发者密码(AppSecret),APPID:开发者ID(AppID)
String url = ACCESS_TOKEN_URL.replace("APPSECRET", "APPSECRET").replace("APPID", "APPID");
JSONObject retStr = WinxinUtil.doGetStr(url);
// 最好将获取到的“access_token”,存于缓存或者数据库(这里只是测试)
String access_token = retStr.get("access_token").toString();
// 请求发送模板消息
String sendUrl = SEND_TEMPLATE_MESSAGE_URL.replace("ACCESS_TOKEN", access_token);
WxTemplate t = new WxTemplate();
t.setTouser("oEvlC5o-gmgQg-BdyzqQBjOiLnIQ"); // 关注此公众号用户的openid
t.setTopcolor("#000000");
t.setTemplate_id(TEMPLATE_ID);
Map<String,TemplateMessage> m = new HashMap<>();
TemplateMessage first = new TemplateMessage();
first.setColor("#000000");
first.setValue("您好,您已成功进行话费充值。");
m.put("first", first);
TemplateMessage accountType = new TemplateMessage();
accountType.setColor("#000000");
accountType.setValue("手机号");
m.put("accountType", accountType);
TemplateMessage account = new TemplateMessage();
account.setColor("#000000");
account.setValue("13912345678");
m.put("account", account);
TemplateMessage amount = new TemplateMessage();
amount.setColor("#000000");
amount.setValue("50元");
m.put("amount", amount);
TemplateMessage result = new TemplateMessage();
result.setColor("#000000");
result.setValue("充值成功");
m.put("result", result);
TemplateMessage remark = new TemplateMessage();
remark.setColor("blue");
remark.setValue("如有疑问,请致电13912345678联系我们。");
m.put("remark", remark);
t.setData(m);
String sendData = JSONObject.fromObject(t).toString();
JSONObject retStr1 = WinxinUtil.doPostStr(sendUrl, sendData);
return retStr1.toString();
}
/**
* 编写Post请求的方法。当我们需要参数传递的时候,可以使用Post请求
*
* @param url 需要请求的URL
* @param outStr 需要传递的参数
* @return 将请求URL后返回的数据,转为JSON格式,并return
*/
public static JSONObject doPostStr(String url,String outStr) throws IOException {
DefaultHttpClient client = new DefaultHttpClient();//获取DefaultHttpClient请求
HttpPost httpost = new HttpPost(url);//HttpPost将使用Get方式发送请求URL
JSONObject jsonObject = null;
httpost.setEntity(new StringEntity(outStr,"UTF-8"));//使用setEntity方法,将我们传进来的参数放入请求中
HttpResponse response = client.execute(httpost);//使用HttpResponse接收client执行httpost的结果
String result = EntityUtils.toString(response.getEntity(),"UTF-8");//HttpEntity转为字符串类型
jsonObject = JSONObject.fromObject(result);//字符串类型转为JSON类型
return jsonObject;
}
}
package com.litte.entity.templateMessage;
import java.util.Map;
/**
* @program: litte
* @description: 模板消息
* @author: Mr.jkx
* @create: 2019-06-05 15:23
*/
public class WxTemplate {
private String template_id;//模板ID
private String touser;//目标客户
private String url;//用户点击模板信息的跳转页面
private String topcolor;//字体颜色
private Map<String,TemplateMessage> 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, TemplateMessage> getData() {
return data;
}
public void setData(Map<String, TemplateMessage> data) {
this.data = data;
}
}
package com.litte.entity.templateMessage;
/**
* @program: litte
* @description: 模板消息
* @author: Mr.jkx
* @create: 2019-06-05 15:30
*/
public class TemplateMessage {
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;
}
}