调用企业微信接口发送消息

复制代码
 

StringBuilder msg = new StringBuilder();
msg.Append("【客户消费订单消息提醒】"+ OrderType);


switch (OrderType)
{

case "汽车售后申请":
var returnSaleOrder= GetReturnSaleOrder(orderNo);
if (returnSaleOrder!= null)
{
var orgName = GetAreaName((returnSaleOrder.AreaGid));
msg.Append("新增汽车售后申请,");
msg.Append("售后片区:" + AreaName + ",");
msg.Append("售后订单: " + returnSaleOrder.ReturnNo + ",");
msg.Append("发起时间:" + returnSaleOrder.CreateDate);
}
break;
case "汽车购买订单":
var order = GetOrder(orderNo);
if (order != null)
{
var orgName = GetAreaName(DataConvertHelper.GetGuid(order.AreaGid));
msg.Append("汽车销售订单,");
msg.Append("销售片区:" + AreaName + ",");
msg.Append("销售订单号: " + order.OrderNo + ",");
msg.Append("购买交易时间:" + order.PayDate);
}
break;
}


var
sendMsg = msg.ToString(); //向不同配置企业微信人员发送服务通知消息 foreach (var item in IPAddressNotice.Split(",")) { var info = new MessageHelper(); info.Touser = item ; info.QyWxSend(sendMsg); } public MessageHelper() { var config = GetConfig("QyWx"); if (config != null) { this.Identify = config["Identify"]; //商户名称 this.Agentid = config["Agentid"];//企业应用ID this.Touser = config["Touser"]; //商户企业微信号成员 this.ServiveUrl = config["ServiveUrl"]; //商户企业微信服务地址 } } /// <summary> /// 发送企业微信 /// </summary> /// <param name="Message"></param> /// <returns></returns> public string QyWxSend(string Message) { string responseString = string.Empty; var requestData = new StringBuilder(); requestData.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>"); requestData.Append("<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">"); requestData.Append("<soap12:Body>"); requestData.Append("<SendMessage xmlns=\"http://gongsi.com/\">"); requestData.Append("<identify>" + this.Identify + "</identify>"); requestData.Append("<agentid>" + this.Agentid + "</agentid>"); requestData.Append("<touser>" + this.Touser + "</touser>"); requestData.Append("<content>" + Message + "</content>"); requestData.Append("</SendMessage>"); requestData.Append("</soap12:Body>"); requestData.Append("</soap12:Envelope>"); WebClient webClient = new WebClient(); byte[] postData = Encoding.UTF8.GetBytes(requestData.ToString()); string serviveUrl = this.ServiveUrl; webClient.Headers.Add("Content-Type", "application/soap+xml"); webClient.Headers.Add("ContentLength", postData.Length.ToString()); try { byte[] responseData = webClient.UploadData(serviveUrl, "POST", postData); responseString= Encoding.UTF8.GetString(responseData); } catch (Exception ex) { var response = new DataResponse { Exception = new ExceptionModel(ex) }; responseString= JsonHelper.SerializeObject(response); } return responseString } /// <summary> /// 发送企业微信消息 可多人 逗号间隔 /// </summary> /// <param name="Message"></param> /// <returns></returns> public string QyWxSendMore(string Users,string Message) { string responseString = string.Empty; var requestData = new StringBuilder(); requestData.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>"); requestData.Append("<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">"); requestData.Append("<soap12:Body>"); requestData.Append("<SendMessageList xmlns=\"http://gongsi.com/\">"); requestData.Append("<identify>" + this.Identify + "</identify>"); requestData.Append("<agentid>" + this.Agentid + "</agentid>"); requestData.Append("<TouserList>" + Users + "</TouserList>"); requestData.Append("<content>" + Message + "</content>"); requestData.Append("</SendMessageList>"); requestData.Append("</soap12:Body>"); requestData.Append("</soap12:Envelope>"); WebClient webClient = new WebClient(); byte[] postData = Encoding.UTF8.GetBytes(requestData.ToString()); string serviveUrl = this.ServiveUrl; webClient.Headers.Add("Content-Type", "application/soap+xml"); webClient.Headers.Add("ContentLength", postData.Length.ToString()); try { byte[] responseData = webClient.UploadData(serviveUrl, "POST", postData); responseString = Encoding.UTF8.GetString(responseData); } catch (Exception ex) { var response = new DataResponse { Exception = new ExceptionModel(ex) }; responseString =JsonHelper.SerializeObject(response); } return responseString; }
复制代码

 

posted @   蜜雪粮液  阅读(3300)  评论(1编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
点击右上角即可分享
微信分享提示