Slack 알림 서비스를 만들어야해서 Slack과 Python 연동에 대해 찾아보던 중 Incoming Webhooks와 Slack Bot 두 가지 서비스를 알게 되었고, 이 두 가지를 정리하기 위해서 글을 작성하게 되었다. 내가 헷갈려서 작성하는 글이니 .. 내 맘대로 작성할 것임..
Incoming Webhooks는 Slack 외부의 애플리케이션이나 서비스가 단순히 메시지를 전송할 수 있게 해주는 가장 기본적인 방식
동작 방식:
특징:
사용 사례:
내 프로젝트의 코드로 예시를 들자면
def send_webhook_message(text):
payload = {"text": text}
response = requests.post(SLACK_WEBHOOK_URL, json=payload)
if response.status_code == 200:
print("✅ Webhook 메시지 전송 완료")
else:
print(f"⚠️ Webhook 메시지 전송 실패: {response.text}")
이런식으로 간단한 상태 알림을 해주는 기능이다.
send_webhook_message("크롤링 작업이 완료되었습니다. 결과를 확인하세요.")
호출할때는 이렇게 호출하면 된다.
Slack Bot은 Slack API를 활용하여 더 복잡하고 상호작용이 가능한 기능을 제공하는 방식
동작 방식:
특징:
사용 사례:
내 프로젝트의 코드로 예시를 들자면
def send_to_slack(product_name, channel, screenshot_path, message=""):
try:
response = client.files_upload_v2(
channel=SLACK_CHANNEL,
file=screenshot_path,
initial_comment=f"{product_name} ({channel}) - {message}",
)
if response.get("ok"):
print(f"✅ Slack으로 전송 완료: {screenshot_path}")
else:
print(f"⚠️ Slack 전송 실패: {response.get('error')}")
except SlackApiError as e:
print(f"⚠️ Slack API 오류: {e.response['error']}")
이런식으로 스크린샷 file을 보내주는 기능이다.
capture_screenshot(product_name, channel, url)
호출할때는 이렇게 호출하면 된다.