sidehub_bot
TypeScript icon, indicating that this package has built-in type declarations

1.0.3 • Public • Published

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 .env file.

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

  1. Clone the repository:

    git clone https://github.com/AmanUpadhyay1609/TG_SIDEHUB.git
    cd SideHub_bot
  2. Install dependencies:

    npm install
  3. 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 and HTTP_SERVER_PORT_2 are unique if both bots start their own HTTP server.

  4. 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

How It Works

  • The project instantiates two BotApplication instances using different configuration objects (config and config_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) in BotApplication ensures that Moralis is only initialized once across all instances.
    • moralisInitPromise handles concurrent calls to Moralis initialization by sharing the promise.
  • Bot Modes:

    • The bots support both polling and webhook modes, configurable via environment variables.

Adding Additional Bot Instances

To add another bot instance (e.g., Bot 3):

  1. 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.).

  2. 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.
  • Environment Variables:

    • Verify that all required variables are properly set in the .env file.
  • 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.

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.

Package Sidebar

Install

npm i sidehub_bot

Weekly Downloads

0

Version

1.0.3

License

ISC

Unpacked Size

631 kB

Total Files

342

Last publish

Collaborators

  • wstf_amanupadhyay1609