Reputation: 397
I'm coding a simple console application in C# to communicate with Slack.com. I'm doing this via their WebApi. Currently I know how to post messages (with attachments, colored, links, users etc.) and send files to server.
If you send a file in a normal way ("Upload a file" on the left side of typing textbox) this file will be shown in the main conversation window. But how can you achieve the same thing via WebAPI ? Currently after sending the file, I can see it in the right side panel where all files are listed only.
Also there is a second question: is it possible to change to color of text in the message (no the "line" in attachment)?
This is the response after sending 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.
The "is_external" and "is_public" are both false, maybe this is the reason - but how can I set them to "true" ?
->> Thank you 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: In this line:
byte[] resp = webClient.UploadData(url, "POST", nfile);
The URL is:
https://slack.com/api/files.upload?token=*********&content=System.Byte[]&channels=[%23*****]
Then I'm passing the byte array.
EDIT:
I have solved the problem. The thing was that the channel should be the ID of channel, not the name of the channel... stupid mistake :(
Upvotes: 3
Views: 10395
Reputation: 4285
Hi here 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;
}
Upvotes: 3
Reputation: 622
I had to change your code a little bit to get it working for anyone still looking for this. I needed to convert the file array to an ASCII encoded string, and then pulled 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);
}
Upvotes: 1
Reputation: 21
If you add pretty=1 after channel property, it will work better.
Example:
string url = "https://slack.com/api/files.upload?token=" + token + "&content=" + nfile + "&channels=[" + Channel + "]&pretty=1"
Upvotes: 2