How do I upload a file using the Slack API as a user?
Currently, the token used when uploading files via files.upload
is associated with my Slack user account. So any uploads done using this token appear to have been done by me.
However, I would like to specify something like as_user
(which is available in use chat.PostMessage
) that will make the download appear as if it was downloaded by the specified Slack user. Is it possible?
I have it:
upload_file(filepath='/path/to/file.jpg',
channels='#uploads',
title='my image',
initial_comment='pls give me some feedback!')
And here the function is called:
import os
import requests
TOKEN = your_token_here
def upload_file(
filepath,
channels,
filename=None,
content=None,
title=None,
initial_comment=None):
"""Upload file to channel
Note:
URLs can be constructed from:
https://api.slack.com/methods/files.upload/test
"""
if filename is None:
filename = os.path.basename(filepath)
data = {}
data['token'] = TOKEN
data['file'] = filepath
data['filename'] = filename
data['channels'] = channels
if content is not None:
data['content'] = content
if title is not None:
data['title'] = title
if initial_comment is not None:
data['initial_comment'] = initial_comment
filepath = data['file']
files = {
'file': (filepath, open(filepath, 'rb'), 'image/jpg', {
'Expires': '0'
})
}
data['media'] = files
response = requests.post(
url='https://slack.com/api/files.upload',
data=data,
headers={'Accept': 'application/json'},
files=files)
return response.text
I found this existing question , but I am not at all clear on the answer to what can be done to make this work.
source to share
There is no option as_user
to upload and share files via the API. If you want to upload files to your Slack channel as another user via files.upload
, you have two options:
- Create a bot user and use that bot user access token
- Create a new Slack user for this purpose (eg "slackadmin") and use that user's token to upload the file.
You can create user bots via setup or as part of the Slack app.
source to share