We make a Telegram bot with the Admin panel and many other goodies
Hello everyone! Since this is my first article and I do not know what and how to do, I will write as I know.
Today I would like to tell you how easy it is to make your Telegram bot in the very popular Programming Language – Python. The article will be in two parts, in the first we will install Python and all the necessary components and write the simplest bot, in the second we will write the basic logic of the bot. If you are not a beginner, you can scroll down, as there will be a lot of information familiar to you.
I will not torment with a long introduction, let’s get started!
And so the first thing we need to do is draw up a plan for our bot. The plan will include: what functions the bot and everything will perform.
How our bot will look from the user’s side:
The user enters the bot and writes the command “/ start”
Goes to the main bot menu
User selects an action on the keyboard buttons
Fully functional boa does not need to be painted as it will take a very long time and in the future it is useless
Now let’s imagine that you will see you as the Admin and Owner of this bot:
You enter the bot by writing the command “/ start”
You get to the main menu of the Admin panel
Choose an action on the keyboard
This is where our bot plan ends, we proceed to the Python installation part.
We go to the site python.org
Click on the “Download” tab
Scroll to the bottom and download the latest version of Python (at the moment it is Python 3.10)
You will now begin installing the Python Installer. When it downloads open it and you should have a window like this:
You will have an inscription “Install Now”, I don’t have it since I already downloaded Python Be sure to check the box next to “Add Python 3.10 to PATH”
After installation, it will be possible to disable the length limitation
MAX_PATH
… Linux systems do not have these restrictions. If you ignore this point, you may face a compatibility problem in the future. Code written on Linux will not run on Windows.That’s it, you now have Python
Now let’s start installing, but already the library, not the YP
If pip is not installed
Download the file get-pip.py and save it on your computer.
Open a command prompt and navigate to the folder where you saved
get-pip.py
…At the command prompt, run the command:
python get-pip.py
orpython3 get-pip.py
…PIP installed 🎉!
Create a new folder where you will develop the bot
Go to the command line, go through it to the development folder and enter the command to install the aiogram library
pip install aiogram
And let’s finally get to the code:
First, let’s open the Python development environment
IDLE on Windows is located in the “Start“→”Python 3.10“→”IDLE“You can also quickly find it through”Search“near the menu”Start“by typing” IDLE “in the search field:
IDLE has the ability to fully work with files – view, edit, create new ones. To create a new file, select “File” -> “New File” (or Ctrl + N). A new window will open:
Save the file to the folder where we installed the library
Everything is ready to write code
The first thing we need to do is import the libraries.
import logging # эта библиотека идет вместе с python from aiogram import Bot, Dispatcher, executor, types # импортируем aiogram
Next, we declare several variables:
API_TOKEN = 'ТОКЕН' # Токен logginglogging.basicConfig(level=logging.INFO) # Initialize bot and dispatcher bot = Bot(token=API_TOKEN) dp = Dispatcher(bot)
Next, we write the first handler for the / start and / help commands
@dp.message_handler(commands=['start', 'help']) async def send_welcome(message: types.Message): await message.reply("Привет! Это простейший бот на aiogram") # отвечает на сообщение
Now we will process each message and send it back.
@dp.message_handler() async def echo(message: types.Message): await message.answer(message.text)
And at the very end we add two lines so that the bot would always work
if __name__ == '__main__':
executor.start_polling(dp, skip_updates=True)
That’s all, now it remains to get the bot token and insert it into the API_TOKEN variable
Getting a Token for a Bot
Open @BotFather and run it (Start / Start).
In the list of suggested commands, select:
/newbot - create a new bot
, click on this command, or enter it manually in the field for entering messages.You will be prompted to indicate the name of the bot, in the future the name of the bot can be changed. Enter a name in the message field.
Next, you will be asked to specify the name by which the bot will be available to users. Write the name using the Latin alphabet, numbers and underscore. Another important condition is that the name must end with “bot”. You can also capitalize “Bot” or “_bot” or “_Bot”.
After these actions, you will receive a message with the bot token
We insert it into the API_TOKEN variable
If you have followed all the steps that are described above, everything will work successfully for you and you are great, but if you have any problems, write to me on Telegram @ derkown
That’s all! Thanks for reading, Part 2 is coming soon. Again, my Telegram is happy to welcome you. Good luck and Goodbye!