sidehub_bot
v1.0.3
Published
A Telegram bot framework for building multi-chain applications
Maintainers
Readme
SideHub_bot
Overview
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.
Features
- 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
.envfile.
Prerequisites
- Node.js (v14 or later)
- npm or yarn
- A running MongoDB instance
- A running Redis instance
- Valid credentials for Moralis (if used)
Setup Instructions
Clone the repository:
git clone https://github.com/AmanUpadhyay1609/TG_SIDEHUB.git cd SideHub_botInstall dependencies:
npm installCreate a
.envfile 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 urlNote: Ensure that
HTTP_SERVER_PORTandHTTP_SERVER_PORT_2are 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:
How It Works
The project instantiates two
BotApplicationinstances using different configuration objects (configandconfig_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) inBotApplicationensures that Moralis is only initialized once across all instances. moralisInitPromisehandles concurrent calls to Moralis initialization by sharing the promise.
- A shared static property (
Bot Modes:
- The bots support both
pollingandwebhookmodes, configurable via environment variables.
- The bots support both
Adding Additional Bot Instances
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.envfile (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); });
Troubleshooting
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
.envfile.
- 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
Contributing
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.
License
This project is licensed under the MIT License.
