Concepts
Understanding the architecture will help you build better bots. Here’s how the pieces fit together.
Layers
Section titled “Layers”The SDK has four layers, from high-level to low-level:
| Layer | What it does |
|---|---|
| Bot | Developer-facing API with @event and @command() decorators |
| Commands | Command parsing, argument extraction, Context object |
| REST Client | HTTP requests to the Syncopate API (send messages, manage channels) |
| SSE Gateway | Real-time event stream via Server-Sent Events |
You’ll mostly interact with the Bot layer. The other layers work behind the scenes.
Connection Lifecycle
Section titled “Connection Lifecycle”1. bot.run() called2. REST: POST /api/auth/login (bot token → session)3. REST: GET /api/workspaces (discover workspace + channels)4. SSE: Connect to /api/events/stream5. SSE: Receive events → dispatch to handlers6. (Loop until disconnected or stopped)Event Flow
Section titled “Event Flow”When a message arrives:
- The SSE Gateway receives a
message.createdevent - The Bot builds a
Messagemodel from the payload - It resolves the
ChannelandUserreferences - It dispatches to your
on_message/messageCreatehandler - If the message starts with the command prefix, it also dispatches to the matching command handler
Authentication
Section titled “Authentication”Bots authenticate with a bot token (prefixed bot_). The SDK exchanges this token for a session on startup. You never need to manage sessions manually.
Channel Visibility
Section titled “Channel Visibility”Bots can only see channels in their workspace. When the bot connects, it fetches the channel list and caches it. New channels are added to the cache via channel.created events.