How to Upload an Image as Attachment with Slack API

Use a files.upload image as an image in a slack message block

OP wants to create message block with a private image URL. That is not possible.

Image URLs in messages blocks (and attachments) need to be public.

An alternative is to directly share an uploaded image file in a channel and include a message.

Details on how to include images in message are described in this answer.

Uploading and attaching images to private messages from bots in Slack

My mistake came from misreading the docs - the files.upload API expects a comma-separated string for the channels property and I was passing an array.

Once I had corrected this I could successfully upload files and share them with a user privately (channel IDs starting with 'D') and in public channels (channel IDs starting with 'C').

Copying the image URL and pasting it in a browser brought me to a login page, so by default they are private to the Slack team as I had hoped.

How to post Slack formated message with local attachment?

In order to attach a local image file to a message you need to first upload it so you get a public URL. Then you can use that URL in your message attachment.

This works with any public image service (e.g. imgur.com) and you can also use any Slack workspace as host for public image files.

Check out this answer for details including a complete example in C#.

How to upload any file on Slack via Slack-App in c#

Here is a shorter working example showing how to just upload any file to Slack with C# only. The example will also automatically share the file the given channel.

I have included the logic to convert the API response from JSON, which will always be needed to determine if the API call was successful.

Note: This example requires Newtonsoft.Json

using System;
using System.Net;
using System.Collections.Specialized;
using System.Text;
using Newtonsoft.Json;

public class SlackExample
{
// classes for converting JSON respones from API method into objects
// note that only those properties are defind that are needed for this example

// reponse from file methods
class SlackFileResponse
{
public bool ok { get; set; }
public String error { get; set; }
public SlackFile file { get; set; }
}

// a slack file
class SlackFile
{
public String id { get; set; }
public String name { get; set; }
}

// main method with logic
public static void Main()
{
var parameters = new NameValueCollection();

// put your token here
parameters["token"] = "xoxp-YOUR-TOKEN";
parameters["channels"] = "test";

var client = new WebClient();
client.QueryString = parameters;
byte[] responseBytes = client.UploadFile(
"https://slack.com/api/files.upload",
"D:\\temp\\Stratios_down.jpg"
);

String responseString = Encoding.UTF8.GetString(responseBytes);

SlackFileResponse fileResponse =
JsonConvert.DeserializeObject<SlackFileResponse>(responseString);
}
}

About content types: Those are part of the header of a HTTP request and can be set manually in the WebClient object (see also this answer). However, for our case you can ignore it, because the default content types that WebClient is using for the POST request will work just fine.

Also see this answer on how to upload files with the WebClient class.



Related Topics



Leave a reply



Submit