從新拾取:TFS2017釘釘機器人源代碼簽入通知

如今不少公司辦公都使用釘釘打卡簽到,因而鑑於公司也使用釘釘就打算用釘釘來作一個源代碼簽入通知。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

相關文章
相關標籤/搜索