This project demonstrates running multiple bot instances concurrently in a single Node.js application. Each bot instance is configurable via environment variables and runs its own HTTP server while sharing common resources such as the Moralis connection.
- Run two (or more) bot instances concurrently.
- Shared Moralis connection using static properties to prevent duplicate initializations.
- Each bot has its own HTTP server configuration.
- Configurable via a single
.env
file.
- Node.js (v14 or later)
- npm or yarn
- A running MongoDB instance
- A running Redis instance
- Valid credentials for Moralis (if used)
-
Clone the repository:
git clone https://github.com/AmanUpadhyay1609/TG_SIDEHUB.git cd SideHub_bot
-
Install dependencies:
npm install
-
Create a
.env
file in the project root with the following variables:# Bot 1 Configuration BOT_TOKEN=YOUR_BOT_TOKEN BOT_SERVER_HOST=YOUR_BOT_SERVER_HOST BOT_SECRET=your_secret BOT_MODE=polling # or "webhook" BOT_ALLOWED_UPDATES=message,callback_query HTTP_SERVER_HOST=localhost HTTP_SERVER_PORT=3003 MORALIS_API_KEY=YOUR_MORALIS_API_KEY MONGO_URL=mongodb://localhost:27017/multichain-bot REDIS_URL=redis://localhost:6379 BOT_USERNAME=YOUR_BOT_USERNAME CHAIN_NAME=solana WEBHOOK_URL=YOUR_WEBHOOK_URL WEB3_BACKEND_URL='https://api.sidebot.xyz' #Sidebot official Backend url # Bot 2 Configuration BOT_TOKEN_2=YOUR_BOT_TOKEN_2 BOT_SERVER_HOST_2=YOUR_BOT_SERVER_HOST_2 BOT_SECRET_2=your_secret_2 BOT_MODE_2=polling # or "webhook" BOT_ALLOWED_UPDATES=message,callback_query HTTP_SERVER_HOST_2=localhost HTTP_SERVER_PORT_2=3003 # Ensure this port is different from Bot 1 if needed MORALIS_API_KEY_2=YOUR_SECOND_MORALIS_API_KEY MONGO_URL_2=YOUR_MONGO_URL # Optional: can be the same as Bot 1 REDIS_URL_2=YOUR_REDIS_URL # Optional: can be the same as Bot 1 BOT_USERNAME_2=YOUR_BOT_USERNAME_2 CHAIN_NAME_2=multichain WEBHOOK_URL_2=YOUR_WEBHOOK_URL_2 WEB3_BACKEND_URL_2='https://api.sidebot.xyz' #Sidebot official Backend url
Note: Ensure that
HTTP_SERVER_PORT
andHTTP_SERVER_PORT_2
are unique if both bots start their own HTTP server. -
Build and run the project:
- To compile the TypeScript code:
npx tsc
- To run the compiled output:
node dist/index.js
- Alternatively, run directly with ts-node:
npx ts-node src/index.ts
- To compile the TypeScript code:
-
The project instantiates two
BotApplication
instances using different configuration objects (config
andconfig_2
). -
HTTP Server Configuration:
- Each bot instance creates its own HTTP server which listens on the configured port.
-
Moralis Connection:
- A shared static property (
isMoralisConnected
) inBotApplication
ensures that Moralis is only initialized once across all instances. -
moralisInitPromise
handles concurrent calls to Moralis initialization by sharing the promise.
- A shared static property (
-
Bot Modes:
- The bots support both
polling
andwebhook
modes, configurable via environment variables.
- The bots support both
To add another bot instance (e.g., Bot 3):
-
Create a new configuration object (e.g.,
config_3
) in the code and add corresponding environment variables in your.env
file (e.g.,BOT_TOKEN_3
,HTTP_SERVER_PORT_3
, etc.). -
Instantiate the new bot instance:
const botApp3 = new BotApplication(config_3); botApp3.start().catch(error => { console.error("Fatal error:", error); process.exit(1); });
-
Port Conflicts:
- Make sure each bot instance is configured to use a unique
HTTP_SERVER_PORT
.
- Make sure each bot instance is configured to use a unique
-
Environment Variables:
- Verify that all required variables are properly set in the
.env
file.
- Verify that all required variables are properly set in the
-
Moralis Issues:
- If you encounter errors related to Moralis, ensure you have a valid API key and that only one connection is established via the shared static properties in
BotApplication
.
- If you encounter errors related to Moralis, ensure you have a valid API key and that only one connection is established via the shared static properties in
Feel free to fork the repository and submit pull requests for improvements or bug fixes. If you encounter issues, please open an issue on GitHub.
This project is licensed under the MIT License.