4

I am trying to upload an image to slack and post it in an image block of a slack message to a specific channel.

  1. upload an image to Slack.
  2. make the image public with files.sharedPublicURL
  3. check if the url is public: public_url_shared being true.
  4. use the permalink_public I receive for the uploaded image for creating the slack message (an image block).

for debugging I am using Slack's Block Kit Builde. I am replacing the URL in the image_url example of the block kit demo with the one I received from slack:

https://slack-files.com/T04AG7BVD-FLWHBHY86-1ba8263c00 

or:

https://slack-files.com/T04AG7BVD-FLNJJURL1-7b17f26c80

The image should be shown. Instead there is the error in Slack's Block Kit Builder as well as a direct slack-api call: Downloading image failed.

If I open the permalink_public in an incognito session. I can see the file. so it is public.

Erik Kalkoken
  • 30,467
  • 8
  • 79
  • 114
sektionschef
  • 123
  • 1
  • 8
  • 1
    I tried open your image link, but it does not show any image. Are you sure its public? This is what I get: https://i.imgur.com/4aLkP3N.png – Erik Kalkoken Jul 29 '19 at 12:21
  • 1
    @ErikKalkoken yes, the image is correct. it's my fallback image. quite confusing. here is another one: https://slack-files.com/T04AG7BVD-FLNJJURL1-7b17f26c80 with the same problem. – sektionschef Jul 29 '19 at 12:33
  • and by the way. with your URL https://i.imgur.com/4aLkP3N.png the image is correctly displayed. – sektionschef Jul 29 '19 at 12:53
  • OK, I think the reason for your problem is that `permalink_public` does not give you a direct link to the image file, but links to a website that shows the image. – Erik Kalkoken Jul 29 '19 at 12:53
  • I think I figured our how to get the direct link. Will put it in an answer. – Erik Kalkoken Jul 29 '19 at 12:56

1 Answers1

10

The reason the link for permalink_public does not work in your layout block is that it links to a public website showing the image, but is not a direct link to the image file (which is what you need of course).

But you can construct a direct image link from the link to the website.

The website link you get from permalink_public has the format:

https://slack-files.com/{team_id}-{file_id}-{pub_secret}

The direct link to the image has the format:

https://files.slack.com/files-pri/{team_id}-{file_id}/{filename}?pub_secret={pub_secret}

So you just need to extract the pub_secret from permalink_public and you should be able to construct the direct link to the image. The other parameters you can get from your file object.

Example for your image:

https://files.slack.com/files-pri/T04AG7BVD-FLWHBHY86/no_image_found.png?pub_secret=1ba8263c00

Note that this does no appear to be a documented approach, so as all undocumented approaches and hacks its subject to change.

Erik Kalkoken
  • 30,467
  • 8
  • 79
  • 114
  • Response from `files.sharedPublicURL` has several links to thumbnails of image e.g. `'thumb_1024': 'https://files.slack.com/files-tmb/{team_id}-{file_id}-123abc/file-name_1024.png'`. They could provide direct link here as well ;-) – Wlad Jul 30 '19 at 15:28
  • What about the `'is_public'` property? It is still false while `'public_url_shared': True`. Would setting `is_public : True` provide a direct link in response or at least make all the thumb links public? – Wlad Jul 30 '19 at 15:32
  • 1
    All other URLs require you to provide token authorization to retrieve the file. That works great if you access a file from your app, but it won't work for a message, because you can't provide authorization there. Here is an example on how that works: https://stackoverflow.com/a/57019688/4379151 – Erik Kalkoken Jul 30 '19 at 15:54
  • 1
    The `is_public` property refers to sharing the file in a public channel on Slack, not making a public URL. – Erik Kalkoken Jul 30 '19 at 15:56
  • When uploading image files with spaces in the filename string replace with underscore – Eugene van der Merwe Oct 24 '21 at 08:24
  • 2
    The `url_private` property has this entire string already `https://files.slack.com/files-pri/{team_id}-{file_id}/{filename}` so you can simplify your implementation by just extracting the `pub_secret` from `permalink_public` and adding the `?pub_secret={pub_secret}` to `url_private`. A bit odd that 2 years later and this is not supported officially... – V Maharajh Nov 20 '21 at 01:13