1. 程式人生 > >Java 微信傳送模板訊息

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;
}

}