Reputation: 3
So I'm coding an application in c# which should send messages via Discord Webhooks. I would like it to be able to send files. Is it possible and how do I do it? Right below is my code to send normal messages without any File.
sendWebHook ((webHook), string.Concat(new string[] {"test message", }), "webhook test");
Will be happy if somebody could help me. :)
Upvotes: 0
Views: 24646
Reputation: 609
Actually there is more simpler approach in .net 4.5 and above. The above example uses webhook url to send an text file. You can change it according to your need.
string Webhook_link = "your_webhook_link_here";
string FilePath = @"C:\Users\sample.txt";
using (HttpClient httpClient = new HttpClient())
{
MultipartFormDataContent form = new MultipartFormDataContent();
var file_bytes = System.IO.File.ReadAllBytes(FilePath);
form.Add(new ByteArrayContent(file_bytes, 0, file_bytes.Length), "Document", "file.txt");
httpClient.PostAsync(Webhook_link, form).Wait();
httpClient.Dispose();
}
I have used MultipartFormDataContent to specify file content using bytes, file type, display filename.
(new MultipartFormDataContent()).Add(
ByteArrayContent, //fill it with byte array of your file
File_Type, //Specify file type e.g Photo Document etc
DisplayFilename //This name would be shown as your filename
);
Remember you need to reference System.Net.Http.dll
as well.
Upvotes: 0
Reputation: 21
you can send files, you do it like this!
https://github.com/MorganSkilly/Unity-Discord-Webhook-System/blob/main/Discord.cs
the script is designed for unity but can easily be changed and applied elsewhere :)
private static readonly Encoding encoding = Encoding.UTF8;
public static HttpWebResponse MultipartFormDataPost(string postUrl, string userAgent, Dictionary<string, object> postParameters)
{
string formDataBoundary = String.Format("----------{0:N}", Guid.NewGuid());
string contentType = "multipart/form-data; boundary=" + formDataBoundary;
byte[] formData = GetMultipartFormData(postParameters, formDataBoundary);
return PostForm(postUrl, userAgent, contentType, formData);
}
private static HttpWebResponse PostForm(string postUrl, string userAgent, string contentType, byte[] formData)
{
HttpWebRequest request = WebRequest.Create(postUrl) as HttpWebRequest;
if (request == null)
{
Debug.LogWarning("request is not a http request");
throw new NullReferenceException("request is not a http request");
}
// Set up the request properties.
request.Method = "POST";
request.ContentType = contentType;
request.UserAgent = userAgent;
request.CookieContainer = new CookieContainer();
request.ContentLength = formData.Length;
// Send the form data to the request.
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(formData, 0, formData.Length);
requestStream.Close();
}
return request.GetResponse() as HttpWebResponse;
}
private static byte[] GetMultipartFormData(Dictionary<string, object> postParameters, string boundary)
{
Stream formDataStream = new System.IO.MemoryStream();
bool needsCLRF = false;
foreach (var param in postParameters)
{
if (needsCLRF)
formDataStream.Write(encoding.GetBytes("\r\n"), 0, encoding.GetByteCount("\r\n"));
needsCLRF = true;
if (param.Value is FileParameter)
{
FileParameter fileToUpload = (FileParameter)param.Value;
string header = string.Format("--{0}\r\nContent-Disposition: form-data; name=\"{1}\"; filename=\"{2}\"\r\nContent-Type: {3}\r\n\r\n",
boundary,
param.Key,
fileToUpload.FileName ?? param.Key,
fileToUpload.ContentType ?? "application/octet-stream");
formDataStream.Write(encoding.GetBytes(header), 0, encoding.GetByteCount(header));
formDataStream.Write(fileToUpload.File, 0, fileToUpload.File.Length);
}
else
{
string postData = string.Format("--{0}\r\nContent-Disposition: form-data; name=\"{1}\"\r\n\r\n{2}",
boundary,
param.Key,
param.Value);
formDataStream.Write(encoding.GetBytes(postData), 0, encoding.GetByteCount(postData));
}
}
// Add the end of the request. Start with a newline
string footer = "\r\n--" + boundary + "--\r\n";
formDataStream.Write(encoding.GetBytes(footer), 0, encoding.GetByteCount(footer));
// Dump the Stream into a byte[]
formDataStream.Position = 0;
byte[] formData = new byte[formDataStream.Length];
formDataStream.Read(formData, 0, formData.Length);
formDataStream.Close();
return formData;
}
public class FileParameter
{
public byte[] File { get; set; }
public string FileName { get; set; }
public string ContentType { get; set; }
public FileParameter(byte[] file) : this(file, null) { }
public FileParameter(byte[] file, string filename) : this(file, filename, null) { }
public FileParameter(byte[] file, string filename, string contenttype)
{
File = file;
FileName = filename;
ContentType = contenttype;
}
}
Upvotes: 2
Reputation: 1589
I checked the API and it seems possible. See there.
You need to precise the header: Content-Type: multipart/form-data
I think you need to precise the file directory. (See the file object reference)
Info: I don't know how to code with C# but there is a nodejs package with a sendFile() function. So it's definitely possible.
Upvotes: 1