I need a slack bot that's able to receive and save files send from slack chatrooms.
The problem is: slack doesn't send file contents, but an array of links pointing to the file. Most of them, including download link are private and cannot be accessed via bot. It does send one public link, but that link points at the file preview, which does not have the file itself (here's an example).
So the question is: how can I access uploaded files via bot?
Once you've saved a message or file, Saved items will appear near the top of your left sidebar. You can find all of your saved items there.
Bots can do a lot of the same things in Slack that regular members can: They have names, profiles, profile photos, and exist in the directory. They can be @mentioned and sent direct messages. They can post messages and upload files.
You can access private URLs from your bot by providing an access token in the HTTP header when you are doing you CURL request.
Your token needs to have the scope files.read
in order to get access.
The format is:
Authorization: Bearer A_VALID_TOKEN
Replace A_VALID_TOKEN with your slack access token.
I just tested it with a simple PHP script to retrieve a file by its "url_private" and it works nicely.
Source: Slack API documententation / file object / Authentication
Example for using the Python requests library to fetch an example file:
import requests url = 'https://slack-files.com/T0JU09BGC-F0UD6SJ21-a762ad74d3' token = 'xoxp-8853424449-8820034832-8891394196-faf6f0' requests.get(url, headers={'Authorization': 'Bearer %s' % token})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With