Spaces:
Sleeping
Sleeping
debug
Browse files
app.py
CHANGED
|
@@ -76,20 +76,23 @@ class SupabaseAsyncInstallationStore(AsyncInstallationStore):
|
|
| 76 |
"app_id": installation.app_id,
|
| 77 |
}
|
| 78 |
try:
|
| 79 |
-
await asyncio.to_thread(
|
| 80 |
lambda: self.supabase.table("installations").upsert(data, on_conflict="team_id").execute()
|
| 81 |
)
|
| 82 |
-
logger.info(f"Saved installation for team: {installation.team_id}")
|
| 83 |
except Exception as e:
|
| 84 |
logger.error(f"Failed to save installation for team {installation.team_id}: {e}")
|
| 85 |
raise
|
| 86 |
|
| 87 |
async def fetch_installation(self, team_id: str, *, enterprise_id: str | None = None, user_id: str | None = None) -> Installation | None:
|
|
|
|
| 88 |
try:
|
| 89 |
result = await asyncio.to_thread(
|
| 90 |
lambda: self.supabase.table("installations").select("*").eq("team_id", team_id).execute()
|
| 91 |
)
|
|
|
|
| 92 |
if not result.data:
|
|
|
|
| 93 |
return None
|
| 94 |
|
| 95 |
data = result.data[0]
|
|
@@ -357,12 +360,11 @@ async def handle_mention(event, say, client):
|
|
| 357 |
# Initialize the Async Slack Request Handler
|
| 358 |
handler = AsyncSlackRequestHandler(app)
|
| 359 |
|
| 360 |
-
# In the slack_events endpoint (around line 364)
|
| 361 |
@api.post("/slack/events")
|
| 362 |
async def slack_events(request: Request):
|
| 363 |
"""Endpoint for all Slack event subscriptions."""
|
| 364 |
try:
|
| 365 |
-
return await handler.handle(request)
|
| 366 |
except Exception as e:
|
| 367 |
logger.error(f"Error handling Slack events: {e}")
|
| 368 |
raise
|
|
@@ -403,26 +405,38 @@ async def install_url():
|
|
| 403 |
logger.error(f"Error generating install URL: {e}")
|
| 404 |
raise
|
| 405 |
|
| 406 |
-
# In the oauth_callback endpoint
|
| 407 |
@api.get("/slack/oauth/callback")
|
| 408 |
async def oauth_callback(request: Request):
|
| 409 |
"""Handles the OAuth callback from Slack to complete installation."""
|
| 410 |
try:
|
| 411 |
-
response = await handler.handle(request)
|
|
|
|
| 412 |
# For successful OAuth, return a simple HTML response
|
| 413 |
if hasattr(response, 'status_code') and response.status_code == 200:
|
| 414 |
return HTMLResponse(
|
| 415 |
-
content="<html><body><h1>Installation successful!</h1><p>You can now use the bot in your Slack workspace.</p></body></html>",
|
| 416 |
status_code=200
|
| 417 |
)
|
| 418 |
return response
|
| 419 |
except Exception as e:
|
| 420 |
logger.error(f"OAuth Callback Error: {e}")
|
| 421 |
return HTMLResponse(
|
| 422 |
-
content=f"<html><body><h1>Installation Failed!</h1><p>Error: {str(e)}</p></body></html>",
|
| 423 |
status_code=500
|
| 424 |
)
|
| 425 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 426 |
|
| 427 |
if __name__ == "__main__":
|
| 428 |
port = int(os.environ.get("PORT", 7860))
|
|
|
|
| 76 |
"app_id": installation.app_id,
|
| 77 |
}
|
| 78 |
try:
|
| 79 |
+
result = await asyncio.to_thread(
|
| 80 |
lambda: self.supabase.table("installations").upsert(data, on_conflict="team_id").execute()
|
| 81 |
)
|
| 82 |
+
logger.info(f"Saved installation for team: {installation.team_id}. Result: {result.data}")
|
| 83 |
except Exception as e:
|
| 84 |
logger.error(f"Failed to save installation for team {installation.team_id}: {e}")
|
| 85 |
raise
|
| 86 |
|
| 87 |
async def fetch_installation(self, team_id: str, *, enterprise_id: str | None = None, user_id: str | None = None) -> Installation | None:
|
| 88 |
+
logger.info(f"Fetching installation for team_id: {team_id}, enterprise_id: {enterprise_id}, user_id: {user_id}") # DEBUG LOG
|
| 89 |
try:
|
| 90 |
result = await asyncio.to_thread(
|
| 91 |
lambda: self.supabase.table("installations").select("*").eq("team_id", team_id).execute()
|
| 92 |
)
|
| 93 |
+
logger.info(f"Fetch result for {team_id}: {len(result.data)} rows") # DEBUG LOG
|
| 94 |
if not result.data:
|
| 95 |
+
logger.warning(f"No installation found for team {team_id}") # Instead of error here
|
| 96 |
return None
|
| 97 |
|
| 98 |
data = result.data[0]
|
|
|
|
| 360 |
# Initialize the Async Slack Request Handler
|
| 361 |
handler = AsyncSlackRequestHandler(app)
|
| 362 |
|
|
|
|
| 363 |
@api.post("/slack/events")
|
| 364 |
async def slack_events(request: Request):
|
| 365 |
"""Endpoint for all Slack event subscriptions."""
|
| 366 |
try:
|
| 367 |
+
return await handler.handle(request)
|
| 368 |
except Exception as e:
|
| 369 |
logger.error(f"Error handling Slack events: {e}")
|
| 370 |
raise
|
|
|
|
| 405 |
logger.error(f"Error generating install URL: {e}")
|
| 406 |
raise
|
| 407 |
|
|
|
|
| 408 |
@api.get("/slack/oauth/callback")
|
| 409 |
async def oauth_callback(request: Request):
|
| 410 |
"""Handles the OAuth callback from Slack to complete installation."""
|
| 411 |
try:
|
| 412 |
+
response = await handler.handle(request)
|
| 413 |
+
logger.info(f"OAuth callback response: {response.status_code if hasattr(response, 'status_code') else 'No status'}") # DEBUG
|
| 414 |
# For successful OAuth, return a simple HTML response
|
| 415 |
if hasattr(response, 'status_code') and response.status_code == 200:
|
| 416 |
return HTMLResponse(
|
| 417 |
+
content="<html><body><h1>Installation successful!</h1><p>You can now use the bot in your Slack workspace.</p><p>Check Supabase for new installation data.</p></body></html>",
|
| 418 |
status_code=200
|
| 419 |
)
|
| 420 |
return response
|
| 421 |
except Exception as e:
|
| 422 |
logger.error(f"OAuth Callback Error: {e}")
|
| 423 |
return HTMLResponse(
|
| 424 |
+
content=f"<html><body><h1>Installation Failed!</h1><p>Error: {str(e)}</p><p>Check logs and Supabase.</p></body></html>",
|
| 425 |
status_code=500
|
| 426 |
)
|
| 427 |
|
| 428 |
+
# Optional: Add a debug endpoint to check installation
|
| 429 |
+
@api.get("/debug/installations")
|
| 430 |
+
async def debug_installations():
|
| 431 |
+
"""Debug: List all installations (remove in prod)."""
|
| 432 |
+
try:
|
| 433 |
+
result = await asyncio.to_thread(
|
| 434 |
+
lambda: supabase.table("installations").select("*").execute()
|
| 435 |
+
)
|
| 436 |
+
return {"installations": result.data}
|
| 437 |
+
except Exception as e:
|
| 438 |
+
return {"error": str(e)}
|
| 439 |
+
|
| 440 |
|
| 441 |
if __name__ == "__main__":
|
| 442 |
port = int(os.environ.get("PORT", 7860))
|