-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiscordWebhook.cs
More file actions
93 lines (84 loc) · 3.49 KB
/
DiscordWebhook.cs
File metadata and controls
93 lines (84 loc) · 3.49 KB
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
using AzureDevOpsDiscord.Models.Discord;
using RestSharp;
namespace AzureDevOpsDiscord
{
public static class DiscordWebhook
{
// the post to Discord will reject the hook unless it includes valid User-Agent
static readonly string USER_AGENT = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36";
// https://discordapp.com/developers/docs/resources/channel#embed-limits
static readonly int MAX_DESCRIPTION_LENGTH = 2048;
static readonly string? DISCORD_ID = Environment.GetEnvironmentVariable("discord_id");
static readonly string? DISCORD_TOKEN = Environment.GetEnvironmentVariable("discord_token");
public static async Task<bool> SendDiscordWebhook(string title, string? url, string description, string? author, int color)
{
if (string.IsNullOrEmpty(DISCORD_ID) || string.IsNullOrEmpty(DISCORD_TOKEN))
{
Console.WriteLine("Discord id or token is not defined in the lambda enviroment variable.");
return false;
}
var client = new RestClient($"https://discordapp.com/api/webhooks/{DISCORD_ID}/{DISCORD_TOKEN}");
var request = new RestRequest();
request.AddHeader("User-Agent", USER_AGENT);
request.AddJsonBody(ConstructBodyString(title, url, description, author, color));
var response = await client.ExecutePostAsync(request);
if (response.IsSuccessful)
{
client.Dispose();
return true;
}
else
{
Console.WriteLine($"{response.StatusCode} {response.Content}");
Console.WriteLine($"{response.ErrorMessage} {response.ErrorException}");
client.Dispose();
return false;
}
}
/**
* {
* "username": "JIRA",
* "avatar_url": "https://wiki.jenkins-ci.org/download/attachments/2916393/headshot.png",
* "embeds": [{
* "title": "This is the Title",
* "url": "https://example.com",
* "description": "This is a description that supports markdown txt",
* "color": 1681177,
* "footer": {
* "text": "This is a footer"
* }
* }]
* }
*/
private static DiscordEmbedMsg ConstructBodyString(string title, string? url, string description, string? author, int color)
{
return new DiscordEmbedMsg()
{
Username = "Azure Devops",
AvatarUrl = "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTkhERSc6r4X73FQTWhB2F_0OYlq7qZMfZcIzarA08oZNbB3ZYjEBfCOo3DhvLlOv_02ww&usqp=CAU",
Embeds = new List<Embed>()
{
new Embed()
{
Title = title,
Url = url,
Description = Truncate(description, MAX_DESCRIPTION_LENGTH),
Color = color,
Footer = new EmbedFooter()
{
Text = author
}
}
}
};
}
private static string? Truncate(string? value, int maxLength)
{
if (!string.IsNullOrEmpty(value) && value.Length > maxLength)
{
return value.Substring(0, maxLength);
}
return value;
}
}
}