Based on the documentation provided here -> https://api.slack.com/tutorials/tracks/uploading-files-python#upload-file. I can upload a file to slack , via python slack sdk (sample code below). I noticed the channel id is optional in the call below. in the response, there is url_private field to the file. to share this or make it public. the api has another method -> files.sharedPublicURL. but there is no example of this , on how to call this api method.
my first question is has anyone tried to make a file public. another question I have is , if I add a Channel id to the file upload call, where does the file live and for how long? is the time configurable. say after 30 days file is deleted.
also , if I wanted to share this file , in other channels or in direct message to user programatically. is that possible?
request
new_file = client.files_upload_v2(
title="My Test Text File",
filename="test.txt",
content="Hi there! This is a text file!",
)
response
{
"ok": true,
"file": {
"id": "F0TD00400",
"name": "dramacat.gif",
"title": "dramacat",
"mimetype": "image/jpeg",
"filetype": "gif",
"pretty_type": "JPEG",
"user": "U0L4B9NSU",
"editable": false,
"size": 43518,
"mode": "hosted",
"is_external": false,
"external_type": "",
"is_public": false,
"public_url_shared": false,
"display_as_bot": false,
"username": "",
"url_private": "https://.../dramacat.gif",
"url_private_download": "https://.../dramacat.gif",
...
The method client.files_upload_v2()
will be deprecated, and the Slack documentation you refer to unfortunately does not mention this.
New Slack Bots/Apps are unable to access this method, and this method will be deprecated entirely next year. If you have not created your bot in March (as far as I know) of this year, likely you will not be able to use this method anymore.
But there is a new way of doing things, documented here and here. This should work:
from slack_sdk import WebClient
import base64
import os, io
import requests
from io import BytesIO
from PIL import Image
from IPython.display import display
message = "Test upload"
example_base64_image = "/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAoHCBYWFRgVFhYYGBYaHBgYGBoaFRoaGhoYGBgaGRgYGBgcIS4lHB4rHxgYJjgmKy8xNTU1GiQ7QDs0Py40NTEBDAwMEA8QGhISGjEhGCExNDE0MTExNDQxNDQ0NDQ0NDQ0NDQ0MTQ/NDQ0NDQ/NDE0NDExMTE/MTQ0NDQxMTExMf/AABEIAMIBAwMBIgACEQEDEQH/xAAbAAABBQEBAAAAAAAAAAAAAAADAQIEBQYAB//EAD4QAAIBAgMFBgQEBAYCAwEAAAECAAMRBBI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flEVCdLR5pkfLF2j4fSaxB6zXYbVBqZjAhmx2U90GvAe0vKN/iXgDZxw1lvjjaopMoqbAPeXeOqBlVuOk0c+knaQugMLsx89IjlGE56QIHCRtkvYsnOUhNotYESo2b/APbB85Nq1MpN5UbPxP8A5K/WCuN2ig3kavoZ2Hrg3sYLEPHEXpHMiYp9D9Yb40jYkaH9+8BEbB85IfdA4QaSQ40k1cYztfYIL+L+0yGaa7tyLIvU/wBpjUccZFb4/DnA8oEprvj3qaaCNRrzOtCN0nUzzikngIme3CI+EdwZwC21vO+J0nLU0ta8Ruutt59IoQW0aJmW17axFYcoBAxKDMdeXsJ0LiQMx+nsIkEgU67WHebhxPITlrt4m9TOnRgypXa/4m9TGis3ib1PKdOgCJWbxN6mSRWbxH1M6dKAtOu/ib8xhGxL+NvzGdOgQYrv4m/MZY7Ox1XKP4j7h87cvOJOjynX4M+Oq/5j/nb9ZOTaNb4f82p/Ub9Z06Wyo2G2lW+H/Nqf1G/WAp7RrfF/m1N4/wD0b9Z06CRMXtKtf+bU/qN+srMLj6vx/wCbU4/O36zp0Di/o7Ur6/xqv9Rv1ja+1K9v51X+o36zp0qfhUJNp1/86r/Ub9Y1tp17fzqv9Rv1izoyLT2lW/zan9Rv1inadf8Azqv9Rv1nTpNOKLbuMqNlzVHO/e5P95SfHa/4m9TEnSNNc/jjWbxN6nnFes3iPqYs6ZtAvjt4m9TF+O3ib1M6dAQ5a7eJvUx6Vm8R9TOnQoK1ZvEfUwZrN4j6mdOhTI1VuZ9TOnTokv/Z"
image_data = base64.b64decode(example_base64_image)
image = Image.open(BytesIO(image_data))
with BytesIO() as buffer:
image.save(buffer, format=image.format) # Save the image to the buffer
image_size_in_bytes = buffer.tell() # Get the size in bytes
print(f"image_size_in_bytes of file {type(image)}: {image_size_in_bytes}")
img_byte_arr = io.BytesIO()
image.save(img_byte_arr, format='PNG')
img_byte_arr = img_byte_arr.getvalue()
attachment = img_byte_arr
slack_channel_id = "Some correct channel ID (not the channel name)"
slack_token = "Some correct token"
slack_client = WebClient(token=slack_token)
# Step 1/4: Get the URL to upload to.
attachment_size = len(attachment)
url_for_uploading = slack_client.files_getUploadURLExternal(
token=slack_token,
filename="Attachment",
length=attachment_size,
)
if url_for_uploading["ok"]:
for item in url_for_uploading:
print(f"{item}")
else:
raise ValueError(
f"Failed to get the URL for uploading the attachment to Slack! Response: {url_for_uploading}"
)
# Step 2/4: Upload the file to the URL.
payload = {
"filename": "attachment.jpeg",
"token": slack_token,
# "channels": ["#dsc-dbx-alerts-test"],
}
response = requests.post(
url_for_uploading["upload_url"], params=payload, data=attachment
)
if response.status_code == 200:
print(
f"Response from Slack: {response.status_code}, {response.text}"
)
else:
raise ValueError(
f"Response from Slack: {response.status_code}, {response.text}, {response.headers}"
)
file_id = url_for_uploading["file_id"]
# Step 3/4: Make the file accessible in the channel.
slack_client.files_completeUploadExternal(
token=slack_token,
files=[{"id": file_id, "title": "Attachment"}],
channel_id=slack_channel_id,
initial_comment=None,
thread_ts=None,
)
attachment_with_slack_url = {
"title": "Attachment",
"image_url": url_for_uploading["upload_url"],
}
# Step 4/4: Send the message to the specified Slack channel.
response = slack_client.chat_postMessage(
channel=slack_channel_id,
text=message,
attachments=[attachment_with_slack_url],
)
# Check if message sending was successful.
if response.status_code != 200:
raise ValueError(
f"Failed to send the message to Slack! Status code returned from the Slack API: {response.status_code}"
)
Your Slack Bot will need sufficient access rights ("scopes"). You can set them here, just click on your bot, scroll down, and provide it with the necessary scopes.