-
Notifications
You must be signed in to change notification settings - Fork 10
/
Slack.cs
52 lines (50 loc) · 1.78 KB
/
Slack.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
using System;
using System.Collections.Specialized;
using System.IO;
using System.Net;
using System.Text;
using Newtonsoft.Json;
namespace Trapdoor
{
public class Slack
{
private readonly string channel;
private readonly string slackPath;
private readonly string token;
private readonly WebClient client;
public Slack(Config config)
{
channel = config.WebhookChannel;
token = config.WebHookToken;
slackPath = config.SlackPath;
client = new WebClient();
}
public string EditNotification(string payload, string text, string ts)
{
var data = new NameValueCollection();
data["token"] = token;
data["channel"] = channel;
data["as_user"] = "true";
data["text"] = text;
data["ts"] = ts;
data["attachments"] = payload;
var response = client.UploadValues("https://slack.com/api/chat.update", "POST", data);
return JsonConvert.DeserializeObject<dynamic>(Encoding.UTF8.GetString(response))["ts"];
}
public string SendNotification(string payload, string text)
{
var data = new NameValueCollection();
data["token"] = token;
data["channel"] = channel;
data["as_user"] = "true";
data["text"] = text;
data["attachments"] = payload;
var response = client.UploadValues("https://slack.com/api/chat.postMessage", "POST", data);
return JsonConvert.DeserializeObject<dynamic>(Encoding.UTF8.GetString(response))["ts"];
}
public string GenerateSlackLink(string ts)
{
return $"{slackPath}/archives/{channel}/p{ts.Replace(".", "")}";
}
}
}