[알림] 파이썬으로 슬랙에 알림 메시지 보내기 (send messge to slack using python)

2023. 1. 6. 15:17노트/Others : 업무


1. 슬랙에 Incoming WebHooks 앱 추가

2. 포스트할 채널 선택 -> 보낼상대 (내이름) or 채널 선택하면 됌

3. url 저장

4. 메세지 전송

코드

import json 
import sys 
import requests 

url = "https://hooks.slack.com/services/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"

title = ("New Incoming Message :zap:")
message = ("Train완료!")


slack_data = {
    "username" : "NotificationBot",
    "icon_emoji" : ":satellite:",
    "attachments" : [
            {
                "color" : "#9733EE",
                "fields" : [
                    {
                    "title" : title, 
                    "value" : message, 
                    "short" : "false",
                }
            ]
        }        
    ]
}

byte_length = str(sys.getsizeof(slack_data))
headers = {'Content-Type': "appication.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)