개발 및 공부/네이버 부스트캠프 AI Tech 2기
Python으로 Slack에 알림 보내기
Hㅏㄴ량
2021. 9. 1. 02:12
1. 새 채널 생성
2. 슬랙에 Incoming WebHooks
앱 추가
)
3. 앱을 설치할 채널 선택
4. URL 저장
5. 메세지 전송
import json
import sys
import random
import requests
import os
import numpy as np
import torch
def send_msg(msg):
url = # 웹후크 UR 입력
message = ("Train 완료!!!\n" + msg)
title = (f"New Incoming Message :zap:") # 타이틀 입력
slack_data = {
"username": "NotificationBot", # 보내는 사람 이름
"icon_emoji": ":satellite:",
#"channel" : "#somerandomcahnnel",
"attachments": [
{
"color": "#9733EE",
"fields": [
{
"title": title,
"value": message,
"short": "false",
}
]
}
]
}
byte_length = str(sys.getsizeof(slack_data))
headers = {'Content-Type': "application/json", 'Content-Length': byte_length}
response = requests.post(url, data=json.dumps(slack_data), headers=headers)
if response.status_code != 200:
raise Exception(response.status_code, response.text)
6. 활용
메세지는 자유롭게 변경 가능하지만 나는 .py로 파일을 따로 만들어 train이 끝날 때마다 호출해서 arg로 넘겨준 epoch, loss, acc 등을 출력하게 만들었음
)