繁体   English   中英

如何使用Slack API作为用户上传文件?

[英]How to upload file using Slack API as user?

现在,通过files.upload上传文件时使用的令牌与我的Slack用户帐户相关联。 因此,使用此令牌执行的所有上载似乎都是由我完成的。

但是,我想指定as_user类的东西(使用chat.PostMessagechat.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频道,则有两种选择:

  1. 创建一个机器人用户并使用该机器人用户的访问令牌
  2. 为此,创建一个新的Slack用户(例如“ slackadmin”),并使用该用户的令牌上传文件。

您可以通过自定义或作为Slack应用程序的一部分来创建机器人用户。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM