Build a Telegram bot powered by AI
Message your own AI from your phone, wherever you are. You'll build a Telegram bot in Python that answers with a model on your PC (free and private) or an API model, talks only to you, remembers each conversation, respects rate limits and keeps running on its own.
.env/reset command to wipe it- Protection on both sides: Telegram's flood limits handled for you, plus your own cooldown and daily cap to protect your API bill
- Two ways to keep it running: Task Scheduler on your Windows PC, or a small Linux VPS with systemd
- A starter kit with an offline test that checks the whole bot with no token, no internet and no model
- Telegram on your phone (free). Creating a bot is free and needs no extra phone number.
- uv, the Python tool from Put AI in your terminal. It fetches Python and the bot's libraries for you.
- A brain, one of these. Local: Ollama 0.20.7 or newer with
gemma4:e2b, from Run a ChatGPT-style AI on your own laptop. Free, and your PC has to be on. Or an API model: an account and prepaid credit with a provider. The example uses OpenAI's gpt-5-mini at $0.25 per million input tokens and $2 per million output tokens (OpenAI pricing ↗). - Optional, for 24/7 without your PC: a small Linux VPS. DigitalOcean's Basic Droplets start at $4 a month (512MiB RAM) or $6 a month (1GiB), billed per second and capped at the monthly price (DigitalOcean pricing ↗). Prices are in US dollars and change from time to time, so check the links. Oracle Cloud's Always Free tier is a no-cost alternative (step 7). Webhooks (also optional) need a domain too.
- Comfort with the terminal:
cd, running a command, reading an error. If that's new, do the Terminal survival kit first.
Create your bot with BotFather
BotFather is Telegram's own bot for making bots. It gives you a token, and that token is the key to your bot.
A Telegram bot is a special account that your program controls through the Bot API ↗. You don't log in as the bot. Instead, your code sends every request with a secret token, and Telegram treats whoever holds it as the bot.
In Telegram, search for @BotFather (it has a blue verified tick, so ignore look-alikes) and send:
Message to @BotFather/newbot- Name: what people see in the chat header, like
Sam's AI. You can change it later. - Username: 5 to 32 characters, Latin letters, numbers and underscores only, and it must end in
bot, likesams_pocket_ai_bot. You can't change it later.
- Name: what people see in the chat header, like
BotFather replies with a link to your bot and the token. A token is a number, a colon, then a long code. This is the example token from Telegram's own docs ↗:
What a token looks like110201543:AAHdqTcvCH1vGWJxfSeofSAs0K5PALDsawKeep the chat with BotFather open; you'll paste the token into a file in the next step. Now two settings that shrink what a stranger could do with your bot. First, stop anyone adding it to a group:
Message to @BotFather/setjoingroupsPick your bot, then Disable. Second, give it a command menu, so
/in the chat shows what it can do:Message to @BotFather/setcommandsPick your bot and send this as one message:
Command liststart - Say hello reset - Forget this conversationCarefulTreat the token like a password. Anyone who has it can read every message sent to your bot and reply as it. Never put it in code, a screenshot, a GitHub repo, a Discord message or a chat with an AI assistant. If it ever leaks, send/mybotsto BotFather, pick your bot, tap API Token, then Revoke current token. The old token dies at once and BotFather gives you a new one to put in.env.
t.me/... link it sent opens a chat with your new bot.If it breaksBotFather won't accept the username + 1 more
BotFather won't accept the username
bot. Usernames are first come, first served, so add a word or an underscore: sams_pocket_ai_bot rather than ai_bot.I closed the chat and lost the token
/mybots, pick your bot and tap API Token. It shows the current token again. Nothing needs revoking unless someone else might have seen it.