A feature-rich Telegram bot with:
- MongoDB Database integration.
- Force Subscribe for restricted access.
- Broadcast Feature for mass messaging.
- Detailed Logs for user activities and join requests.
/start: Greet users and display bot usage.- Join request approval with logging.
/stats: Show user and group stats.- Force Subscribe for restricted access.
- Log details when a user starts the bot and when join requests are accepted.
Before installing, make sure you have the following:
- Python 3.7 or higher
- MongoDB instance (you can use MongoDB Atlas or any MongoDB server)
-
Clone the repository:
git clone https://github.com/yourusername/mn-bot.git cd mn-bot -
Create and activate a virtual environment:
python -m venv venv source venv/bin/activate # On Windows use `venv\Scripts\activate`
-
Install dependencies:
pip install -r requirements.txt
-
Set up your
.envfile:Copy the
.env.examplefile to.envand fill in your details:cp .env.example .env
Update
.envwith your own values for:BOT_TOKEN: The token you got from @BotFather on Telegram.API_IDandAPI_HASH: Your Telegram API credentials (get them from here).LOG_CHANNEL_ID: The ID of the log channel for logging user activity.JOIN_REQUEST_LOG_CHANNEL_ID: The ID of the channel where join request approvals are logged.MONGO_URI: Your MongoDB connection string (MongoDB Atlas or a local instance).
Example
.envfile:BOT_TOKEN=your_bot_token API_ID=your_api_id API_HASH=your_api_hash LOG_CHANNEL_ID=-1001234567890 JOIN_REQUEST_LOG_CHANNEL_ID=-1009876543210 MONGO_URI=mongodb+srv://username:password@cluster.mongodb.net/mn-bot?retryWrites=true&w=majority
-
Run the bot:
python app.py
You can deploy this bot on any cloud platform like:
- Heroku
- Render
- Koyeb
-
Create a new Heroku application.
-
Add your MongoDB URI to Heroku environment variables:
- Go to your Heroku app’s settings.
- Add the
MONGO_URIvariable. - Set your bot token, API ID, and API Hash as environment variables in the same way.
-
Push your repository to Heroku:
git remote add heroku https://git.heroku.com/your-heroku-app.git git push heroku main
-
Scale your dynos:
heroku ps:scale web=1
The bot should now be running on Heroku.
Once the bot is running, you can interact with it using the following commands:
/start: Greet users and display bot usage./stats: Displays statistics about the bot's usage (number of users, groups, and channels).
When a user interacts with the bot, it will check if they are subscribed to the required channels. If they are not, the bot will send an invite link to the required channel(s).
The bot uses MongoDB to store user and chat data. It tracks:
- User Details: User ID, name, username, and interaction data.
- Chat Details: Information about channels or groups where the bot is active.
The bot sends logs to two separate channels:
- User Activity Log: When a user starts the bot, their details are logged.
- Join Request Log: When a join request is approved, the details of the user and the chat are logged.
- app.py: Main file where the bot runs and handles commands and events.
- config.py: Stores configuration and credentials.
- utils/db.py: MongoDB database handler.
- utils/helpers.py: Helper functions for logging, greeting, and managing users.
- .env: Stores sensitive information like tokens and database URI.
This project is licensed under the MIT License - see the LICENSE file for details.