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

时间:2024-03-09 08:49:03
 

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