Skip to content

Instantly share code, notes, and snippets.

@Tim-Schwalbe
Created April 16, 2019 16:01
Show Gist options
  • Save Tim-Schwalbe/3e31ad655bb153190c896f481603d771 to your computer and use it in GitHub Desktop.
Save Tim-Schwalbe/3e31ad655bb153190c896f481603d771 to your computer and use it in GitHub Desktop.
python - mattermost api v4 send file into channel
import os
import json
import requests
SERVER_URL = "YOUR_SERVER_URL"
CHANNEL_ID = "YOUR_CHANNEL_ID"
FILE_PATH = './test.jpg'
s = requests.Session()
s.headers.update({"Authorization": "Bearer YOUR_PERSONAL_ACCESS_TOKEN"})
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/v4/files', files=form_data)
FILE_ID = r.json()["file_infos"][0]["id"]
p = s.post(SERVER_URL + '/api/v4/posts', data=json.dumps({
"channel_id": CHANNEL_ID,
"message": "YOUR_MESSAGE",
"file_ids": [ FILE_ID ]
}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment