-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
40 lines (30 loc) · 1.21 KB
/
main.py
File metadata and controls
40 lines (30 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
from dotenv import load_dotenv
load_dotenv() # Load env vars BEFORE importing anything else
from fastapi import FastAPI, Request, BackgroundTasks
from src.service import process_slack_event
app = FastAPI()
@app.post("/slack/message")
async def handle_slack_message(request: Request, background_tasks: BackgroundTasks):
"""
Main Endpoint: Handles Verification, Deduplication, and Handoff.
"""
# 1. IGNORE RETRIES
# Slack retries requests if we take too long. We ignore them to prevent duplicate answers.
if request.headers.get("x-slack-retry-num"):
return {"status": "ignored"}
# 2. PARSE JSON
payload = await request.json()
# 3. HANDLE VERIFICATION HANDSHAKE
if payload.get("type") == "url_verification":
return {"challenge": payload.get("challenge")}
# 4. VALIDATE EVENT
if "event" not in payload:
return {"status": "ignored"}
event = payload["event"]
# 5. IGNORE BOTS (Prevent infinite loops)
if "bot_id" in event:
return {"status": "ignored"}
# 6. QUEUE TASK & RETURN INSTANTLY
# This prevents the Slack "3000ms timeout" error.
background_tasks.add_task(process_slack_event, event)
return {"status": "ok"}