【问题标题】:How do I send files in discord webhook?如何在 discord webhook 中发送文件?
【发布时间】:2020-07-10 21:15:00
【问题描述】:

所以我正在用 c# 编写一个应用程序,它应该通过 Discord Webhooks 发送消息。我希望它能够发送文件。有可能吗?我该怎么做?下面是我在没有任何文件的情况下发送普通消息的代码。

sendWebHook ((webHook), string.Concat(new string[] {"test message", }), "webhook test");

如果有人可以帮助我,我会很高兴。 :)

【问题讨论】:

  • 无法通过 Discord 的 webhook 发送文件。您只能使用它发送有效负载。图像可以在 Discord 上显示,但这是通过图像 URL 完成的。您不是在发送图像本身。
  • 好的。然后我会尝试让一个机器人制作一个下载链接然后

标签: c# discord


【解决方案1】:

其实.net 4.5 及以上版本有更简单的方法。上面的示例使用 webhook url 发送文本文件。您可以根据需要进行更改。

    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();
    }

我使用MultipartFormDataContent 来指定使用字节、文件类型、显示文件名的文件内容。

(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
);

请记住,您还需要引用 System.Net.Http.dll

【讨论】:

    【解决方案2】:

    你可以发送文件,你这样做!

    https://github.com/MorganSkilly/Unity-Discord-Webhook-System/blob/main/Discord.cs

    脚本是为统一而设计的,但可以轻松更改并应用于其他地方:)

    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;
            }
        }
    

    【讨论】:

      【解决方案3】:

      我检查了 API,似乎可行。见there

      您需要精确标题:Content-Type: multipart/form-data

      我认为你需要精确的文件目录。 (参见文件对象参考)

      信息: 我不知道如何使用 C# 进行编码,但有一个带有 sendFile() 函数的 nodejs 包。所以这绝对是可能的。

      【讨论】:

        猜你喜欢
        • 2020-11-26
        • 2020-09-25
        • 2018-09-11
        • 2020-03-20
        • 2019-01-15
        • 2020-04-11
        • 2019-03-05
        • 2019-10-21
        • 2021-10-21
        相关资源
        最近更新 更多