feat(gateway): route Slack events through external chat

This commit is contained in:
Anish Sarkar 2026-06-01 12:36:53 +05:30
parent 61a3586caf
commit f305a2e67d
4 changed files with 136 additions and 15 deletions

View file

@ -6,7 +6,7 @@ from collections.abc import Callable
from dataclasses import dataclass
from app.db import ExternalChatAccount, ExternalChatAccountMode, ExternalChatPlatform
from app.gateway.accounts import account_token
from app.gateway.accounts import account_token, slack_account_credentials
from app.gateway.base.adapter import BasePlatformAdapter, ParsedInboundEvent
from app.gateway.base.commands import BaseGatewayCommands
from app.gateway.base.translator import BaseStreamTranslator
@ -70,6 +70,23 @@ def _whatsapp_baileys_translator_factory(
)
def _slack_translator_factory(
adapter: BasePlatformAdapter,
event: ParsedInboundEvent,
) -> BaseStreamTranslator:
channel_id = event.metadata.get("channel_id")
thread_ts = event.metadata.get("thread_ts")
if not channel_id or not thread_ts:
raise RuntimeError("missing_slack_thread_metadata")
from app.gateway.slack.translator import SlackStreamTranslator
return SlackStreamTranslator(
adapter=adapter, # type: ignore[arg-type]
channel_id=channel_id,
thread_ts=thread_ts,
)
def resolve_platform_bundle(account: ExternalChatAccount) -> PlatformBundle:
if account.platform == ExternalChatPlatform.TELEGRAM:
token = account_token(account)
@ -108,4 +125,24 @@ def resolve_platform_bundle(account: ExternalChatAccount) -> PlatformBundle:
auto_bind_owner=True,
)
if account.platform == ExternalChatPlatform.SLACK:
from app.gateway.slack.adapter import SlackAdapter
from app.gateway.slack.commands import SlackGatewayCommands
credentials = slack_account_credentials(account)
bot_token = credentials.get("bot_token")
if not bot_token:
raise RuntimeError("missing_slack_bot_token")
cursor_state = account.cursor_state or {}
return PlatformBundle(
adapter=SlackAdapter(
bot_token,
bot_user_id=cursor_state.get("bot_user_id"),
),
translator_factory=_slack_translator_factory,
platform_label="slack",
commands=SlackGatewayCommands(),
auto_bind_owner=False,
)
raise RuntimeError(f"unsupported_gateway_platform:{account.platform.value}:{account.mode.value}")