现在很多公司办公都使用钉钉打卡签到,于是鉴于公司也使用钉钉就打算用钉钉来做一个源代码签入通知。
首先先去打开官方网站了解钉钉的通知,钉钉机器人提供了很多模板(GitHub啊,GitLab啊, Coding啊)
但是没有TFS~ 哈哈! 这里我们选择 自定义机器人 《钉钉开放平台机器人文档》
设置机器人名字,就是聊天对话时显示的名称
拿到webhook就可以进行服务挂钩了~。 现在登录你的TFS站点 -> 项目 -> 服务挂钩
选择已签入代码,当然可选的还有很多,可以选择发布部署,CI/CD都可以挂钩
可以提供参数写入标头,以及用户名和密码身份验证。但这里我主要是演示,就只设置URL
点击测试通过可以看到TFS给你发送的JSON这里我只要了部分信息。来显示提交时间、提交版本、作者、提交备注
测试完成后就建立好服务挂钩了~,这时候做个马上做个站点来接受这个信息
我这里是用ASP.NET Core WebAPI 建立项目,自己可以随意。
/// <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")} 提交版本:{jsonObj.resource.changesetId} 作者:{jsonObj.resource.author.displayName + "|" + jsonObj.resource.author.uniqueName} 提交备注:{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