2017-02-17 7 views
2

나는 텍스트를 게시 simplly incoming webhooks들어오는 webhook을 통해 파일을 보내는 방법은 무엇입니까?

import requests, json 
URL = 'http://chat.something.com/hooks/1pgrmsj88qf5jfjb4eotmgfh5e' 
payload = {"channel": "general", "text": "some text"} 
r = requests.post(URL, data=json.dumps(payload)) 

통해 Mattermost 채널에이 코드를 텍스트를 보낼 수 있어요. 파일을 채널에 게시하는 방법을 찾지 못했습니다. /home/alok/Downloads/Screenshot_20170217_221447.png에 파일을 게시하고 싶습니다. 누구든지 공유하시기 바랍니다.

답변

2

현재 Incoming Webhooks API를 사용하여 파일을 첨부 할 수 없습니다. 파일이 첨부 된 게시물을 만들려면 Mattermost Client API을 사용해야합니다. 여기

SERVER_URL = "http://chat.example.com/" 
TEAM_ID = "team_id_goes_here" 
CHANNEL_ID = "channel_id_goes_here" 
USER_EMAIL = "[email protected]" 
USER_PASS = "password123" 
FILE_PATH = '/home/user/thing_to_upload.png' 

import requests, json, os 

# Login 
s = requests.Session() # So that the auth cookie gets saved. 
s.headers.update({"X-Requested-With": "XMLHttpRequest"}) # To stop Mattermost rejecting our requests as CSRF. 

l = s.post(SERVER_URL + 'api/v3/users/login', data = json.dumps({'login_id': USER_EMAIL, 'password': USER_PASS})) 

USER_ID = l.json()["id"] 

# Upload the File. 
form_data = { 
     "channel_id": ('', CHANNEL_ID), 
     "client_ids": ('', "id_for_the_file"), 
     "files": (os.path.basename(FILE_PATH), open(FILE_PATH, 'rb')), 
} 
r = s.post(SERVER_URL + 'api/v3/teams/' + TEAM_ID + '/files/upload', files=form_data) 

FILE_ID = r.json()["file_infos"][0]["id"] 

# Create a post and attach the uploaded file to it. 
p = s.post(SERVER_URL + 'api/v3/teams/' + TEAM_ID + '/channels/' + CHANNEL_ID + '/posts/create', data = json.dumps({ 
    'user_id': USER_ID, 
    'channel_id': CHANNEL_ID, 
    'message': 'Post message goes here', 
    'file_ids': [FILE_ID,], 
    'create_at': 0, 
    'pending_post_id': 'randomstuffogeshere', 
})) 
(> = 3.5 Mattermost에 사용 Mattermost의 API v3의) 그것을 얻을 수있는 방법의 예