Skip to content

Instantly share code, notes, and snippets.

@lot224
Created March 6, 2019 18:33
Show Gist options
  • Save lot224/e6e0398c2c62a334168a63f09ffff2bc to your computer and use it in GitHub Desktop.
Save lot224/e6e0398c2c62a334168a63f09ffff2bc to your computer and use it in GitHub Desktop.
An very basic example of a Webhook in C# to Discord.
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
namespace discordWebHook
{
class Program
{
private static HttpClient client;
public static HttpClient Client {
get {
if (client == null)
client = new HttpClient();
return client;
}
}
static void Main(string[] args)
{
//https://discordapp.com/api/webhooks/123456789012345678/ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyzABCDEF
var WebHookId = "123456789012345678";
var WebHookToken = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyzABCDEF";
const string Success = "https://cdn.shopify.com/s/files/1/0185/5092/products/persons-0041_large.png?v=1369543932";
const string Cloud = "https://cdn.shopify.com/s/files/1/0185/5092/products/persons-0189_large.png?v=1369544011";
const string Failure = "https://cdn.shopify.com/s/files/1/0185/5092/products/persons-0035_large.png?v=1369543848";
const string Meh = "https://cdn.shopify.com/s/files/1/0185/5092/products/persons-0052_large.png?v=1369543755";
const string Warning = "https://cdn.shopify.com/s/files/1/0185/5092/products/persons-0016_large.png?v=1369543588";
const string colorBlue = "1F61E6";
const string colorGreen = "80E61F";
const string colorRed = "E7421F";
const string colorPurple = "C61FE6";
const string colorYellow = "E6C71F";
var SuccessWebHook = new
{
username = "Text of the message",
content = "This is the main content section",
avatar_url = "https://cdn.shopify.com/s/files/1/0185/5092/products/persons-0041_large.png?v=1369543932",
embeds = new List<object>
{
new
{
title = "Embed",
url="https://www.google.com/search?q=something",
description="This is the description section of the Embed, the embed has a color bar to the left side",
color= int.Parse(colorGreen, System.Globalization.NumberStyles.HexNumber)
},
new
{
title = "Another Embed",
url="https://www.google.com/search?q=somethingElse",
description="This is the description section of the Embed, the embed has a color bar to the left side",
color= int.Parse(colorPurple, System.Globalization.NumberStyles.HexNumber)
}
}
};
string EndPoint = string.Format("https://discordapp.com/api/webhooks/{0}/{1}", WebHookId, WebHookToken);
var content = new StringContent(JsonConvert.SerializeObject(SuccessWebHook), Encoding.UTF8, "application/json");
Client.PostAsync(EndPoint, content).Wait();
}
}
}
@testingLuciano
Copy link

Thanks a lot, there is a way to attach files with the message ?

@lot224
Copy link
Author

lot224 commented Mar 16, 2023

I haven't played around with this code in years, a quick stack overflow search yielded this https://stackoverflow.com/questions/62494644/how-to-send-file-attachment-with-embed-discord-net-webhook-c-sharp

@testingLuciano
Copy link

testingLuciano commented Mar 29, 2023

I found out this solution, this one fits better to me especially for zipping the folder with the files before send to the channel.

public static void  GenerateFileForDiscord(string logFolderPath, string logFolderName)
{
            var WebHookId = "Insert your WebHook ID here";
            var WebHookToken = "Insert your WebHook Token here";
            MultipartFormDataContent logFolderZipped = new MultipartFormDataContent();
            var logFolderBytes = System.IO.File.ReadAllBytes(logFolderPath + ".zip");
            logFolderZipped.Add(new ByteArrayContent(logFolderBytes, 0, logFolderBytes.Length), logFolderName, logFolderName+".zip");
            string EndPoint = string.Format("https://discordapp.com/api/webhooks/{0}/{1}", WebHookId, WebHookToken);            
            Client.PostAsync(EndPoint, logFolderZipped).Wait();
}

@lot224
Copy link
Author

lot224 commented Apr 17, 2023

That's pretty awesome, thanks for sharing. Hope your project is going well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment