我发了个朋友圈,Swagger真他妈的牛B,解放了开发API的码农,麻麻再也不用担心我们写API文档耽误回家吃饭了。
/// <summary> /// 发送钉钉消息 /// </summary> /// <param name="toUser">Employee的userid,多个使用|分隔</param> /// <param name="title">标题</param> /// <param name="content">发送内容</param> /// <param name="sourceUrl">消息向导地址</param> /// <returns></returns> private ApiResult SendCardMessageToEmployee(string toUser, string title, string content, string sourceUrl) { var result = new ApiResult { Success = false, ResponseDatetime = DateTime.Now.ToString(), ResultMessage = "操作失败!", ResultObject = null }; try { toUser = toUser.Trim(‘|‘); string JSONData = "{\"touser\":\"" + toUser + "\",\"toparty\":\"\",\"agentid\":\"" + _settings.Value.sendmessage_agentid + "\",\"msgtype\":\"action_card\",\"action_card\":{\"title\":\"" + title + "\",\"markdown\":\"<font size=7 face=\‘黑体\‘>DingTalk中间件提醒: </font> \n \n " + content + "\r \n " + "\r \n " + "\r \n " + DateTime.Now.ToString() + "\",\"single_title\": \"查看详情\",\"single_url\": \"" + sourceUrl + "\"}}"; var access_token = GetAccessToke(); var tokenObj = access_token.ResultObject as AccessToken; string url = "https://oapi.dingtalk.com/message/send?access_token=" + tokenObj.access_token; string response = GetResponseData(JSONData, url); result.ResultObject = response; result.Success = string.IsNullOrEmpty(response); result.ResultMessage = "API调用成功!Requst Url:[" + url + "],Content:[" + JSONData + "]"; } catch (Exception ex) { result.ResultObject = ex; _logger.LogError(ex, ex.Message, new object[] { }); } return result; } /// <summary> /// 发送钉钉消息 /// </summary> /// <param name="toUser">Employee的userid,多个使用|分隔</param> /// <param name="content">发送内容</param> /// <param name="sourceUrl">发送内容</param> /// <returns></returns> private ApiResult SendMediaMessageToEmployee(string toUser, string content, string sourceUrl) { var result = new ApiResult { Success = false, ResponseDatetime = DateTime.Now.ToString(), ResultMessage = "操作失败!", ResultObject = null }; try { toUser = toUser.Trim(‘|‘); string JSONData = "{\"touser\":\"" + toUser + "\",\"toparty\":\"\",\"agentid\":\"" + _settings.Value.sendmessage_agentid + "\",\"" + content + ",\"single_title\": \"查看详情\",\"single_url\": \"" + sourceUrl + "\"}}"; var access_token = GetAccessToke(); var tokenObj = access_token.ResultObject as AccessToken; string url = "https://oapi.dingtalk.com/message/send?access_token=" + tokenObj.access_token; string response = GetResponseData(JSONData, url); result.ResultObject = response; result.Success = string.IsNullOrEmpty(response); result.ResultMessage = "API调用成功!Requst Url:[" + url + "],Content:[" + JSONData + "]"; } catch (Exception ex) { result.ResultObject = ex; _logger.LogError(ex, ex.Message, new object[] { }); } return result; } /// <summary> /// 给指定员工发送钉钉消息1 /// </summary> /// <param name="userid">指定员工的职员ID</param> /// <param name="title">标题</param> /// <param name="content">发送内容</param> /// <param name="sourceUrl">消息向导地址</param> /// <returns></returns> [HttpGet] [HttpPost] public ApiResult SendDingTalkMessage(string userid, string title, string content, string sourceUrl = "http://www.cquni.com") { if (string.IsNullOrEmpty(title)) title = "通知"; var result = new ApiResult { Success = false, ResponseDatetime = DateTime.Now.ToString(), ResultMessage = "操作失败!", ResultObject = null }; try { var rs = SendCardMessageToEmployee(userid, title, content, sourceUrl); result.Success = rs.Success; result.ResultMessage = rs.ResultMessage; result.ResultObject = rs.ResultObject; } catch (Exception ex) { result.ResultObject = ex; } return result; }
/// <summary> /// 给指定员工发送钉钉消息2 /// </summary> /// <param name="departmentName">部门名称</param> /// <param name="employeeName">人员姓名</param> /// <param name="title">消息标题</param> /// <param name="content">消息内容</param> /// <param name="sourceUrl">url</param> /// <returns></returns> [HttpGet] [HttpPost] public ApiResult SendDingTalkMessageExt(string departmentName, string employeeName, string title, string content, string sourceUrl = "http://www.cquni.com") { var result = new ApiResult { Success = false, ResponseDatetime = DateTime.Now.ToString(), ResultMessage = "操作失败!", ResultObject = null }; if (string.IsNullOrEmpty(title)) title = "通知"; var deptment = GetDepartmentInfo().department; if (deptment != null && deptment.Count > 0) { var currDept = deptment.Where(d => d.name.Contains(departmentName)).FirstOrDefault(); if (currDept == null) { result.ResultMessage = "获取人员的部门信息出错,输入部门名称:" + departmentName; } else { var employeeList = GetDepartmentEmployeeList(currDept).userlist; if (employeeList != null && employeeList.Count > 0) { var currentEmp = employeeList.Where(e => e.name.Contains(employeeName)).FirstOrDefault(); try { var rs = SendCardMessageToEmployee(currentEmp.userid, title, content, sourceUrl); return rs; } catch (Exception ex) { _logger.LogError(ex.Message, new object[] { ex, ex.InnerException }); } } else { result.ResultMessage = string.Format("很奇怪,这个部门‘{0}‘一个人也没有!", departmentName); } } } else { result.ResultMessage = "获取公司部门信息出错!"; } return result; } /// <summary> /// 给指定员工发送钉钉消息(支持图文,文件,图片等格式) /// </summary> /// <param name="departmentName">部门名称</param> /// <param name="employeeName">人员姓名</param> /// <param name="mediaContent">消息内容 eg: "msgtype": "voice","voice": {"media_id": "MEDIA_ID","duration": "10" } /// 格式说明请看钉钉官网文档 https://ding-doc.dingtalk.com/doc#/serverapi2/ye8tup </param> /// <param name="sourceUrl"></param> /// <returns></returns> [HttpGet] [HttpPost] public ApiResult SendDingTalkMediaMessage(string departmentName, string employeeName, string mediaContent, string sourceUrl = "http://www.cquni.com") { var result = new ApiResult { Success = false, ResponseDatetime = DateTime.Now.ToString(), ResultMessage = "操作失败!", ResultObject = null }; var deptment = GetDepartmentInfo().department; if (deptment != null && deptment.Count > 0) { var currDept = deptment.Where(d => d.name.Contains(departmentName)).FirstOrDefault(); if (currDept == null) { result.ResultMessage = "获取人员的部门信息出错,输入部门名称:" + departmentName; } else { var employeeList = GetDepartmentEmployeeList(currDept).userlist; if (employeeList != null && employeeList.Count > 0) { var currentEmp = employeeList.Where(e => e.name.Contains(employeeName)).FirstOrDefault(); try { var rs = SendMediaMessageToEmployee(currentEmp.userid, mediaContent, sourceUrl); return rs; } catch (Exception ex) { _logger.LogError(ex.Message, new object[] { ex, ex.InnerException }); } } else { result.ResultMessage = string.Format("很奇怪,这个部门‘{0}‘一个人也没有!", departmentName); } } } else { result.ResultMessage = "获取公司部门信息出错!"; } return result; }