Skip to content

Instantly share code, notes, and snippets.

@fr34kyn01535
Created July 21, 2019 18:58
Show Gist options
  • Save fr34kyn01535/7ada18c604f398f3668abdbda7550ced to your computer and use it in GitHub Desktop.
Save fr34kyn01535/7ada18c604f398f3668abdbda7550ced to your computer and use it in GitHub Desktop.
Calling a Discord Webhook in C#
void Main()
{
string webhookUrl = "https://discordapp.com/api/webhooks/602563605837250567/XWwqCqUc45sdSd3wfgdfggfjOSJ93fJQ0l-VjawpHh_UbYOzI324234234sShS6Pcx8b";
new DiscordEmbedWebhookRequest() {
Embeds = new List<UserQuery.DiscordEmbedWebhookRequest.DiscordEmbed>(){
new UserQuery.DiscordEmbedWebhookRequest.DiscordEmbed(){
Title = "Fancy"
}
}
}.SendSync(webhookUrl);
}
public abstract class DiscordWebhookRequest {
[JsonProperty("username")]
public string Username { get; set; }
[JsonProperty("avatar_url")]
public string AvatarUrl { get; set; }
[JsonProperty("tts")]
public bool IsTTS { get; set; }
public HttpResponseMessage SendSync(string webhookUrl)
{
webhookUrl += "?wait=true";
using (HttpClient client = new HttpClient())
{
Task<HttpResponseMessage> response = client.PostAsync(webhookUrl, new StringContent(JsonConvert.SerializeObject(this), Encoding.UTF8, "application/json"));
response.Wait();
return response.Result;
}
}
public Task<HttpResponseMessage> SendAsync(string webhookUrl)
{
using (HttpClient client = new HttpClient())
{
return client.PostAsync(webhookUrl, new StringContent(JsonConvert.SerializeObject(this), Encoding.UTF8, "application/json"));
}
}
}
public class DiscordTextWebhookRequest : DiscordWebhookRequest
{
private string content;
[JsonProperty("content")]
public string Content
{
get
{
return content;
}
set
{
if (value.Length > 2000)
{
throw new InvalidDataException("The message can't be longer then 2000 chars.");
}
content = value;
}
}
}
public class DiscordEmbedWebhookRequest : DiscordWebhookRequest
{
public class DiscordEmbed
{
public class DiscordEmbedFooter
{
[JsonProperty("text")]
public string Text { get; set; }
[JsonProperty("icon_url")]
public string IconUrl { get; set; }
[JsonProperty("proxy_icon_url")]
public string ProxyIconUrl { get; set; }
}
public class DiscordEmbedImage
{
[JsonProperty("url")]
public string Url { get; set; }
[JsonProperty("proxy_url")]
public string ProxyUrl { get; set; }
[JsonProperty("height")]
public int Height { get; set; }
[JsonProperty("width")]
public int Width { get; set; }
}
public class DiscordEmbedVideo
{
[JsonProperty("url")]
public string Url { get; set; }
[JsonProperty("height")]
public int Height { get; set; }
[JsonProperty("width")]
public int Width { get; set; }
}
public class DiscordEmbedProvider
{
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("url")]
public string Url { get; set; }
}
public class DiscordEmbedAuthor
{
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("url")]
public string Url { get; set; }
[JsonProperty("icon_url")]
public string IconUrl { get; set; }
[JsonProperty("proxy_icon_url")]
public string ProxyIconUrl { get; set; }
}
public class DiscordEmbedField
{
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("value")]
public string Value { get; set; }
[JsonProperty("inline")]
public bool ShowInline { get; set; }
}
[JsonProperty("title")]
public string Title { get; set; }
[JsonProperty("type")]
public string Type { get; set; } = "rich";
[JsonProperty("description")]
public string Description { get; set; }
[JsonProperty("url")]
public string Url { get; set; }
[JsonProperty("timestamp")]
public string Timestamp
{
get
{
return TimestampDate?.ToString("s", System.Globalization.CultureInfo.InvariantCulture);
}
set
{
TimestampDate = DateTime.ParseExact(value, "s", System.Globalization.CultureInfo.InvariantCulture);
}
}
[JsonIgnoreAttribute]
public DateTime? TimestampDate { get; set; } = null;
[JsonProperty("color")]
public int Color { get; set; }
[JsonProperty("footer")]
public DiscordEmbedFooter Footer { get; set; }
[JsonProperty("image")]
public DiscordEmbedImage Image { get; set; }
[JsonProperty("thumbnail")]
public DiscordEmbedImage Thumbnail { get; set; }
[JsonProperty("video")]
public DiscordEmbedVideo Video { get; set; }
[JsonProperty("provider")]
public DiscordEmbedProvider Provider { get; set; }
[JsonProperty("author")]
public DiscordEmbedAuthor Author { get; set; }
[JsonProperty("fields")]
public List<DiscordEmbedField> Fields { get; set; }
}
[JsonProperty("embeds")]
public List<DiscordEmbed> Embeds { get; set; }
}
public class DiscordFileWebhookRequest : DiscordWebhookRequest
{
[JsonProperty("payload_json")]
public string Payload { get; set; }
[JsonProperty("file")]
public string FileName { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment