我有这段代码,可以在构建完成后通知松弛通道。在某些情况下,每当构建失败时,我都想向自己添加一个通知,以便快速解决问题。我该如何更新它以使用@符号向某人发送通知?
我在网上看到的一些示例使用SlackMessage构造函数添加一个UserName,但是由于某些原因,我无法重载此构造函数。
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();
}
}