How do I download a file using Slack Bolt Python API?

178 Views Asked by At

I am using Slack Bolt Python API with Socket mode, and I get an event such as this when a user uploads a file to a channel:

Unhandled request ({'type': 'event_callback', 'event': {'type': 'message', 'subtype': 'file_share'}})
---
[Suggestion] You can handle this type of event with the following listener function:

@app.event("message")
def handle_message_events(body, logger):
    logger.info(body)

Unhandled request ({'type': 'event_callback', 'event': {'type': 'message', 'subtype': 'file_share'}})

I am not sure how to get the download URL for this file? The code that I have is not working, which is:

@app.event("file_share")
def file_func(payload, client, ack):
    ack()

    #get the file id every time someone uploads a file
    my_file = payload.get('file').get('id')
    
    #get the json using files_info
    url = client.files_info(file = my_file).get('file').get('url_private')
    file_name = client.files_info(file = my_file).get('file').get('title')

    # save file
    resp = requests.get(url, headers={'Authorization': 'Bearer %s' % token})
    save_file = Path(file_name)
    save_file.write_bytes(resp.content)

I got this code from another site, so it's not mine.

0

There are 0 best solutions below