feat(gateway): add Slack gateway configuration

This commit is contained in:
Anish Sarkar 2026-06-01 12:36:07 +05:30
parent b0b0f3517b
commit 5b71685dad
3 changed files with 38 additions and 1 deletions

View file

@ -2,6 +2,8 @@
from __future__ import annotations
import json
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
@ -25,6 +27,19 @@ def account_token(account: ExternalChatAccount) -> str | None:
)
def slack_account_credentials(account: ExternalChatAccount) -> dict:
"""Decrypt Slack gateway credentials stored as encrypted JSON."""
if not account.encrypted_credentials:
return {}
raw = TokenEncryption(config.SECRET_KEY or "").decrypt_token(account.encrypted_credentials)
try:
data = json.loads(raw)
except json.JSONDecodeError:
# Backward-compatible fallback if a token string was stored directly.
return {"bot_token": raw}
return data if isinstance(data, dict) else {}
async def get_or_create_system_telegram_account(
session: AsyncSession,
) -> ExternalChatAccount:
@ -78,3 +93,18 @@ async def get_or_create_system_whatsapp_account(
await session.flush()
return account
async def get_slack_account_by_team(
session: AsyncSession,
*,
team_id: str,
) -> ExternalChatAccount | None:
result = await session.execute(
select(ExternalChatAccount).where(
ExternalChatAccount.platform == ExternalChatPlatform.SLACK,
ExternalChatAccount.is_system_account.is_(True),
ExternalChatAccount.cursor_state["team_id"].astext == team_id,
)
)
return result.scalars().first()