代码之家  ›  专栏  ›  技术社区  ›  Tree55Topz

Slack Integration-如何在发布到频道时使用@通知用户

  •  0
  • Tree55Topz  · 技术社区  · 7 年前

    我有一段代码,它在构建完成后通知一个空闲通道。在某些情况下,每当构建失败时,我希望向自己添加一个通知,以便能够快速扭转问题。我如何更新此内容以包含对使用@符号的人的通知?

    我在网上看到的一些示例使用SlackMessage构造函数添加用户名,但由于某些原因,我无法重载此构造函数。

    public static void PostToSlack()
            {
                string url= "...";
                string slackUrl = GlobalData.slackUrl;
                string buildName = TestContext.Parameters["BuildName"];
                string buildID = TestContext.Parameters["BuildID"];
                string testName = TestContext.CurrentContext.Test.Name;
                string outcome = TestContext.CurrentContext.Result.Outcome.ToString();
    
                //If tests failed but the suite actually completed, set more understandable outcome message
                if (outcome == "Failed(Child)")
                {
                    outcome = "Completed with Issues";
                }
    
                //Build the text string to post to slack
                string postText = "Build Completed";
    
                SlackMessage message = new SlackMessage
                {
                    text = postText
                };
    
    
                //Convert serializable object to JSON
                string json = JsonConvert.SerializeObject(message, Newtonsoft.Json.Formatting.Indented);
    
                var httpWebRequest = (HttpWebRequest)WebRequest.Create(slackUrl);
                httpWebRequest.ContentType = "application/json";
                httpWebRequest.Method = "POST";
    
                //Send to Slack
                using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
                {
                    streamWriter.Write(json);
                    streamWriter.Flush();
                    streamWriter.Close();
                }
    
                //Error checking if Slack sends back a 404 or 400
                var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                {
                    var result = streamReader.ReadToEnd();
                }
    
            }
    
    0 回复  |  直到 7 年前