5

I upload an image to slack using https://slack.com/api/files.upload not specifying channels field in the request. I can see the image in the web interface. What the api call should be to share the uploaded image in some channel at some point in the future?

I tried to upload an image and response from slack was:

 {
    "ok": true,
    "file": {
        "id": "FHJ9QTX1V",
        "created": 1554115093,
        "timestamp": 1554115093,
        "name": "scaled_IMG-20190324-WA0002.jpg",
        "title": "scaled IMG-20190324-WA0002",
        "mimetype": "image/jpeg",
        "filetype": "jpg",
        "pretty_type": "JPEG",
        "user": "UGRR6FCF7",
        "editable": false,
        "size": 217356,
        "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/TGQU3SCHF-FHJ9QTX1V/scaled_img-20190324-wa0002.jpg",
        "url_private_download": "https://files.slack.com/files-pri/TGQU3SCHF-FHJ9QTX1V/download/scaled_img-20190324-wa0002.jpg",
        "thumb_64": "https://files.slack.com/files-tmb/TGQU3SCHF-FHJ9QTX1V-fa34003fce/scaled_img-20190324-wa0002_64.jpg",
        "thumb_80": "https://files.slack.com/files-tmb/TGQU3SCHF-FHJ9QTX1V-fa34003fce/scaled_img-20190324-wa0002_80.jpg",
        "thumb_360": "https://files.slack.com/files-tmb/TGQU3SCHF-FHJ9QTX1V-fa34003fce/scaled_img-20190324-wa0002_360.jpg",
        "thumb_360_w": 360,
        "thumb_360_h": 270,
        "thumb_480": "https://files.slack.com/files-tmb/TGQU3SCHF-FHJ9QTX1V-fa34003fce/scaled_img-20190324-wa0002_480.jpg",
        "thumb_480_w": 480,
        "thumb_480_h": 360,
        "thumb_160": "https://files.slack.com/files-tmb/TGQU3SCHF-FHJ9QTX1V-fa34003fce/scaled_img-20190324-wa0002_160.jpg",
        "thumb_720": "https://files.slack.com/files-tmb/TGQU3SCHF-FHJ9QTX1V-fa34003fce/scaled_img-20190324-wa0002_720.jpg",
        "thumb_720_w": 720,
        "thumb_720_h": 540,
        "thumb_800": "https://files.slack.com/files-tmb/TGQU3SCHF-FHJ9QTX1V-fa34003fce/scaled_img-20190324-wa0002_800.jpg",
        "thumb_800_w": 800,
        "thumb_800_h": 600,
        "thumb_960": "https://files.slack.com/files-tmb/TGQU3SCHF-FHJ9QTX1V-fa34003fce/scaled_img-20190324-wa0002_960.jpg",
        "thumb_960_w": 960,
        "thumb_960_h": 720,
        "thumb_1024": "https://files.slack.com/files-tmb/TGQU3SCHF-FHJ9QTX1V-fa34003fce/scaled_img-20190324-wa0002_1024.jpg",
        "thumb_1024_w": 1024,
        "thumb_1024_h": 768,
        "image_exif_rotation": 1,
        "original_w": 1040,
        "original_h": 780,
        "permalink": "https://autolainen.slack.com/files/UGRR6FCF7/FHJ9QTX1V/scaled_img-20190324-wa0002.jpg",
        "permalink_public": "https://slack-files.com/TGQU3SCHF-FHJ9QTX1V-3366c52c9c",
        "comments_count": 0,
        "is_starred": false,
        "shares": {},
        "channels": [],
        "groups": [],
        "ims": [],
        "has_rich_preview": false
    }
}

And then tried to share the image using /api/chat.postMessage:

{
    "channel": "CH68ZSHFA",
    "text": "test",
    "blocks": [
        {
            "type": "section",
            "text": {
                "type": "plain_text",
                "text": "test"
            }
        },
        {
            "type": "image",
            "image_url": "https://autolainen.slack.com/files/UGRR6FCF7/FHJ9QTX1V/scaled_img-20190324-wa0002.jpg",
            "alt_text": "attachment"
        }
    ],
    "as_user": false,
    "username": "Client name"
}

I used url from url_private, url_private_download, permalink, permalink_public fields of the file description but got the same response:

{
    "ok": false,
    "error": "invalid_blocks",
    "response_metadata": {
        "messages": [
            "[ERROR] downloading image failed [json-pointer:/1/image_url]"
        ]
    }
}

1 Answer 1

3

Unfortunately there is no official API method to share a file on your workspace after it has been uppladed. So if you don't include the channel(s) in your initial files.upload request there is no official way to share that file later on.

But there is an unofficial API method called files.share, which has that very functionality. It works perfectly, but you will need a legacy token to use it, so this might not be a solution for you.

Another way to use an image on Slack is to include it in a message (as you are trying in your code example). Technically speaking that is not the same as sharing a file on Slack, since it only works for images and provide limited functionality for users.

It will work though, but only if your image URL is public, because chat.postMessage only works with public URLs to images and files on Slack are private by default (which means you need to provide authorization in any request to access that file from outside of Slack).

To get a public URL for your file you can call the API method files.sharedPublicURL after you uploaded it. You will get a public URL as response, which you can then use to include that image in your message.

1
  • Thanks for such a good answer! I was thinking I didn't read Slack api documentation properly. Do I understand correctly that if I use files.sharedPublicURL method the file will be accessible by anyone on the internet by public url? If so that's not an option for me.
    – Quadrate
    Apr 1, 2019 at 14:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.