How to connect Telegram and build your first bot or agent

Step by step: create a bot in BotFather, store the token safely, build the bot with AI, turn it into an agent and avoid leaks.


What a Telegram bot is

A bot is like a clerk at a counter: it receives messages and replies following the rules you set. A bot can simply answer commands, or become an agent: connect AI to it and it will understand free-form requests.


Step 1. Create the bot in BotFather

  • In Telegram find the official bot @BotFather.
  • Send the command /newbot.
  • Choose a name for the bot and a username (it must end in bot).
  • BotFather sends you a token - a long string. It is your bot's password: never show it to anyone or paste it into chats.

Step 2. Store the token safely

Put the token in a .env file in the project folder, and add that file to .gitignore so it does not reach GitHub. If you did show the token, in BotFather pick the bot and issue a new token: the old one stops working.


Step 3. Let AI build the bot

I am not technical. Help me build my first Telegram bot. The token is in the .env file as TELEGRAM_BOT_TOKEN. The bot should answer the /start command with a greeting and repeat my messages. Explain each step in plain words and help me run the bot on my computer.

Step 4. Check it

Run the bot on your computer, find it in Telegram and write /start. If it replied, your first bot works.


Step 5. Turn the bot into an agent

For the bot to understand free-form requests we connect AI through an access key (API key). The key is a secret and money: using AI is billed, so set a spending limit in the provider's dashboard. Ask AI: "Connect Claude to the bot, key in .env as ANTHROPIC_API_KEY, and make the bot answer questions about [your topic]".


Step 6. Make the bot safe

  • Restrict the bot: let it answer only you (by your Telegram ID) or your team.
  • Do not feed customer data or trade secrets into the bot.
  • Token and keys only in a protected file or in the hosting settings.

Step 7. Where to host it

While the bot runs on your computer, it answers only when the computer is on. To make it run all the time, host it on a hosting service or your own server (see the guide "How to buy your own server and publish your work on it").

AI Sandbox Lab

We will do it together, step by step

In the program sessions we go through these steps together: you are not left alone with the terminal. Not sure the format fits? Book a free 15-minute call.

See the program

← All materials

Ask a question