forked from amoeba/TownCrier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWebhook.cs
154 lines (133 loc) · 4.47 KB
/
Webhook.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Threading;
namespace TownCrier
{
class Webhook
{
public string Name;
public string Method;
public string URLFormatString; // Contains an @ somewhere
public string PayloadFormatString; // Or contains an @ somewhere
public Webhook(string name, string url, string method, string payload)
{
Name = name;
URLFormatString = url;
Method = method;
PayloadFormatString = payload;
}
public override string ToString()
{
try
{
StringBuilder sb = new StringBuilder();
sb.Append("Webhook: Name: '");
sb.Append(Name);
sb.Append("', URL: '");
sb.Append(URLFormatString.ToString());
sb.Append(" (");
sb.Append(Method);
sb.Append(") with payload '");
sb.Append(PayloadFormatString);
sb.Append("'.");
return sb.ToString();
}
catch (Exception ex)
{
Util.LogError(ex);
return "Failed to print Webhook";
}
}
public string ToSetting()
{
try
{
StringBuilder sb = new StringBuilder();
sb.Append("webhook\t");
sb.Append(Name);
sb.Append("\t");
sb.Append(URLFormatString);
sb.Append("\t");
sb.Append(Method);
sb.Append("\t");
sb.Append(PayloadFormatString);
return sb.ToString();
}
catch (Exception ex)
{
Util.LogError(ex);
return "";
}
}
public Uri URI(WebhookMessage message)
{
try
{
return new Uri(URLFormatString.Replace("@", message.ToQueryStringValue()));
}
catch (Exception ex)
{
Util.LogError(ex);
return new Uri(URLFormatString);
}
}
private string Payload(WebhookMessage message)
{
return PayloadFormatString.Replace("@", message.ToJSONStringValue());
}
public void Send(WebhookMessage message)
{
try
{
Thread t = new Thread(() =>
{
try
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(URI(message));
req.Method = Method;
if (Method == "POST" && PayloadFormatString != "")
{
req.ContentType = "application/json";
using (var streamWriter = new StreamWriter(req.GetRequestStream()))
{
streamWriter.Write(Payload(message));
streamWriter.Flush();
streamWriter.Close();
}
}
var httpResponse = (HttpWebResponse)req.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var responseText = streamReader.ReadToEnd();
}
}
catch (WebException wex)
{
if (wex.Response != null)
{
using (var errorResponse = (HttpWebResponse)wex.Response)
{
using (var reader = new StreamReader(errorResponse.GetResponseStream()))
{
string error = reader.ReadToEnd();
Util.LogError(new Exception(error));
}
}
}
}
catch (Exception ex)
{
Util.LogError(ex);
}
});
t.Start();
}
catch (Exception ex)
{
Util.LogError(ex);
}
}
}
}