如今不少公司办公都使用钉钉打卡签到,因而鉴于公司也使用钉钉就打算用钉钉来作一个源代码签入通知。web
首先先去打开官方网站了解钉钉的通知,钉钉机器人提供了不少模板(GitHub啊,GitLab啊, Coding啊)json
可是没有TFS~ 哈哈! 这里咱们选择 自定义机器人 《钉钉开放平台机器人文档》api
设置机器人名字,就是聊天对话时显示的名称async
拿到webhook就能够进行服务挂钩了~。 如今登陆你的TFS站点 -> 项目 -> 服务挂钩测试
选择已签入代码,固然可选的还有不少,能够选择发布部署,CI/CD均可以挂钩网站
能够提供参数写入标头,以及用户名和密码身份验证。但这里我主要是演示,就只设置URLurl
点击测试经过能够看到TFS给你发送的JSON这里我只要了部分信息。来显示提交时间、提交版本、做者、提交备注spa
测试完成后就创建好服务挂钩了~,这时候作个立刻作个站点来接受这个信息.net
我这里是用ASP.NET Core WebAPI 创建项目,本身能够随意。3d
/// <summary> /// 钉钉处理器 /// </summary> [Route("api/[controller]")] public class DingTalkController : Controller { /// <summary> /// 钉钉机器人TFS签入挂钩 /// </summary> /// <remarks> /// 经过TFS服务挂钩返回JSON来实现转发钉钉机器人 /// </remarks> /// <returns>Task</returns> [IgnoreGlobalResult] [HttpPost("PushCommitMessage")] public async Task<IActionResult> PostAsync() { var result = string.Empty; using (var reader = new StreamReader(Request.Body, Encoding.UTF8)) { result = await reader.ReadToEndAsync(); WorkItem jsonObj = JsonConvert.DeserializeObject<WorkItem>(result); if (jsonObj != null) { var content = $"提交时间:{jsonObj.resource.createdDate.ToString("yyyy-MM-dd HH:mm:ss")}\n提交版本:{jsonObj.resource.changesetId}\n做者:{jsonObj.resource.author.displayName + "|" + jsonObj.resource.author.uniqueName}\n提交备注:{jsonObj.resource.comment}"; var url = "https://oapi.dingtalk.com/robot/send?access_token=fc10329e2d326d2eaf81a8317asasdasffdgdffghfghdadsfsdfadsfdsfga5dac3314e98fa88d"; //序列化JSON TextTypeMsg objMsg = new TextTypeMsg(); objMsg.msgtype = "text"; objMsg.text = new TextTypeMsg.Text(); objMsg.text.content = content; var json = JsonConvertHelper.ToJson(objMsg); var request = new HttpRequest(HttpMethod.Post, url); request.ContentType(HttpContentType.Json.Description()); request.SetJson(json); DingTalkResult dingTalkResult = JsonConvertHelper.ToObject<DingTalkResult>(request.ResultAsync().Result); OperationResult operationResult = new OperationResult(); if (dingTalkResult.errmsg == "ok") { operationResult.Code = ErrorCodeEnum.Success.ToString(); operationResult.Message = dingTalkResult.errmsg; operationResult.Data = ""; } else { operationResult.Code = ErrorCodeEnum.ThirdPartyError.ToString(); operationResult.Message = dingTalkResult.errmsg; operationResult.Data = ""; } return Json(operationResult); } else { OperationResult operationResult = new OperationResult(); operationResult.Code = ErrorCodeEnum.SerializedError.ToString(); operationResult.Message = ErrorCodeEnum.SerializedError.Description(); operationResult.Data = ""; return Json(operationResult); } } } } public class DingTalkResult { public string errmsg { get; set; } public string errcode { get; set; } } public class WorkItem { public WorkItemResource resource { get; set; } } public class WorkItemResource { public int changesetId { get; set; } public Author author { get; set; } public DateTime createdDate { get; set; } public string comment { get; set; } } public class Author { public string displayName { get; set; } public string uniqueName { get; set; } } public class TextTypeMsg { public string msgtype { get; set; } public Text text { get; set; } public At at { get; set; } public class Text { public string content { get; set; } } public class At { public List<string> atMobiles { get; set; } public bool isAtAll { get; set; } } }
最终效果
参考文档
https://open-doc.dingtalk.com/docs/doc.htm?spm=a219a.7629140.0.0.evfrZF&treeId=257&articleId=105735&docType=1
https://blog.csdn.net/xxdddail/article/details/73249468