-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWebHookPoster.cs
56 lines (52 loc) · 1.7 KB
/
WebHookPoster.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
using System.Net.Http.Headers;
namespace PlannerPoc
{
public interface IWebhookPoster
{
Task PostMessageAsync(string message);
}
public class WebhookPoster : IWebhookPoster
{
HttpClient client;
private readonly string webhookUrl;
private const string CardBody = "{" +
"\"type\":\"message\", " +
"\"attachments\":[ " +
"{" +
"\"contentType\":\"application/vnd.microsoft.card.adaptive\"," +
"\"contentUrl\":null," +
"\"content\":{"+
"\"$schema\":\"http://adaptivecards.io/schemas/adaptive-card.json\","+
"\"type\":\"AdaptiveCard\","+
"\"version\":\"1.2\","+
"\"body\":["+
"{"+
"\"type\": \"TextBlock\","+
"\"text\": \"REPLACEME\""+
"}"+
"]"+
"}"+
"}"+
"]"+
"}";
public WebhookPoster(HttpClient client, string webhookUrl)
{
this.client = client;
this.webhookUrl = webhookUrl;
}
public async Task PostMessageAsync(string text)
{
try
{
string body = CardBody.Replace("REPLACEME", text);
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var content = new StringContent(body, System.Text.Encoding.UTF8, "application/json");
var response = await client.PostAsync(webhookUrl, content);
}
catch (Exception ex)
{
throw ex;
}
}
}
}