Skip to main content
The Bot Gateway is a WebSocket endpoint that provides real-time events for bot applications. Bots authenticate with their bot token and receive events from channels they’ve been added to as members.

Getting Started

1

Create a Bot

Create a bot using any of these methods:
  • Web UI: Server Settings → Bots → Create Bot
  • CLI: meepachat bots create --username mybot --server <serverID>
  • API: POST /api/bots with a human user’s Bearer token
All three return a bot token — save it immediately, it’s only shown once.
2

Add Bot to a Server

The bot is automatically added to the server specified during creation. To add it to additional servers:
  • Web UI: Server Settings → Bots → Add Existing Bot
  • CLI: meepachat bots add-to-server <botID> --server <serverID>
  • API: POST /api/servers/{serverID}/bots/{botID}
3

Connect to the Gateway

Open a WebSocket connection to /api/bot-gateway using the bot token. The bot receives a ready event with its current state.
4

Handle Events

Listen for real-time events (message.created, reaction.sync, etc.) and respond via the REST API using the bot token.

Connection

Endpoint: GET /api/bot-gateway Authenticate using the Authorization header:
Bot tokens follow the format <bot_id>.<secret> and are obtained when creating a bot via POST /api/bots.
Passing the token as a ?token= query parameter works but is deprecated. Use the Authorization: Bot <token> header instead.

Connection Parameters

Ready Event

Immediately after connecting, the bot receives a ready event containing:
  • Bot user object (id, username, avatar, etc.)
  • All servers the bot is a member of, with channels the bot has been added to
  • All DM channels the bot is a participant in
This is the only time the bot receives this initialization data. Store it for the duration of the session.

Client-to-Server Events

ping

Heartbeat. The server responds with a pong event.

subscribe

Subscribe to additional channels at the WebSocket level. Use this for channels added to the bot’s membership after the initial connection, or for DM channels opened after connect. This only adds the channel to the in-memory subscription — the bot must already be a member of the channel to receive events via the REST API.

typing

Broadcast a typing indicator to a channel. The bot must be subscribed to the channel.
Other clients in the channel will receive a typing event with the bot’s user ID.

Server-to-Client Events

Bots receive the same real-time events as human WebSocket clients. All events follow this structure:
See the WebSocket page for detailed payload documentation for each event type.

Sending Messages

Bots send messages via the standard REST API using their bot token:
The bot will receive its own message.created event over the gateway.

Full Example Bot (Node.js)

Reconnection Strategy

Bots should implement reconnection with exponential backoff:
  1. Wait 1 second before the first retry
  2. Double the wait time on each failure (max 30 seconds)
  3. Reset the wait time after a successful connection
  4. After reconnecting, the server sends a new ready event with current state
  5. Use the ready event to resync channel subscriptions and local state

Lifecycle Events

When a bot is removed from a server via DELETE /api/servers/{serverID}/bots/{botID}, the server forcibly closes the bot’s gateway connection. The bot should reconnect and will receive an updated ready event reflecting the new server list.

Rate Limits

Bots share the same rate limits as human users:
The WebSocket gateway connection itself is not rate limited. Once connected, real-time events bypass the HTTP rate limiter. Only REST API calls (sending messages, etc.) count against the rate limit.

Security Best Practices

Bot tokens grant full access to all servers the bot belongs to. Treat them like passwords.
  • Keep bot tokens secret: Never commit tokens to source control or expose them in client-side code.
  • Rotate compromised tokens: If a token is leaked, regenerate it immediately via POST /api/bots/{botID}/regenerate-token.
  • Validate event data: Do not trust message content blindly. Sanitize before processing or displaying.
  • Log security events: Track unusual patterns such as spam or unauthorized access attempts.
  • Use environment variables: Store the bot token in an environment variable, not in source code.