Blog / Build with AI /
Building a Telegram bot with AI
A Telegram bot is one of the friendliest first projects in this whole series. There is no website to design, no app store to pass, no hosting puzzle to solve before you see anything work. You message a bot on your phone and it answers.
It is also practical. A bot can remind you of things, log expenses, send you a message when your site goes down, or save links you forward to it. That makes it a nice fit for anyone who runs websites and wants alerts in the place they already look.
The finished thing, in one line
A small Python program that listens for messages sent to your bot and replies. We will start with a bot that saves notes and repeats them back.
How a Telegram bot works
Picture a post box with your name on it. People drop messages into the box. Your program checks the box, reads each message, and sends a reply.
In technical terms, Telegram runs the box. Your code asks Telegram, “Anything new for my bot?” (this is called polling), or Telegram can call your code when something arrives (a webhook). Polling is simpler to begin with, and I would start there. If webhooks are unfamiliar, webhooks explained for automation builders covers the idea.
Step 1: create the bot
Inside Telegram, you talk to an official bot called BotFather. You send it a command to create a new bot, give the bot a name and a username that ends in bot, and BotFather replies with a token: a long string that works like a password for your bot.
That token is the key to your bot. Anyone who has it can control the bot. So:
- Do not paste it into a public repository.
- Do not put it in code that gets published.
- Keep it in an environment variable or a separate file that is not shared.
Environment variables for beginners explains how, and never put API keys in front-end code explains why. If a token leaks, BotFather lets you generate a new one.
Step 2: choose a language and library
Python is the easiest choice here, and there are well-known libraries that handle the conversation with Telegram so you do not have to write it from scratch. Ask your AI tool which library it suggests, then check it is actively maintained and read its documentation page. Libraries change their style between versions, and AI sometimes writes code for an older version. If you get odd errors, that is often the cause.
Step 3: write the prompt
Be specific about what the bot does:
Write a Python Telegram bot. It should read the bot token from an environment variable called TELEGRAM_TOKEN. When a user sends /start, reply with a short welcome message. When a user sends any other text, save it to a file called notes.txt with the date and time, and reply “Saved.” When a user sends /list, reply with the last five notes. Use polling. Add comments to every function and tell me exactly how to run it.
Notice what is named: the token source, three behaviours, the storage, and the way of receiving messages. If you leave these vague, the AI fills the gaps with guesses.
Prompt templates for building small tools has a reusable structure for this kind of request.
Step 4: run it
Ask the AI to explain how to set up a virtual environment, install the library and set the environment variable on your system. If you have never done it, running Python scripts without being a developer is a gentle walk-through.
Start the script. It should sit there, waiting. Open Telegram, find your bot, press Start and send a message. If it replies, you have a working bot.
If it does not, copy the exact error into your AI chat. Do not paraphrase it. That is the advice in debugging with AI: give it the error, not the frustration.
Step 5: restrict who can use it
This step is the one that gets skipped. By default, anyone who finds your bot’s username can message it. For a personal tool that is a problem.
Telegram gives each user a numeric ID. Ask the AI to add a check:
Only respond to messages from the user ID 123456789. Ignore everyone else silently.
You can find your ID by asking another bot, or by having your own bot print the ID from the first message you send it. Then lock the door.
Step 6: add something useful
Once the basic loop works, turn it into something you would use. A few ideas that suit people who manage sites:
- Uptime alerts. A script checks a URL every few minutes and the bot messages you if it fails. Building an uptime checker with AI is a good base.
- A reminder for domain renewals. See building a domain expiry reminder with AI.
- A link saver. Forward an article to the bot and it appends the URL to a spreadsheet.
- A daily summary. Pull a few numbers from a report and send them each morning.
Choose one. Resist building all four.
Step 7: decide where it runs
A bot only works while your program is running. If you close your laptop, it goes quiet. That is fine for experiments, less so for alerts.
Options:
- Your own computer. Free, unreliable.
- A small cloud server or VPS. Reliable, costs a little, needs some setup.
- A scheduled job. If the bot only sends messages (like a daily summary), you do not need it running all the time; a scheduled script can send a message and exit. Scheduling scripts to run automatically shows how.
Sending only is much simpler than listening. For alerts, it is often all you need.
What can go wrong
Duplicate or missing replies
Polling code can process the same message twice if it is written carelessly. The popular libraries handle this, so lean on them instead of writing your own loop.
The bot stops overnight
Programs crash, servers restart. Make sure your host restarts the program and log errors somewhere you can read them. Logging: knowing what your tool did helps.
Handing messages to AI
If your bot passes user messages to an AI service, remember that text leaves your machine. Keep to the minimum and read your provider’s terms. Add a limit on how many requests a user can make, or one chatty friend could run up your bill. Rate limits and quotas covers it.
Storing private notes in plain text
A text file on your laptop is fine. A text file on a public server is not. Think about where the data sits.
Key takeaways
- BotFather creates the bot and gives you a token. Guard it.
- Start with polling, one behaviour, one user.
- Restrict access to your own Telegram ID.
- Pick a place for it to run that matches how reliable it must be.
- Send-only bots are the simplest and often enough.
Related reading
- Building an internal link finder script with AI
- Automating file renaming and organising with AI scripts
- Building an order export tool with AI
What is one message you wish arrived on your phone automatically? That is your first bot.