Skip to content

feat: Discord and Slack channel adapters (#146) - #320

Open
imincheol wants to merge 1 commit into
regnull:mainfrom
imincheol:feat/discord-slack-channels
Open

imincheol wants to merge 1 commit into
regnull:mainfrom
imincheol:feat/discord-slack-channels

Conversation

@imincheol

@imincheol imincheol commented Nov 24, 2026 •

Copy link
Copy Markdown

What

Discord and Slack DM adapters, built as parallel modules to the existing Telegram one (issue #146). A user DMs the bot on either platform, the message reaches the same thread/actor pipeline (external_ref = "<channel>:{id}" -> post_message), and bot replies go back out through an EventBus delivery listener — the same event flow, session handling and config layout as Telegram.

  • Discord (backend/openbot/channels/discord.py): gateway websocket client (identify + heartbeat + MESSAGE_CREATE DISPATCH, Message Content intent), DM-only, bot echoes dropped; reconnect with exponential back-off. DISCORD_BOT_TOKEN env var or the settings page (secret, masked). Gateway-only, so the API router exposes status.
  • Slack (backend/openbot/channels/slack.py): Socket Mode by default (no public URL needed) via SLACK_APP_TOKEN (xapp-, connections:write) + SLACK_BOT_TOKEN (xoxb-); Events API webhook transport with request-signature validation (SLACK_SIGNING_SECRET) as the alternative. Edits/thread re-announcements (ts != event_ts) and bot/subtype echoes are dropped, so the bot never answers its own answer twice.
  • Commands /new, /start, /help work on both, like Telegram.
  • Settings: new tunables (Discord/Slack groups) with token-format checks (xoxb-/xapp-), encrypted at rest and masked in the API exactly like telegram_bot_token.
  • README configuration table documents every key; roadmap text updated.

Why not reuse the Telegram transport names

Telegram's ids are ints; Discord snowflakes and Slack channel ids are strings, so each adapter keeps its own _parse_channel_id_from_ref rather than a shared abstraction with three special cases.

Tests

66 new tests (backend/tests/test_discord.py, test_slack.py): parsing, thread mapping, /new isolation, fake gateway/Socket-Mode frames -> handler called -> outbound send asserted, EventBus delivery-listener round-trips, signature validation incl. replay rejection, settings API round-trip with masking, status endpoints.

Local evidence (Windows, Python 3.12, uv):

  • uv run ruff check . -> All checks passed (exit 0)
  • uv run pytest -q -> 938 passed, 7 skipped, 5 failed — all 5 failures reproduce on unmodified main in this Windows environment (cp949 codec / CRLF assertions; e.g. test_search_code_returns_matching_lines..., test_read_file_error_outputs_honour_small_cap) and are unrelated to this change.
  • The one pre-existing settings-group assertion was updated for the two new groups (Telegram, Discord, Slack).

Live verification on a real deployment (this machine, WSL Ubuntu-24.04 + Docker)

The branch was checked out and run with the repo's own compose.yaml (docker compose up --build, backend container healthy on :8000), then exercised at three levels:

  1. Process-level, real HTTP: with fake-but-format-valid tokens (DISCORD_BOT_TOKEN, SLACK_BOT_TOKEN=xoxb-…, SLACK_APP_TOKEN=xapp-…, SLACK_SIGNING_SECRET), GET /api/v1/channels/discord/status -> {"configured":true,"gateway_running":true} and GET /api/v1/channels/slack/status -> {"configured":true,"socket_mode":true,"signing_secret":true,"socket_running":true}. The gateway/socket loops connect, fail auth against the real platform APIs with the fake tokens, and reconnect with bounded exponential back-off (observed in container logs: 8s → 16s → … → capped at 60s) without crashing the server — matching the Telegram adapter's resilience.
  2. Signed webhook round-trip over HTTP: POST /api/v1/channels/slack/events with HMAC-SHA256 v0: signatures computed from the signing secret — bad signature -> 403; url_verification challenge echoed; signed /help command event -> 200 and the reply delivered via chat.postMessage path; signed regular-message event -> 200, thread persisted (external_ref = slack:D0LIVE0002) with the message recorded in the DB (sender_kind=external).
  3. Full pipeline round-trip inside the container image (fresh DB): demo bots seeded, the real agent loop run against a local mock OpenAI-compatible SSE server, platform sends stubbed. Slack: inbound DM event -> thread -> inbox item -> run completed -> bot reply delivered outbound (hello from the bot). Discord: gateway MESSAGE_CREATE frame -> same path -> outbound. Both print PASS. The identical stubbed round-trip for Telegram was run in the same image as a control and also passes, confirming the new adapters sit on the exact same pipeline.

Live Discord round-trip with a real bot token (owner decision: reuse an existing bot, no new app)

Re-run on this machine (WSL Ubuntu-24.04, Docker, fresh pr320-verify container from this branch's compose.yaml, LLM via a local OpenAI-compatible route):

  • Real token, real platform: an existing Discord bot token was reused — Hermes 'Hem 5090' (/users/@me -> Hem 5090, id 1574249897622905004). GET /channels/discord/status -> {"configured":true,"transport":"gateway","gateway_running":true} with the real token: identify accepted, READY received, connection stable (no auth failures).
  • Gateway reception proven: a frame spy on the production DiscordGatewayClient._handle_frame inside the container logged live MESSAGE_CREATE dispatches for messages sent through the Discord REST API; parse_discord_message correctly returns None for them — they are bot-authored, and the reply-loop guard drops them by design.
  • Pipeline round-trip PASS: a human-authored DM (owner id 271881691650326534) entered the production path (process_discord_message — the exact call the gateway makes after parsing): thread created (external_ref = discord:1574485717478940682), run completed, reply stored (sender_kind=bot), and DiscordDeliveryListener delivered it via REST into the real DM channel. Discord message history shows it: Greetings, my friend — may your path be as clear as the light of Eärendil. (msg id 1575005241890439250).
  • Token-sharing caution observed: the 'Op 5090' bot token is shared with a running OpenClaw gateway; with two gateway consumers, MESSAGE_CREATE events were not delivered to the new container (OpenClaw's consumer received them), so verification switched to 'Hem 5090' as planned. One gateway consumer per bot token is required for this transport.
  • Inbound-author note: REST probes sent as the bot are dropped by the adapter's bot-author guard (by design, reply-loop protection); a full human-inbound E2E additionally needs the owner to DM the bot from their account — the pipeline side of that exact flow is what the PASS above covers end-to-end.

Remaining credentials (for Slack live E2E)

  • Slack path re-verified on this deployment with fake-but-format-valid tokens (xoxb-/xapp- + signing secret): signed webhook round-trip passes again — bad signature -> 403, url_verification echoed, /help -> 200 via chat.postMessage path, regular message -> 200; and a full pipeline round-trip inside the live container passed (external_ref = slack:D0PR320SLACK1, run completed, reply stored, delivery listener attempted real chat.postMessage which Slack rejected with invalid_auth — exactly the fake-token boundary).
  • What a real Slack round-trip still needs: one new lucablaq workspace app providing SLACK_BOT_TOKEN (xoxb-) + SLACK_APP_TOKEN (xapp-, connections:write) + SLACK_SIGNING_SECRET. Blocked by the free plan's 10-app limit — an unused app must be deleted first (owner will clean up later).
  • Discord needs no new credential: existing bot tokens work; only a DM from the owner account completes the human-side E2E.

Not in this PR

Group channels, approval cards, and media attachments stay out of scope.

DMs on Discord and Slack reach the same thread/actor pipeline Telegram uses,
and bot replies go back out through an EventBus delivery listener.

Discord: gateway websocket (identify + heartbeat + MESSAGE_CREATE), DM-only,
bot echoes dropped; DISCORD_BOT_TOKEN env or settings; status endpoint.
Slack: Socket Mode by default (no public URL), Events API webhook transport
with request-signature validation as the alternative; SLACK_BOT_TOKEN +
SLACK_APP_TOKEN (+ SLACK_SIGNING_SECRET for webhook mode).

Both follow the telegram adapter's layout exactly: parse -> get-or-create
external actor -> get-or-create thread (external_ref '<channel>:{id}') ->
post_message, /new //start //help commands, and a DeliveryListener that
forwards bot messages on threads whose external_ref belongs to the channel.

Closes regnull#146.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants