Search code examples
imagewebhooksslackslack-api

Is it possible to use a file path instead of url as `image_url` when sending a message via Slack API or Incoming Webhook?


Let's assume I have the following block which I want to send via Incoming Webhook to Slack

   {
        "type": "image",
        "title": {
                   "type": "plain_text",
                   "text": "foo bar"
                 },
        "image_url": "https://api.slack.com/img/blocks/example/beagle.png",
        "alt_text": "foo"
    }

but instead of providing a http url as image_url I would like to provide a file path because the file I want to send is in the same folder as my script.

1) Is this possible? I guess no.

2) Is it possible to upload an image via files.upload in a first step WITHOUT actually displaying is on any channel just to get the URL which then can be used as image_url?


Solution

  • 1) No. As I mentioned earlier its not possible to use a file path to directly include an image in a message. The only way is to provide a public URL to an image file.

    2) Yes, you can use files.upload you uploaded to Slack in your messages. And you do not have to share them in any channel do it.

    The basic approach is:

    • Upload your image to Slack with files.upload. And if you do not provide any channels with that API call the file will not be shared in any channel. You will get the file object incl. the file ID of the uploaded image file.

    • Call files.sharedPublicURL to make that file public by providing the file ID

    Your image file is now public and you can use the permalink_public of the file object to include that image in message. However, you still need to construct the direct URL for that image from the URL you get from permalink_public. Check out this post on how to get the direct URL for the image.