Search code examples
pythonslackslack-api

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


I want to use a local file (image) in a block. As far as I know there is no way to use a local image in a message block directly, so I've decided to try to use a files.upload to first upload the image to slack and then use the link to that image in a block.

  1. Upload image using files.upload
  2. Get link for that image
  3. Post a message block with the link to the image.

Problem is no matter what I do, I can't seem to get a valid link that I can then use in the message block. (All I get are "downloading image failed" errors).

I don't want the image to be publicly accessible. I want it to be only accessible to slack users logged into our slack workspace. So I need a link that my slackbot can use but not a public link.

Is there any way to do this?

I've already checked this related question and followed instructions there to try and get a link useable by my bot but I couldn't make it work.

Thank you very much for any help.

Block I'm trying to send: (for now I'm not dynamically building the link, just trying to do it manually till I get a link that works)

webclient.chat_postMessage(
channel=channel_id,
blocks=[
    {
        "type": "section",
        "text": {
            "type": "mrkdwn",
            "text": "TEST TEST TEST"
        },
        "accessory": {
            "type": "image",
            "image_url": "https://slack-files.com/files-pri/T024G****-FU85H****/file?pub_secret=e54507****",
            "alt_text": "User's picture"
        }
    }
]

Files.upload response:

{'text': '', 'files': [{'id': 'FU85H****', 'created': 158279****, 'timestamp': 158279****, 'name': 'file', 'title': "User's image:", 'mimetype': 'image/png', 'filetype': 'png', 'pretty_type': 'PNG', 'user': 'UPTM1****', 'editable': False, 'size': 114035, 'mode': 'hosted', 'is_external': False, 'external_type': '', 'is_public': False, 'public_url_shared': False, 'display_as_bot': False, 'username': '', 'url_private': 'https://files.slack.com/files-pri/T024G****-FU85H****/file', 'url_private_download': 'https://files.slack.com/files-pri/T024G****-FU85H****/download/file', 'thumb_64': 'https://files.slack.com/files-tmb/T024G****-FU85H****-9513e3****/file_64.png', 'thumb_80': 'https://files.slack.com/files-tmb/T024G****-FU85H****-9513e3****/file_80.png', 'thumb_360': 'https://files.slack.com/files-tmb/T024G****-FU85H****-9513e3****/file_360.png', 'thumb_360_w': 291, 'thumb_360_h': 256, 'thumb_160': 'https://files.slack.com/files-tmb/T024G****-FU85H****-9513e3****/file_160.png', 'image_exif_rotation': 1, 'original_w': 291, 'original_h': 256, 'thumb_tiny': 'AwAqADBYCFJ3EgEY4qx5kRB+8OAOnpSC3T1NKYEA6mgBGnhQ7mYjknp60xZYHA2OSVHHHSq5RWnfdyFOBmlkiVcFTgjnilcdie4lWQDGeKr7gDVw2yHuaYbRPVqegiXdQW4qIvUEl0icZyfQUAOYDc2R3zSoodgOvaoomaXLnjngelSOXjQupwRzUlrYvGmmq****/DHY3v0qbNUQZ11KVUKDyetVKmuv9b+FQUAWrST5yD3qS8k+UKD15qrF98fUf****/1x+goHfQhJycmrlnOeIzz6V****/160CP/9k=', 'permalink': 'https://gaia.slack.com/files/UPTM1****/FU85H****/file', 'permalink_public': 'https://slack-files.com/T024G****-FU85H****-e54507****', 'is_starred': False, 'has_rich_preview': False}], 'upload': True, 'user': 'UPTM1****', 'display_as_bot': False, 'team': 'T024G****', 'source_team': 'T024G****', 'user_team': 'T024G****', 'channel': 'DUFV5****', 'event_ts': '158279****.002500', 'ts': '158279****.002500'}

Solution

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