This is a template for a Hydrogram bot using the Hyrogram framework a Pyrogram Alternative. Follow the steps below to set up and run the bot. For better developer experience install this code snippet extenstion.
- Python 3.8 or higher
- MongoDB
- Telegram account
-
Clone the Repository Clone this repository to your local machine using:
git clone https://github.com/nuhmanpk/hydrogram-bot.git cd hydrogram-bot
-
Create a Virtual Environment Create a virtual environment to manage dependencies:
python3 -m venv venv
Activate the virtual environment:
On Windows:
venv\Scripts\activate
On macOS/Linux:
source venv/bin/activate
-
Install Dependencies Install the required Python packages:
pip install -r requirements.txt
-
Obtain Required Values You need to gather the following values to configure your bot:
- DATABASE_URL: The URL for your MongoDB database.
- DATABASE_NAME: The name of your MongoDB database.
- BOT_TOKEN: Your bot token from BotFather on Telegram.
- API_ID: Your API ID from my.telegram.org.
- API_HASH: Your API hash from my.telegram.org.
-
Set Environment Variables Create a .env file in the root of your project directory and add the following content:
DATABASE_URL=your_mongodb_url DATABASE_NAME=your_database_name BOT_TOKEN=your_bot_token API_ID=your_api_id API_HASH=your_api_hash
Replace the placeholders (your_mongodb_url, your_database_name, your_bot_token, your_api_id, your_api_hash) with your actual values.
-
Run the Bot Finally, run the bot with:
python3 main.py
-
Install PM2 Install PM2, a process manager for Node.js applications that can also be used to manage Python processes:
sudo npm install pm2 -g
-
Start the Bot with PM2
- Start your bot using PM2:
pm2 start python3 --name hydrogram-bot -- main.py
- To make PM2 start on system boot:
pm2 startup pm2 save
-
Monitor and Manage Your Bot You can manage your bot with PM2 using the following commands:
- List running processes:
pm2 list
- Stop the bot:
pm2 stop hydrogram-bot
- Restart the bot:
pm2 restart hydrogram-bot
- View logs:
pm2 logs hydrogram-bot
Happy Coding ... 🚀