brainax

Your own Telegram bot

Lesson 14 of 22 in the course “AI Agents for Life”. Free, with animations and a quiz.

An agent in your messenger

The best agent is the one that’s close at hand. A website chat has to be opened, found and reminded who it is every time. A Telegram bot lives where you already spend your day. The brief is built in, so you keep it short: “+ buy cat food” or “how much did I spend on taxis this month?” By the way, the bot itself is free: Telegram doesn’t charge for creating bots. You only pay for the AI step in Zapier or n8n, and both have a free way to start.

BotFather: a bot in 2 minutes

Every Telegram bot is born at one bot: @BotFather. Find it in search and check the blue checkmark, because there are plenty of fakes. Send /newbot. First it asks for a name that everyone sees, for example “Liza’s helper.” Then a username, which is how people find the bot. Telegram’s rule: the username must end in “bot,” like liza_helper_bot. If it’s taken, add numbers. BotFather replies with a long string of numbers and letters. That’s the token, and it’s the next slide.

The token: never to anyone

The token isn’t your account password, it’s the key to the whole bot. With it, someone can read what people send the bot and reply in its name. Telegram says it plainly: keep your token secure. Where it must never go: a screenshot, a chat with friends, a public GitHub repo, or even the text of the AI brief. Where it belongs: in the connection settings in Zapier or n8n, where it stays hidden. If it leaks anyway, don’t panic: in BotFather, use /token to issue a new one, and the old one stops working.

Answer only you

A bot’s username is public, so anyone can find it and message it. Without protection your bot answers everyone, and the AI step burns your limit on strangers’ essays. The fix is the chat ID. It’s a number that comes with every message, and yours is always the same. Finding it is easy: message the bot, open a test run in Zapier or n8n and look for the chat id field. Then add a filter right after the trigger: if the chat ID isn’t yours, stop. n8n even has a ready-made option for it: Restrict to Chat IDs. It’s the same least privilege from the connectors lesson, just for a bot.

Building the chain

The chain is the same as last lesson, only the input changes. Step 1: the trigger “new message in Telegram.” In n8n it’s called Telegram Trigger; in Zapier look for the Telegram Bot app. That’s where the token goes. Step 2: a filter on your chat ID. Step 3: the AI step with the brief from the next slide. Step 4: a reply in the same chat, and if it’s a task or a purchase, a row in Google Sheets too. Tip: test the brief in a normal chat on ten of your own messages first, and only then connect the bot.

Try it: the pocket helper

Create a bot in BotFather and put the token only in the Zapier or n8n settings. Build the four steps from the last slide and paste the brief below into the AI step. Check three things: the bot answers “+ buy bread,” answers a question, and stays silent if you message it from another account. Not ready to connect anything? Copy the brief into a normal chat and write to it as if it were the bot: you’ll see how it works.

Check yourself

  1. What should you do first?
    Correct answer: Issue a new token in BotFather. A new token instantly makes the old one useless. Whoever saw it, the key no longer fits.
  2. How do you stop this?
    Correct answer: A filter after the trigger: answer only her chat ID. A chat ID filter runs before the AI step, so strangers’ messages cost neither limit nor money.
  3. Why won’t BotFather accept the name?
    Correct answer: A bot’s username must end in “bot”. That’s Telegram’s rule for every bot: liza_helper_bot works, liza_helper doesn’t.
  4. Where should the token live?
    Correct answer: In the Telegram connection settings in n8n. Connection settings hide the token, and that’s exactly where it’s needed: n8n uses it to receive messages and reply.

← “If → then”: autopilot without code An agent team: the trip HQ →