ForcePilot/backend/package/yuxi/channels/adapters/slack/probe.py

58 lines
1.9 KiB
Python
Raw Normal View History

from __future__ import annotations
import asyncio
from slack_sdk.errors import SlackApiError
from slack_sdk.socket_mode.aiohttp import SocketModeClient
from slack_sdk.web.async_client import AsyncWebClient
async def probe_bot_token(token: str) -> dict:
if not token.startswith("xoxb-"):
return {"status": "error", "message": "Token must start with 'xoxb-'"}
try:
client = AsyncWebClient(token=token)
auth = await client.auth_test()
if not auth.get("ok"):
return {"status": "error", "message": f"auth.test failed: {auth.get('error')}"}
return {
"status": "ok",
"bot_id": auth.get("bot_id", ""),
"bot_user_id": auth.get("user_id", ""),
"team": auth.get("team", ""),
"team_id": auth.get("team_id", ""),
"url": auth.get("url", ""),
}
except SlackApiError as e:
err = e.response.get("error", str(e))
return {"status": "error", "message": err}
except Exception as e:
return {"status": "error", "message": str(e)}
async def probe_app_token(token: str) -> dict:
if not token.startswith("xapp-"):
return {"status": "error", "message": "Token must start with 'xapp-'"}
handler = SocketModeClient(app_token=token)
connect_task = asyncio.create_task(handler.connect_async())
try:
try:
await asyncio.wait_for(connect_task, timeout=5.0)
except TimeoutError:
connect_task.cancel()
return {"status": "ok", "message": "Socket Mode connection timed out (expected without full setup)"}
except Exception:
pass
finally:
if not connect_task.done():
connect_task.cancel()
try:
handler.disconnect()
except Exception:
pass
return {"status": "ok", "message": "Socket Mode handshake completed"}