/// <summary> /// 发送模板消息 /// </summary> /// <param name="type"></param> /// <param name="touser"></param> /// <param name="contentList"></param> /// <returns></returns> public static TemPlateMessageResponse SendTemPlateMessage(string template_file_path, string touser, List <string> contentList, string url) { var ret = new TemPlateMessageResponse(); var template_id = string.Empty; var msgitemlist = new List <TemPlateMessageItem>(); var doc = new XmlDocument(); try { string application_path = WebUtil.getApplicationPath().ToLower(); if (!string.IsNullOrEmpty(application_path)) { if (!template_file_path.ToLower().StartsWith(application_path)) { template_file_path = application_path + template_file_path; } } string filename = HttpContext.Current.Server.MapPath(template_file_path); doc.Load(filename); //加载Xml文件 var rootElem = doc.DocumentElement; //获取根节点 if (rootElem != null) { template_id = rootElem.GetElementsByTagName("template_id")[0].InnerText; var parmNodes = rootElem.GetElementsByTagName("parm"); //获取parm子节点集合 foreach (XmlNode node in parmNodes) { var parmname = ((XmlElement)node).GetAttribute("name"); //获取parmname属性值 var colorval = ((XmlElement)node).GetAttribute("color"); //获取colorval属性值 msgitemlist.Add(new TemPlateMessageItem { color = colorval, key = parmname }); } for (var i = 0; i < msgitemlist.Count; i++) { msgitemlist[i].value = contentList[i]; } ret = MessageHandler.SendTemPlateMessage(GetAccessToken(null), touser, template_id, msgitemlist, url); } } catch (Exception ex) { LogHelper.WriteError("TemPlateMessageResponse", template_file_path, ex); } return(ret); }
/// <summary> /// 获取模板列表 /// </summary> /// <param name="accessToken"></param> /// <returns></returns> public static GetTemplateInfo GetTemplateInfo(string accessToken) { IMpClient mpClient = new MpClient(); Request.TemPlateMessage request = new Request.TemPlateMessage() { Method = "GET", AccessToken = accessToken, ReqUrl = "https://api.weixin.qq.com/cgi-bin/template/get_all_private_template?access_token={0}", }; TemPlateMessageResponse response = mpClient.Execute(request); if (response.IsError) { Utility.LogHelper.WriteDebug("获取模板列表响应", response.Body); } return(response.TemplateInfo); }
/// <summary> /// 发送模板消息 /// </summary> /// <param name="accessToken"></param> /// <param name="msg"></param> /// <returns></returns> public static TemPlateMessageResponse SendTemPlateMessageOperate(string accessToken, SendTemPlateMessage msg) { IMpClient mpClient = new MpClient(); Request.TemPlateMessage request = new Request.TemPlateMessage() { Method = "POST", AccessToken = accessToken, ReqUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={0}", SendData = msg.ToJsonString() }; //LogHelper.WriteInfo("template_msg: ", msg.ToJsonString()); TemPlateMessageResponse response = mpClient.Execute(request); if (response.IsError) { LogHelper.WriteDebug("发送模板消息响应", response.Body); } return(response); }
/// <summary> /// 删除模板 /// </summary> /// <param name="accessToken"></param> /// <param name="msg"></param> /// <returns></returns> public static TemPlateMessageResponse DeleteTemPlate(string accessToken, DeleteTemPlate msg) { IMpClient mpClient = new MpClient(); Request.TemPlateMessage request = new Request.TemPlateMessage() { Method = "POST", AccessToken = accessToken, ReqUrl = "https://api.weixin.qq.com/cgi-bin/template/del_private_template?access_token={0}", SendData = msg.ToJsonString() }; TemPlateMessageResponse response = mpClient.Execute(request); if (response.IsError) { Utility.LogHelper.WriteDebug("删除模板", request.SendData); Utility.LogHelper.WriteDebug("删除模板响应", response.Body); } return(response); }
/// <summary> /// 设置所属行业 /// </summary> /// <param name="accessToken"></param> /// <param name="msg"></param> /// <returns></returns> public static TemPlateMessageResponse SetIndustry(string accessToken, SetIndustry msg) { IMpClient mpClient = new MpClient(); Request.TemPlateMessage request = new Request.TemPlateMessage() { Method = "POST", AccessToken = accessToken, ReqUrl = "https://api.weixin.qq.com/cgi-bin/template/api_set_industry?access_token={0}", SendData = msg.ToJsonString() }; TemPlateMessageResponse response = mpClient.Execute(request); if (response.IsError) { Utility.LogHelper.WriteDebug("模板消息设置所属行业", request.SendData); Utility.LogHelper.WriteDebug("模板消息设置所属行业响应", response.Body); } return(response); }