Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access Slack files from a slack bot

Tags:

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?

like image 263
ddgd Avatar asked Mar 22 '16 01:03

ddgd


People also ask

Where are the files saved in Slack?

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.

What can Slack bots do?

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.


2 Answers

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

like image 73
Erik Kalkoken Avatar answered Oct 20 '22 07:10

Erik Kalkoken


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}) 
like image 21
Maik Röder Avatar answered Oct 20 '22 07:10

Maik Röder