[英]How to add custom username in Slack API files.upload using bot user?
[英]How to upload file using Slack API as user?
现在,通过files.upload
上传文件时使用的令牌与我的Slack用户帐户相关联。 因此,使用此令牌执行的所有上载似乎都是由我完成的。
但是,我想指定as_user
类的东西(使用chat.PostMessage
时chat.PostMessage
),这将使上载看起来像是由指定的Slack用户上载的。 这可能吗?
我有这个:
upload_file(filepath='/path/to/file.jpg',
channels='#uploads',
title='my image',
initial_comment='pls give me some feedback!')
这是被调用的函数:
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
我确实找到了这个现有问题 ,但对于使这项工作可以做些什么,我根本找不到明确的答案。
没有as_user
选项可用于通过API上传和共享文件。 如果要通过另一个用户通过files.upload
将文件上传到Slack频道,则有两种选择:
您可以通过自定义或作为Slack应用程序的一部分来创建机器人用户。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.