问题
I can upload images to Slack via my SlackAPI and I can upload files and I can upload images with comments - but I can not do this with files that are no images. I'm sure it's a problem with my message-structure - have a look: All this is done via HttpClient!
This is my working fileUpload-method:
public MultipartFormDataContent SendFileToChannel()
{
var requestContent = new MultipartFormDataContent();
var fileContent = new StreamContent(GetFile.ReadFile());
requestContent.Add(new StringContent(token), "token");
requestContent.Add(fileContent, "file", Path.GetFileName(GetFile.path));
return requestContent;
}
there is no 'channel' in this method because I don't want to publish it yet. Then I set "public_url_shared": true and get the public URL(with another method). In the response evreything seems to be okay, shared is set to true and I get a permalink_public-value which I pass on to post a message containing this file but...
Now I should be able to post a message while using the permalink_url I get from the second method. And this works with images. But it doesn't work with files. I allways get the error response "no_file_data". Here is my method for this:
public MultipartFormDataContent SendMessageWithFile(SlackFileResponse resp)
{
var requestContent = new MultipartFormDataContent();
requestContent.Add(new StringContent(token), "token");
requestContent.Add(new StringContent(channel), "channel");
requestContent.Add(new StringContent(text), "text");
requestContent.Add(new StringContent("[{ \"fallback\":\"Anhang\", \"text\":\"\", \"\":\"" + resp.permalink_public + "\"}]"), "attachments");
return requestContent;
}
Am I doing something wrong here? Because via the RTM-Slack one can easily just drag&drop a file in there and add a message to it. So it has to be possible via the SlackAPI ,too. Right?
回答1:
Here is why this approach does not work:
Slack threats images differently from other files. Images are the only type of file that you can include in a message attachment via URL. For that we have the properties image_url
(and thumb_url
) for attachments.
Check out this page for a list of all available attachment properties.
If you want to post any other file in a Slack channel and be able to comment it you need to upload it via files.upload
and share it immediately. That is important, because it is currently not possible via the official API to share a previously uploaded file in a channel at a later point.
To include a comment with your file upload just set the initial_comment
property in your API call.
Example:
var requestContent = new MultipartFormDataContent();
var fileContent = new StreamContent(GetFile.ReadFile());
requestContent.Add(new StringContent(token), "token");
requestContent.Add(new StringContent("my_channel"), "channels");
requestContent.Add(new StringContent("Check out this amazing new file"), "initial_comment");
requestContent.Add(fileContent, "file", Path.GetFileName(GetFile.path));
来源:https://stackoverflow.com/questions/53337534/how-to-upload-a-file-no-image-and-comment-it-at-the-same-time-to-slack-via-s