【问题标题】:Post message with file attached on slack在 slack 上发布带有附加文件的消息
【发布时间】:2015-04-17 13:15:33
【问题描述】:

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

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

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

这是通过https://slack.com/api/files.upload发送文件后的响应

{
    "ok": true,
    "file": {
        "id": "F04EX4***",
        "created": 1429279966,
        "timestamp": 1429279966,
        "name": "Testing.txt",
        "title": "Testing",
        "mimetype": "text\/plain",
        "filetype": "text",
        "pretty_type": "Plain Text",
        "user": "U*********",
        "editable": true,
        "size": 28,
        "mode": "snippet",
        "is_external": false,
        "external_type": "",
        "is_public": false,
        "public_url_shared": false,
        "url": "https:\/\/slack-files.com\/files-pub\/T*******\
/testing.txt",
        "url_download": "https:\/\/slack-files.com\/files-pub\/T************\
/download\/testing.txt",
        "url_private": "https:\/\/files.slack.com\/files-pri\/T*******\
/testing.txt",
        "url_private_download": "https:\/\/files.slack.com\/files-pri\/T**********\
 /download\/testing.txt",
        "permalink": "https:\/\/******.slack.com\/files\/******\
/F0******\/testing.txt",
        "permalink_public": "https:\/\/slack-files.com\/********",
        "edit_link": "https:\/\/******.slack.com\/files\/****\/F******\/testing.txt\/edit",
        "preview": "This is a test file number2.",
        "preview_highlight": "<div class=\
"sssh-code\"><div class=\"sssh-line\"><pre>This is a test file number2.<\/pre><\/div>\n
<\/div>",
        "lines": 1,
        "lines_more": 0,
        "channels": [],
        "groups": [],
        "ims": [],
        "comments_count": 0
    }
}

对不起,我不知道如何很好地格式化这个。

“is_external”和“is_public”都是假的,也许这就是原因 - 但我怎样才能将它们设置为“真”?

->> 感谢您的编辑! :) 这是我正在使用的整个功能:

public static void SlackSendFile()
    {
        FileStream str = File.OpenRead(@"C:\Users\Eru\Desktop\Testing.txt");
        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);
        string url = "https://slack.com/api/files.upload?token=" + Token + "&content=" + nfile + "&channels=[" + Channel + "]";

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

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

编辑1: 在这一行:

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

网址是:

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

然后我传递字节数组。

编辑:

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

【问题讨论】:

  • 是的,这就是我发送文件的方式。但这不会在主对话窗口中显示该文件。
  • 尝试调试您的应用程序以查看返回的 http 响应,同时确保您的应用程序的信任级别设置为完全。
  • 你能告诉我们你也发布的 JSON 吗?
  • 问题已解决 -> 见帖子,最后“编辑”

标签: c# slack-api


【解决方案1】:

如果在channel属性后面加上pretty=1,效果会更好。

例子:

string url = "https://slack.com/api/files.upload?token=" + token + "&content=" + nfile + "&channels=[" + Channel + "]&pretty=1"

【讨论】:

    【解决方案2】:

    您好,这里是 RestSharp 的一个干净示例

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

    【讨论】:

      【解决方案3】:

      我不得不稍微更改您的代码,以使其适用于仍在寻找此功能的任何人。我需要将文件数组转换为 ASCII 编码的字符串,然后将详细信息提取到参数中。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-12-25
        • 1970-01-01
        • 1970-01-01
        • 2015-11-27
        • 1970-01-01
        • 2015-10-16
        • 1970-01-01
        相关资源
        最近更新 更多