1. Intro
예전에는 서버에서 실행한 script 결과를 이메일로 보냈다면 이제는 Slack이라는 훌륭한 도구를 이용하여 정보를 주고 받을 수 있다.
Slack의 WebHooks을 사용하면 되는데
서버에서 python을 이용해서 메시지를 전송해보자.
2. Incomming Webhook 생성
Slack에서 채널을 만든 후 “Add an app”을 클릭한다.

“Add configuration”을 클릭하여 설정을 추가한다.

WebHooks을 추가할 때 중요한 것은 “Post to Channel”, “Webhook URL”이다.
이 2개의 정보는 python에서 message를 보낼 때 참조한다.

3. Source Code
#!/usr/bin/python3
# Maker : LT
# Date : 2018.12.12.
# Description : get stock info
import json
import requests
def SendMessage(msg):
webhook_url = "[WebHook URL]"
channel = "#[Channel Name]"
sender = "[Sender Name]"
icon = ":red_car:" # You can change icon : https://www.webpagefx.com/tools/emoji-cheat-sheet
payload = {"channel": channel, \
"username": sender, \
"text": msg, \
"icon_emoji": icon }
requests.post(
webhook_url, data=json.dumps(payload),
headers={'Content-Type': 'application/json'}
)