Initialization
Platform handlers in Bot Studio are designed to distribute processes across multiple servers. Running all platform processes on a single server, such as handling Telegram bots, Discord bots, Slack bots, etc., could lead to server crashes due to resource overload. To address this, Bot Studio introduced platform handlers in version 10.
Platform handlers are distinct servers responsible for managing all bot-related processes. This includes handling events, executing processes, managing nodes, and more.
Initialization
WebSocket Client
When a handler starts, it first creates a WebSocket client and establishes a connection with the origin WebSocket server. The WebSocket protocol is chosen for its speed and reliability in data transfer between a client and server. This connection is crucial for:
- Verifying bot keys
- Saving process data to the databases on the origin server
- Activating or deactivating bots
A verification token, which changes after each update, is used by both the handler and the server to validate incoming requests and establish the connection. Once successfully connected, the handler is designated for that platform, and the origin server directs all relevant tasks to it.
If the connection to the origin server is lost, the client retries every 2.5 seconds to reconnect. Similarly, if the client connection is lost, the origin server retries every 2.5 seconds to send requests.
Running Bot Instances
The origin server activates all bots upon startup or activates specific bots when updates occur. It sends a request to the handler to activate the bot. To prevent server crashes from simultaneous activations, a queue system is used. The queue activates 50 bot instances at a time, waiting for available space before activating more bots.
This queue system is also used for deactivating bots. The duration of the queue depends on the platform's optimization. For example, Telegram bots start faster than Discord bots because Telegram uses webhooks while Discord uses WebSockets.
Creating HTTP Server for Handler
The handler requires an HTTP server to manage incoming verification requests and Webhook requests from developers to bots. The following middlewares are used for the handler's HTTP server:
- CORS: Handles cross-origin requests.
- GZIP Compression: Compresses request sizes.
- Response Time Header: Adds a header to each response indicating the processing time.
- Size Limitation: Limits the maximum request size to 250KB, consistent with the origin server's limit.
- Verification: This route is exclusively used by the server to verify bot keys. Requests not from the origin server receive a
403 Forbidden
status, indicating unauthorized access.
This is the process for initializing platform handlers, ensuring efficient and secure management of bot processes across multiple servers.