Search code examples
pythonslack-api

How to use the permalink_public URL of an uploaded image to include it in a message?


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.


Solution

  • 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.