C# 将文件附加在slack上的邮件发布

C# 将文件附加在slack上的邮件发布,c#,slack-api,C#,Slack Api,我正在用C#编写一个简单的控制台应用程序,以便与Slack.com通信。 我是通过他们的WebApi来做这件事的。 目前我知道如何发布消息(带有附件、彩色、链接、用户等)并将文件发送到服务器 如果您以正常方式发送文件(“输入文本框”左侧的“上载文件”),此文件将显示在主对话窗口中。但是如何通过WebAPI实现同样的功能呢?当前,在发送文件后,我可以在右侧面板中看到它,其中仅列出了所有文件 还有第二个问题:是否可以更改消息中文本的颜色(附件中的“行”没有) 这是通过发送文件后的响应 编辑1: 在这

我正在用C#编写一个简单的控制台应用程序,以便与Slack.com通信。 我是通过他们的WebApi来做这件事的。 目前我知道如何发布消息(带有附件、彩色、链接、用户等)并将文件发送到服务器

如果您以正常方式发送文件(“输入文本框”左侧的“上载文件”),此文件将显示在主对话窗口中。但是如何通过WebAPI实现同样的功能呢?当前,在发送文件后,我可以在右侧面板中看到它,其中仅列出了所有文件

还有第二个问题:是否可以更改消息中文本的颜色(附件中的“行”没有)

这是通过发送文件后的响应

编辑1: 在这方面:

 https://slack.com/api/files.upload?token=*********&content=System.Byte[]&channels=[%23*****]
网址是:

string url = "https://slack.com/api/files.upload?token=" + token + "&content=" + nfile + "&channels=[" + Channel + "]&pretty=1"
然后我传递字节数组

编辑:


我已经解决了这个问题。问题是频道应该是频道的ID,而不是频道的名称。。。愚蠢的错误:(

如果在通道属性后添加pretty=1,它会工作得更好

例如:

public void UploadFile(string token, string filePath, string channel)
{
    var client = new RestClient("https://slack.com");

    var request = new RestRequest("api/files.upload", Method.POST);
    request.AddParameter("token", token);
    request.AddParameter("channels", channel);

    var fileInfo = new FileInfo(filePath);
    request.AddFile("file", File.ReadAllBytes(filePath), fileInfo.Name, contentType:"multipart/form-data");

    //Execute the request
    var response = client.Execute(request);
    var content = response.Content;
}

嗨,这里有一个关于RestSharp的干净例子

    public static void SlackSendFile(string token, string channelId, string filepath)
    {
        FileStream str = File.OpenRead(filepath);
        byte[] fBytes = new byte[str.Length];
        str.Read(fBytes, 0, fBytes.Length);
        str.Close();

        var webClient = new WebClient();
        string boundary = "------------------------" + DateTime.Now.Ticks.ToString("x");
        webClient.Headers.Add("Content-Type", "multipart/form-data; boundary=" + boundary);
        var fileData = webClient.Encoding.GetString(fBytes);
        var package = string.Format("--{0}\r\nContent-Disposition: form-data; name=\"file\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n{3}\r\n--{0}--\r\n", boundary, "Testing.txt", "multipart/form-data", fileData);

        var nfile = webClient.Encoding.GetBytes(package);
        var encodedfile = System.Text.Encoding.ASCII.GetString(nfile);
        string url = "https://slack.com/api/files.upload?token=" + token + "&content=" + encodedfile + "&channels=" + channelId + "";

        byte[] resp = webClient.UploadData(url, "POST", nfile);

        var k = System.Text.Encoding.Default.GetString(resp);
        Console.WriteLine(k);
    }

我不得不稍微修改一下你的代码,让它为仍在寻找它的人工作。我需要将文件数组转换为ASCII编码的字符串,然后将细节转换为参数


尝试阅读以下内容:是的,这就是我发送文件的方式。但这不会在主对话窗口中显示文件。请尝试调试应用程序以查看返回的http响应,同时确保应用程序的信任级别设置为“完全”。您是否也可以向我们显示您发布的JSON?问题已解决->请参阅帖子,最后一篇“编辑”
string url = "https://slack.com/api/files.upload?token=" + token + "&content=" + nfile + "&channels=[" + Channel + "]&pretty=1"
public void UploadFile(string token, string filePath, string channel)
{
    var client = new RestClient("https://slack.com");

    var request = new RestRequest("api/files.upload", Method.POST);
    request.AddParameter("token", token);
    request.AddParameter("channels", channel);

    var fileInfo = new FileInfo(filePath);
    request.AddFile("file", File.ReadAllBytes(filePath), fileInfo.Name, contentType:"multipart/form-data");

    //Execute the request
    var response = client.Execute(request);
    var content = response.Content;
}
    public static void SlackSendFile(string token, string channelId, string filepath)
    {
        FileStream str = File.OpenRead(filepath);
        byte[] fBytes = new byte[str.Length];
        str.Read(fBytes, 0, fBytes.Length);
        str.Close();

        var webClient = new WebClient();
        string boundary = "------------------------" + DateTime.Now.Ticks.ToString("x");
        webClient.Headers.Add("Content-Type", "multipart/form-data; boundary=" + boundary);
        var fileData = webClient.Encoding.GetString(fBytes);
        var package = string.Format("--{0}\r\nContent-Disposition: form-data; name=\"file\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n{3}\r\n--{0}--\r\n", boundary, "Testing.txt", "multipart/form-data", fileData);

        var nfile = webClient.Encoding.GetBytes(package);
        var encodedfile = System.Text.Encoding.ASCII.GetString(nfile);
        string url = "https://slack.com/api/files.upload?token=" + token + "&content=" + encodedfile + "&channels=" + channelId + "";

        byte[] resp = webClient.UploadData(url, "POST", nfile);

        var k = System.Text.Encoding.Default.GetString(resp);
        Console.WriteLine(k);
    }