Study/Python
Slack files.upload로 이미지 전송하기
Zeromk2
2022. 3. 9. 13:35
이번에는 파일 업로드를 해보겠습니다.
파일 업로드는 chat.postMessage API를 사용하지 않고 별도의 API를 사용합니다.
이번에도 헤더 부분 정의가 필요합니다.
Content-type이 application/json 이 아닙니다!
header = {'Content-type': 'application/x-www-form-urlencoded; charset=utf-8', 'Authorization': {slack 토큰}}
data로 보낼 attachments를 지정해줍니다.
이미지를 먼저 객체화 합니다.
with open({전송 이미지 경로}, 'rb') as f:
content = f.read()
그리고 이미지 데이터인 content를 data에 넣어줍니다.
attachments = {
"channels": {슬랙채널ID},
"thread_ts": {쓰레드일 경우 쓰레드ts},
"title": "error screentshot",
"content": content
}
그리고 전송!
requests.post('https://slack.com/api/files.upload', headers=header, data=attachments)
728x90