slackapi/python-slack-sdk

Need help with Python and files_completeUploadExternal method

angrychimp opened this issue · 5 comments

I'm attempting to upload a file and share it to a private channel. I'm using a bot token for an internal app. I've confirmed that the app is installed in the channel, and has the file:read and file:write permissions for my workspace.

Reproducible in:

The Slack SDK version

slack-bolt==1.18.1
slack-sdk==3.26.0

Python runtime version

Python 3.11.8

OS info

❯ sw_vers
ProductName:		macOS
ProductVersion:		14.5
BuildVersion:		23F79
❯ uname -v
Darwin Kernel Version 23.5.0: Wed May  1 20:13:18 PDT 2024; root:xnu-10063.121.3~5/RELEASE_ARM64_T6030

Steps to reproduce:

  1. Use below code
  2. Run script with a valid SLACK_BOT_TOKEN and SLACK_CHANNEL
import os
import requests
from io import StringIO
from slack_sdk import WebClient
slack = WebClient(token=os.environ.get('SLACK_BOT_TOKEN'))
output = "sample text output"
io_stream = StringIO(output)
slack_post = slack.chat_postMessage(channel=os.environ.get('SLACK_CHANNEL'), text=f"placeholder", unfurl_links=True)
upload_url = slack.files_getUploadURLExternal(filename=f"file.txt", length=len(output), alt_txt=f"file", snippet_type="text")
resp = requests.post(upload_url['upload_url'], files={"file": (f"file.txt", io_stream, "application-type")})
completed = slack.files_completeUploadExternal(files=[{"id": upload_url['file_id'], "title": "file"}], initial_comment="file", channel_id=slack_post['channel'], thread_ts=slack_post['ts'])

Expected result:

File should be uploaded successfully and shared in the given channel as attachment/snippet in the thread under the original message.

Actual result:

Successful 'ok' responses are returned from each call, but the snippet does not appear in the message thread. Also, the private file URLs returned in response to the completeUploadExternal call appear to error out - implying the file uploads are unsuccessful.

Additional Info

I've posted this question in the community Slack:
https://community.slack.com/archives/CHY642221/p1716924245139989

I was referred to this project by @filmaj

Hey @angrychimp, is there a reason you are not using the files_upload_v2 method the SDK provides? It wraps up this entire song-and-dance of making the getUploadURLExternal call, POSTing to the upload URL, and then calling completeUpload.

Here's my Python app code:

    resp = client.chat_postMessage(channel=body["channel_id"], text="gonna post a file in thread here shortly")
    client.files_upload_v2(
        channel=body["channel_id"],
        snippet_type="text",
        alt_text="sample file",
        filename="file.txt",
        content="things,stuff\nbits,bobs\n",
        thread_ts=resp["ts"]
    )

And here's the result in my production workspace:

Screenshot 2024-05-29 at 8 38 46 AM

Thanks - I was looking at the API reference and did not see anything for the new files_upload_v2, and did not think to look at the SDK docs.

Using this new method seems to work just fine. I appreciate the help.

files_upload_v2 doesn't seem to support the alt_text param? Any insight appreciated, otherwise will open a new issue.

I don't think so. You can pass alt_txt to the method.

Yes, alt_txt works. Thanks.