About this repository?
A friendly and easy to use boilerplate to create your own telegram bot using python and Pyrogram framwork. You can use this repository as a template or you can fork this repository to start your new project.
I hate to start a new project with scratch or an empty template and repeating same basic code over and over , often i miss the correct configuration, structures , readme, instruction or i need to go through sample of codes over and over. You can Use this repository as a template to solve this problem.
There is one more scenario where a new user want to create a telegram bot but dont have any prior experience with telegram api or proper structuring of file. This resporitory can be great example to learn and get the basic idea of Telegram API.
(PS: I tried to keep the code as clean and readable as possible but still any suggestion(s) and contibution(s) will be highly appreciated :)
More information and links
This Telegram Bot is written in python using Pyrogram framework.
Pyrogram is a modern, elegant , much faster and asynchronous MTProto API (MTproto vs botapi) framework.
Pyrogram Documentation | Pyrogram Support Group | Telegram API Documentation
Some other libraries and Framework: Python Tlegram Bot | Telethon | List of llibraries and frameworks uding various type of languages.
Join my Discussion Group if you have any suggestion or bugs to discuss.
Bot Deployment and Usage
( VPS or Local hosting )
Upgrading, Updating and setting up required packages in Server.
sudo apt-get update && sudo apt-get upgrade -y
sudo apt install python3-pip -y
sudo pip3 install -U pip
Cloning Github Respository and Starting the Bot in Server.
git clone https://github.com/sanjit-sinha/Telegram-Bot-Boilerplate && cd Telegram-Bot-Boilerplate
pip3 install -U -r requirements.txt
Now edit the config vars by typing nano config.env
and save it by pressing ctrl+o and ctrl+x.
Setting up config variables files (config.env).
- Get your API_ID and API_HASH from Telegram.org, BOT_TOKEN and BOT_USERNAME from Botfather. You can get user ids for sudo users and owner from MissRoseBot by just using /info command and copying ID value from result.
now you can start the bot by simply typing bash start
or python3 -m TelegramBot
The bot will stop working once you logout from the server. You can run the bot 24*7 in the server by using screen or tmux.
sudo apt install tmux -y
tmux && bash start
Now the bot will run 24*7 even if you logout from the server. Click here to know about tmux and screen advance commands.
Basic Bot Commands and it's usage
-
Users Commands
/start - To get the start message.
/help - Alias command for start.
/alive - To check if bot is alive or not.
/ping - Ping the telegram api server.
-
Sudo User Commands
/speedtest: Check the internet speed of bot server.
/serverstats: Get the stats of server.
/stats: Alias command for serverstats.
-
Developer Commands
/shell: To run the terminal commands via bot.
/exec: To run the python commands via bot.
/update: To update the bot to latest commit from repository.
/restart: Restart the bot.
/log: To get the log file of bot.
├── Dockerfile
├── LICENSE
├── README.md
├── config.env ( For storing all the environment variables)
├── requirements.txt ( For keeping all the library name wich project is using)
├── TelegramBot
│ │
│ ├── __init__.py ( Initializing the bot from here.)
│ ├── __main__.py ( Starting the bot from here.)
│ ├── config.py ( Importing and storing all envireonment variables from config.env)
│ ├── logging.py ( Help in logging and get log file)
│ │
│ ├── assets ( An assets folder to keep all type of assets like thumbnail, font, constants, etc.)
│ │ └── __init__.py
│ │ ├── font.ttf
│ │ └── template.png
│ │
│ ├── database ( Sperate folder to manage databse related stuff for bigger projects.)
│ │ ├── __init__.py
│ │ └── db.py
│ │
│ ├── helpers ( Contain all the file wich is imported and used all over the code. It act as backbone of code. )
│ │ ├── __init__.py
│ │ ├── decorators.py ( Contain all the python decorators)
│ │ ├── errors.py ( Contain custom errors wich can be raised in code when it is necessary. )
│ │ ├── filters.py
│ │ └── functions.py ( Contain all the functions wich is used all over the code. )
│ │
│ ├── plugins ( plugins folder contain all the plugins commands via wich user interact)
│ │ │
│ │ ├── __init__.py
│ │
│ │ ├── developer
│ │ │ ├── __init__.py
│ │ │ ├── terminal.py
│ │ │ └── updater.py
│ │ │
│ │ ├── sudo
│ │ │ ├── __init__.py
│ │ │ └── serverstats.py
│ │ │
│ │ └── users
│ │ ├── __init__.py
│ │ ├── alive.py
│ │ └── start.py
│ │
│ └── version.py
└── start ( A start file containing bash script to start the bot using bash start)
Some important articles and Links wich will help you to understand the code better.
- What is __init__.py│ ( YouTube video )
- What is __name__ = "__main__" in python?
- About python logging
- Python concurrecny and asyncio
- What is Environment Variables (.env files) ?
- About Python decorator
- What is __main__.py
- What is requirements.txt and why should we use it?
- What iS Dockerfile?
- what is License in a repository? │choosealicense.
- What is README.md? │ ( writing format for Github readme ) │ ( website wich will help you in writing readme ) │website to get png images wich can be used in making readme:- shields.io│ flaticon.com
Credits and Contibution
Codes and structure of this bot is heavily inspired by open source projects like YukkiMusicbot | Userge | TG-FileStreamBotetc.
special thanks to Dan for creating Pyrogram.
Any type of suggestions, pointing out bug or contribution is highly appreciated. :)
Copyright and License
- copyright (C) 2022 by Sanjit sinha
- Licensed under the terms of the The MIT License