Files
Stock-trading-programming/.claude/discord_notify.py
T
whdwo798 a3f54fab47 [2026-05-15] Discord 알림 중복 발송 수정
- 커밋 없는 세션(스케줄러 태스크 등)은 알림 생략
- commit + push 완료 후 1회만 발송
- push 안된 경우 경고 메시지 포함

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-15 15:02:25 +09:00

46 lines
1.6 KiB
Python

import subprocess, json, urllib.request, datetime, sys, pathlib
WEBHOOK = "https://discord.com/api/webhooks/1504705352166543370/H3dhs_4LaxnaFj_mhUUi02qpk3JegE_Ji9C6rNpEqRNv_iW6aqvso8VyPw17048Nt0oF"
def run(cmd):
r = subprocess.run(cmd, capture_output=True, text=True, encoding='utf-8', errors='replace')
return r.stdout.strip(), r.returncode
SESSION_SHA_FILE = pathlib.Path('.claude/session_start_sha.txt')
start_sha = SESSION_SHA_FILE.read_text().strip() if SESSION_SHA_FILE.exists() else ''
now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M')
# 이번 세션 커밋 메시지
if start_sha:
messages, _ = run(['git', 'log', '--pretty=format:%s', f'{start_sha}..HEAD'])
else:
messages, _ = run(['git', 'log', '--pretty=format:%s', '-5'])
# 커밋 없으면 조용히 종료 (스케줄러 태스크 등 노이즈 방지)
if not messages.strip():
sys.exit(0)
# push 상태 확인
unpushed, _ = run(['git', 'log', '--oneline', 'origin/master..HEAD'])
if unpushed:
push_status = "⚠️ Push 안됨 — 수동으로 push 필요"
else:
push_status = "✅ Commit + Push 완료"
bullets = '\n'.join(f"{m}" for m in messages.splitlines() if m.strip())
msg = f"**[Claude Code] {now}**\n\n**변경 내용:**\n{bullets}\n\n{push_status}"
if len(msg) > 1990:
msg = msg[:1987] + '...'
data = json.dumps({'content': msg}).encode('utf-8')
req = urllib.request.Request(
WEBHOOK, data=data,
headers={'Content-Type': 'application/json', 'User-Agent': 'DiscordBot (stockbot, 1.0)'},
method='POST'
)
try:
urllib.request.urlopen(req)
except Exception as e:
print(f"Discord notify failed: {e}", file=sys.stderr)