如何以用户身份使用Slack API上传文件? [英] How to upload file using Slack API as user?

查看:0
本文介绍了如何以用户身份使用Slack API上传文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,通过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!')

下面是要调用的函数:

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

我确实找到了this existing question,但我根本找不到关于可以执行哪些操作的明确答案。

推荐答案

接口没有as_user上传分享选项。如果您希望通过files.upload以不同用户身份将文件上载到Slack频道,您有两个选择:

  1. 创建bot user并使用该机器人用户的访问令牌
  2. 为此创建新的Slack用户(例如"slackadmin")并使用 要上载文件用户的令牌。

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

这篇关于如何以用户身份使用Slack API上传文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆