现在,通过上传文件时使用的令牌files.upload
与我的 Slack 用户帐户相关联。因此,使用此令牌执行的任何上传似乎都是由我完成的。
但是,我想指定类似的内容as_user
(在使用时可用chat.PostMessage
),这将使上传看起来好像是由指定的 Slack 用户上传的。这可能吗?
我有这个:
upload_file(filepath='/path/to/file.jpg',
channels='#uploads',
title='my image',
initial_comment='pls give me some feedback!')
Run Code Online (Sandbox Code Playgroud)
这是被调用的函数:
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
Run Code Online (Sandbox Code Playgroud)
我确实找到了这个现有问题,但我根本没有找到明确的答案来说明可以采取哪些措施来使这项工作发挥作用。
没有as_user
通过 API 上传和共享文件的选项。如果您想以其他用户身份将文件上传到 Slack 频道,files.upload
您有两个选择:
您可以通过自定义或作为 Slack 应用程序的一部分来创建机器人用户。