This repository has been archived by the owner on Jul 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDiscordWebhook.cs
67 lines (61 loc) · 2.8 KB
/
DiscordWebhook.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
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
namespace MsTeamsBot
{
class DiscordWebhook
{
private static readonly HttpClient _client = new HttpClient();
private string _webhookURL;
private string _username;
private string _avatarUrl;
public DiscordWebhook(string url) { this._webhookURL = url; }
public DiscordWebhook(string url, string username) { this._webhookURL = url; this._username = username; }
public DiscordWebhook(string url, string username, string avatarURL) { this._webhookURL = url; this._username = username; this._avatarUrl = avatarURL; }
public async Task<HttpResponseMessage> SendMessageAsync(string username = null, string avatarUrl = null, string content = null, bool isTTS = false, Embed embeds = null)
{
var msg = new Message(username ?? this._username, avatarUrl ?? this._avatarUrl, content, isTTS, embeds);
return await _client.PostAsync(this._webhookURL, new StringContent(JsonConvert.SerializeObject(msg), Encoding.UTF8, "application/json"));
}
public async Task<HttpResponseMessage> SendMessageAsync(string username = null, string avatarUrl = null, string content = null, bool isTTS = false, IEnumerable<Embed> embeds = null)
{
var msg = new Message(username ?? this._username, avatarUrl ?? this._avatarUrl, content, isTTS, embeds);
return await _client.PostAsync(this._webhookURL, new StringContent(JsonConvert.SerializeObject(msg), Encoding.UTF8, "application/json"));
}
[JsonObject]
internal class Message
{
[JsonProperty("username")]
public string Username;
[JsonProperty("avatar_url")]
public string AvatarUrl;
[JsonProperty("content")]
public string Content;
[JsonProperty("tts")]
public bool isTTS;
[JsonProperty("embeds")]
public List<Embed> Embeds;
public Message(string username, string avatarUrl, string content, bool isTTS, IEnumerable<Embed> embeds)
{
this.Username = username;
this.AvatarUrl = avatarUrl;
this.Content = content;
this.isTTS = isTTS;
Embeds = new List<Embed>(embeds);
}
public Message(string username, string avatarUrl, string content, bool isTTS, Embed embed)
{
this.Username = username;
this.AvatarUrl = avatarUrl;
this.Content = content;
this.isTTS = isTTS;
Embeds = new List<Embed>();
Embeds.Add(embed);
}
}
}
}