Search code examples
pythonpython-3.xslackslack-api

Is it possible to upload a file to some channel without publishing it as a message using Slack API?


I'm trying to use files.upload from Slack API.

with open("test.txt", "rb") as file:
    files = {"file": ("test.txt", file, "text")}

    payload = {
        "title": filename,
        "filename": filename,
        "token": slack_token_bot,
        "channels": [slack_channel],
    }

    res = requests.post(
        "https://slack.com/api/files.upload",
        params=payload,
        files=files,
        timeout=30,
    )

It works fine however it instantly publishes the message in the corresponding channel. I would like to avoid that: I only need the url of the uploaded file so I can later format a custom message including the link as a Block elements.

Is there anyway to upload to a channel without creating a message?

I thought also about using empty "channels" and sharing it later but it wont work easily: How to share uploaded image in a channel?

Actually, if "channels" is empty I can't even see the file using any of the returned url by the request (I guess this is because the file belongs to the bot not the user, so it's private until it's published in a channel).


Solution

  • Unfortunately, there is no way to accomplish what you want without sharing the file in a public channel. Maybe create a public channel that is just for you to upload files to? Not ideal but if you're the only member of that channel it will at least reduce unwanted noise in larger public channels and you will still be able to send your custom message into the channels you need.