Java 微信发送模板消息

/**
	 * 创建模板消息
	 * @param openId
	 * @param template_id
	 * @param url
	 * @param topcolor
	 * @param carrierName
	 * @param waybillCode
	 * @param waybillDesc
	 * @return
	 */
	public static String makeRouteMessage(String openId,String template_id,String url,String topcolor,String carrierName, String waybillCode, String waybillDesc){
		Template template = new Template();
		template.setTouser(openId);
		template.setTemplate_id(template_id);
		template.setUrl(url);
		template.setTopcolor(topcolor);
		Map<String, TemplateData> data = new HashMap<String, TemplateData>();
		data.put("first", new TemplateData(carrierName+"\n","#ff6600"));
		data.put("waybillNo", new TemplateData(waybillCode+"\n","#ff6600"));
		data.put("remark", new TemplateData(waybillDesc,"#ff6600"));
		template.setData(data);
		JSONObject jsonObject = JSONObject.fromObject(template);
		System.out.println(template);
		return jsonObject+"";
	}
	/**
	 * 发送消息
	 * @param accessToken
	 * @param jsonMsg
	 * @return
	 */
	public static boolean sendTemplateMessage(String accessToken, String jsonMsg){
		logger.info("消息内容:{"+jsonMsg+"}");
		boolean result = false;
		//请求地址
		String requestUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=ACCESS_TOKEN";
		requestUrl = requestUrl.replace("ACCESS_TOKEN", accessToken);
		//发送模板消息
		JSONObject jsonObject = WeixinUtil.httpRequest(requestUrl, "POST", jsonMsg);
		if(null != jsonObject){
			int errorCode = jsonObject.getInt("errcode");
			String errorMsg = jsonObject.getString("errmsg");
			if(0 == errorCode){
				result = true;
				logger.info("模板消息发送成功errorCode:{"+errorCode+"},errmsg:{"+errorMsg+"}");
				System.out.println("模板消息发送成功errorCode:{"+errorCode+"},errmsg:{"+errorMsg+"}");
			}else{
				logger.info("模板消息发送失败errorCode:{"+errorCode+"},errmsg:{"+errorMsg+"}");
				System.out.println("模板消息发送失败errorCode:{"+errorCode+"},errmsg:{"+errorMsg+"}");
			}
		}
		return result;
	}
/**
 * 模版消息结构
 * @author why
 *
 */
public class Template {
	private String touser;//接收人的openId
	private String template_id;//模版id
	private String url;//点击模版访问url
	private String topcolor;//消息头部颜色
	private Map<String,TemplateData> data;//消息内容
	public Template() {
		super();
	}
	public Template(String touser, String template_id, String url,
			String topcolor, Map<String, TemplateData> data) {
		super();
		this.touser = touser;
		this.template_id = template_id;
		this.url = url;
		this.topcolor = topcolor;
		this.data = data;
	}
	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 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;
	}
	
}

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页