2017-03-01 45 views
1

現在,通過files.upload上傳文件時使用的令牌與我的Slack用戶帳戶相關聯。因此,使用此令牌執行的任何上傳似乎都是由我製作的。如何使用Slack API作爲用戶上傳文件?

但是,我想指定類似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,但我沒有找到答案清楚在所有關於什麼可以做做這個工作。

回答

2

沒有as_user選項通過API上傳和共享文件。如果你想將文件上傳到鬆弛的通道作爲通過files.upload不同的用戶有兩種選擇:

  1. 創建bot user,並使用該機器人用戶
  2. 的訪問令牌爲此創建一個新的鬆弛用戶(例如「slackadmin」)並使用該用戶的令牌上傳文件。

您可以通過自定義或作爲Slack應用程序的一部分來創建殭屍工具用戶。