我发了个朋友圈,Swagger真他妈的牛B,解放了开发API的码农,麻麻再也不用担心我们写API文档耽误回家吃饭了。
////// 发送钉钉消息 /// /// Employee的userid,多个使用|分隔 /// 标题 /// 发送内容 /// 消息向导地址 /// 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\":\"DingTalk中间件提醒: \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; } /// /// 发送钉钉消息 /// /// Employee的userid,多个使用|分隔 /// 发送内容 /// 发送内容 /// 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; } /// /// 给指定员工发送钉钉消息1 /// /// 指定员工的职员ID /// 标题 /// 发送内容 /// 消息向导地址 /// [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; }
////// 给指定员工发送钉钉消息2 /// /// 部门名称 /// 人员姓名 /// 消息标题 /// 消息内容 /// url /// [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; } /// /// 给指定员工发送钉钉消息(支持图文,文件,图片等格式) /// /// 部门名称 /// 人员姓名 /// 消息内容 eg: "msgtype": "voice","voice": {"media_id": "MEDIA_ID","duration": "10" } /// 格式说明请看钉钉官网文档 https://ding-doc.dingtalk.com/doc#/serverapi2/ye8tup /// /// [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; }