yf176 commited on
Commit
249c3a7
·
1 Parent(s): a07c09d

fix: handle url_verification directly in app.py, no slack-bolt dependency for challenge

Browse files
Files changed (1) hide show
  1. src/polyhedra/app.py +27 -11
src/polyhedra/app.py CHANGED
@@ -14,6 +14,7 @@ SLACK_SIGNING_SECRET — Slack signing secret for request verification
14
 
15
  from __future__ import annotations
16
 
 
17
  import logging
18
  import os
19
 
@@ -23,7 +24,8 @@ logger = logging.getLogger(__name__)
23
  def create_app():
24
  """Build and return the FastAPI application."""
25
  try:
26
- from fastapi import FastAPI
 
27
  except ImportError as exc:
28
  raise ImportError("fastapi is required: pip install fastapi uvicorn") from exc
29
 
@@ -38,20 +40,34 @@ def create_app():
38
  """Liveness probe."""
39
  return {"status": "ok", "service": "polyhedra"}
40
 
41
- # Always mount /slack/events — SlackIntegration handles missing credentials gracefully.
 
42
  try:
43
  from polyhedra.messaging.slack import SlackIntegration
44
-
45
  slack = SlackIntegration()
46
- slack.mount_to_fastapi(app)
47
- logger.info("Slack integration mounted at /slack/events")
48
- except ImportError:
49
- logger.warning(
50
- "slack-bolt not installed — Slack events endpoint disabled. "
51
- "Install with: pip install slack-bolt"
52
- )
53
  except Exception as exc:
54
- logger.warning("Slack integration failed to mount: %s", exc)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
 
56
  return app
57
 
 
14
 
15
  from __future__ import annotations
16
 
17
+ import json
18
  import logging
19
  import os
20
 
 
24
  def create_app():
25
  """Build and return the FastAPI application."""
26
  try:
27
+ from fastapi import FastAPI, Request
28
+ from fastapi.responses import JSONResponse
29
  except ImportError as exc:
30
  raise ImportError("fastapi is required: pip install fastapi uvicorn") from exc
31
 
 
40
  """Liveness probe."""
41
  return {"status": "ok", "service": "polyhedra"}
42
 
43
+ # Slack event handler — always mounted, handles url_verification directly.
44
+ _slack_bolt_handler = None
45
  try:
46
  from polyhedra.messaging.slack import SlackIntegration
47
+ from slack_bolt.adapter.fastapi.async_handler import AsyncSlackRequestHandler
48
  slack = SlackIntegration()
49
+ if slack._app is not None:
50
+ _slack_bolt_handler = AsyncSlackRequestHandler(slack._app)
51
+ logger.info("Slack Bolt handler initialized")
 
 
 
 
52
  except Exception as exc:
53
+ logger.warning("Slack Bolt init failed: %s", exc)
54
+
55
+ @app.post("/slack/events")
56
+ async def slack_events(request: Request) -> JSONResponse:
57
+ # Always handle url_verification directly so Slack can verify the URL
58
+ # even before secrets are fully configured.
59
+ try:
60
+ body = await request.json()
61
+ except Exception:
62
+ body = {}
63
+
64
+ if body.get("type") == "url_verification":
65
+ return JSONResponse({"challenge": body.get("challenge", "")})
66
+
67
+ if _slack_bolt_handler is not None:
68
+ return await _slack_bolt_handler.handle(request)
69
+
70
+ return JSONResponse({"ok": True})
71
 
72
  return app
73