Skip to content

Create Telegram Bot with this friendly python boilerplate using pyrogram framework.

License

Notifications You must be signed in to change notification settings

sanjit-sinha/TelegramBot-Boilerplate

Repository files navigation

Telegram Bot Boilerplate
[ Pyrogam version ]


About this repository?

This repository serves as a template for creating a new Telegram bot using Python. It aims to save time for those starting new projects from scratch, and provides a helpful resource for those who are new to the Telegram API and want to learn and understand basic file structure. The repository includes a Pluggable boilerplate that is easy to use and customize for your own project. You can use the repository as a template or fork it to begin a new project. The following are some of the features of this repository:

  • Fully asynchronous code: The code in this repository makes use of libraries such as httpx, aiofiles, pyrogram and mongodb motor to enable fully asynchronous execution. This means that the code can perform multiple tasks concurrently, improving the performance and efficiency of the bot.

  • Pre-built features: This repository contains a range of pre-built plugins, functions, and decorators to assist you in getting started. Some examples include commands for broadcasting, pasting, checking spam counter, evaluating/executing code, checking database and server stats, and more..

  • Pluggable plugins: The code in this repository is designed to be modular and extensible, allowing you to easily add new functionality to the bot by creating and integrating plugins.

  • Ratelimiting: To prevent FloodWait errors, the code in this repository includes ratelimiting functionality that uses the leaky bucket algorithm and the pyrate_limiter library to limit the number of requests that can be made by users in a given time period.

  • MongoDB support: The code in this repository includes support for storing data in a MongoDB database, allowing you to easily keep track of users and channels associated with the bot."


More information and links

This bot uses the Pyrogram framework, which is based on the MTProto protocol, to communicate with the Telegram API. .

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 Telegram Bot | Telethon | List of libraries and frameworks using various type of languages.

Join Discussion Group if you have any suggestion or bugs to discuss.


Screenshots


Bot Deployment and Usage

( VPS or Local hosting )

( checkout no database branch if you don't want to use database in your bot. )

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 Repository 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 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.
  • To get guide for getting MONGO_URI click here

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.
    /ping - Ping the telegram api server
    /paste - Paste the text in katb.in

  • Sudo User Commands

    /speedtest - Check the internet speed of bot server.
    /serverstats - Get the stats of bot server.
    /stats - Alias command for serverstats.
    /dbstats - Get the stats of bot database.

  • Developer Commands

    /shell - To run the terminal commands via bot.
    /py - 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.
    /broadcast - broadcast the message to bot users and chat.


DirectoryLayout


├── 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 database related stuff for bigger projects.)
│   │   ├── __init__.py
│   │   ├── database.py              (contain functions related to handle database operations all over the bor)
│   │   └── MongoDb.py               (Contain a MongoDB class to handle CRUD operations on MongoDB collection )
│   │  
│   ├── helpers                       ( Contain all the file wich is imported and  used all over the code. It act as backbone of code.)
│   │   ├── __init__.py
│   │   ├── filters.py 
│   │   ├── decorators.py            ( Contain all the python decorators)
│   │   ├── ratelimiter.py           (Contain RateLimiter class that handle ratelimiting part of the bot.)
│   │   └── 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
│   │   │   ├── speedtest.py
│   │   │   ├── dbstats.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.

Basic structure for building your own plugin.

from TelegramBot.helpers.decorators import ratelimiter
from pyrogram import Client, filters
from pyrogram.types import Message


@Client.on_message(filters.command(["hello", "hi"]))
@ratelimiter
async def hello(client: Client, message: Message):
    """
    simple plugin to demonstrate in readme.
    """   	
    return await message.reply_text("world")	    

Credits and Contibution

Codes and structure of this bot is heavily inspired by open source projects like YukkiMusicbot | Userge | TG-FileStreamBot etc..

Special Thanks to
Dan for creating Pyrogram.
Starry for guiding and acutebot repository.
Annihilator for helping me out with pyrogram stuff.

Any type of suggestions, pointing out bug or contribution is highly appreciated. :)


Copyright and License