slack-apislack-block-kit

Slack API for sending a message with files and blocks


I am using Slack's API for sending messages, using the blocks parameter for formatted messages. Now, I want to add the feature of attaching files to the messages, just like you would via the Slack interface (e.g. copy pasting an image / uploading a pdf).

After exploring the API and searching online, I couldn't find a way to have both blocks and files in the same API call for sending a message:

  1. chat.postMessage allows to use blocks but does not accept file Ids/urls.
  2. files.uploadV2 allows to attach a file to a message with initial_comment as a simple string text, but does not accept blocks for rich text.
  3. I also tried using postMessage with 'image' blocks, which partially achieves my goal (for image files only), but the interfaces of 'image' and 'file' inside Slack are different (can't edit from within Slack a message with 'image' block; the display is different).
  4. Another method I tried is using files.uploadV2, then chat.update with blocks - this seems to work, but marks the message in Slack as edited - a non ideal user experience.

Is there a way to get the full capabilities of 'files' and 'blocks' while sending a message in 1 API call to Slack?

It seems to me it should be possible, since the other direction works - when Slack send an event of a new message in their events, they do share both blocks and files in the same event.


Solution

  • From Slack support:

    For messages posted via the Slack API, we currently only support attaching image files via the slack_file or image block. Alternatively, you could try using remote files to see if that works for you - https://api.slack.com/messaging/files#remote

    If remote files doesn't work, your best bet is option 4 to be honest. Sorry for the bad news here.