A message with an attached file on the slack

I am coding a simple C # console application to communicate with Slack.com. I do this through their WebApi. I currently know how to send messages (with attachments, colored, links, users, etc.) and send files to the server.

If you send the file in the normal way ("Upload File" on the left side of the input text box), this file will appear in the main conversation window. But how can you achieve the same through WebAPI? Currently, after uploading a file, I see it in the right sidebar where only all files are listed.

Also the second question arises: is it possible to change the color of the text in the message (there is no "line" in the application)?

This is the answer after submitting the file via 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
    }
}

      

Sorry, I don't know how to format this nicely.

"is_external" and "is_public" are false, maybe this is the reason, but how can I set them to "true"?

- -> Thanks for the edit! :) This is the whole function I'm using:

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

      

EDIT1: On this line:

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

      

Url:

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

      

Then I pass the byte array.

EDIT:

I solved the problem. The point is that the channel should be a channel id, not a channel name ... silly mistake :(

+3


source to share


3 answers


If you add pretty = 1 after the channel property it works better.

Example:



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

      

+2


source


Hi here's a clean example with 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;
    }

      

0


source


I had to change the code a bit to get it to work for anyone still looking for this. I needed to convert an array of files to an ASCII encoded string and then output the details into parameters.

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

      

0


source







All Articles