import httpx from common.logger import LOG from config.settings import settings TIMEOUT_SEC = 10.0 def _card(title: str, detail: str) -> dict: """Adaptive Card 1.2 — Teams Workflows 수신 webhook 이 받는 최소 모양.""" return { "type": "message", "attachments": [{ "contentType": "application/vnd.microsoft.card.adaptive", "contentUrl": None, "content": { "$schema": "http://adaptivecards.io/schemas/adaptive-card.json", "type": "AdaptiveCard", "version": "1.2", "body": [ {"type": "TextBlock", "text": title, "weight": "Bolder", "wrap": True}, {"type": "TextBlock", "text": detail, "wrap": True}, ], }, }], } async def send(title: str, detail: str) -> bool: """TEAMS_WEBHOOK_URL 이 있을 때만 보낸다. 로그에 주소를 남기지 않는다.""" url = settings.teams_webhook_url.strip() if not url: return False if not url.startswith("https://"): LOG.error("[teams] TEAMS_WEBHOOK_URL 이 https 가 아니다") return False try: async with httpx.AsyncClient(timeout=TIMEOUT_SEC, follow_redirects=False) as client: res = await client.post(url, json=_card(title, detail)) res.raise_for_status() return True except httpx.HTTPError as e: LOG.error(f"[teams] send failed: {type(e).__name__}") return False