Initial set up for telegram bot

Clock
14.01.2025
An eye
46
Hearts
0
Connected dots
0
Connected dots
0
Connected dots
0

Introduction

This is a basic article, you will see how to get a token for a bot and how it can be used. Also, we will talk about the various possible bot settings and what they can be used for.

Getting a token

To get a token, you need to contact the father of all telegram bots, BotFather. Enter the command /newbot and do what he asks (that is, enter the name of the bot and its name with the prefix bot).
Something like this ...
He will reply to you with a link to a chat with a new bot and its token. Save the token in a separate file. I usually call such a file .token. The file will have only one line, instead of TOKENMOI insert your token:
BOT_TOKEN=TOKENMOI
An example:
BOT_TOKEN=1111111111:AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
Ideally, it would be better to save it in an environment variable. After all, to get to the virtual environment variable, you will first need to get root rights on the machine and then activate the virtual environment.

Setting up bot

In the chat with BotFather, we can manage our bots, applications, and games. But we are only interested in bots, you can do the following:
  1. Create a bot, /newbot
  2. View created bots, /mybots
  3. Delete bots, /deletebot
  4. Edit bots, /editbot (add descriptions, change the name, edit commands...)
An example ...
In particular, we can activate inline mode for the bot. That is, a mode in which you can contact the bot from any chat, and it will return some answer to the current chat.

Activating Inline mode for a bot

To do this, in a chat with the same BotFather:
  1. enter the /mybots command
  2. select your bot
  3. select Bot settings
  4. click on Inline mode
  5. Turn on (enable)
From 1st to 3d step
4th step
5th step

Create a virtual environment and install python packages

Now it's time to create a virtual environment and install packages. Create a directory where all the files from the bots will be. Move the file with the bot token there (.token file), then create a virtual environment:
python -m venv .venv
Activate it and install the necessary packages:
.\.venv\Scripts\activate
pip install aiogram pandas babel
Packages that I install by default:
  1. aiogram: de facto the only package that we need. It will allow you to communicate with the telegram API using only asynchronous calls.
  2. pandas: for working with documents, not required
  3. babel: for translations, not required, but highly recommended.

Conclusion

All my bots have one beginning, and here I described it. Next you need to write the basic functionality for the bot, handlers, routers, and connect to databases or create your own. It will be different for different bots, but the beginning is the same for all, and it is here. ж)

Comments

(0)
captcha
Send
It's empty now. Be the first (o゚v゚)ノ