Build an AI Telegram Bot with n8n
Put the Lead Desk agent on Telegram: a public bot that answers 24/7, with private memory for every user, ready to sell to clients.
Series: Road to AI Automation · Episode 3 · Level: Intermediate · Time: about 45 minutes to follow along
Before this: Episode 2. We reuse the Lead Desk agent.
What you will build
A real AI Telegram bot. Anyone can message it, each person gets their own private memory, and it answers 24/7.
Telegram Trigger → AI Agent → Telegram (Send a text message)
├─ Gemini
├─ Simple Memory (key = chat ID)
└─ Tools (Leads Sheet + more)Part 1: Create the bot with BotFather
- In Telegram, search @BotFather (official, blue tick).
- Send
/newbot. - Give a display name (for example Lead Desk) and a username that ends in bot (for example
leaddesk_demo_bot). - Copy the bot token.
- In n8n, create a Telegram API credential and paste the token.
Part 2: Reality check, Telegram needs public HTTPS
Telegram has to send each message to n8n. It cannot reach localhost. You need a public HTTPS URL.
Option A: n8n Cloud. Works out of the box.
Option B: your VPS + a domain + HTTPS. Example with Caddy (it gets the HTTPS certificate automatically):
n8n.yourdomain.com {
reverse_proxy localhost:5678
}Then tell n8n its public address with an environment variable:
WEBHOOK_URL=https://n8n.yourdomain.com/
Part 3: Telegram Trigger
- New workflow (or duplicate your Episode 2 agent).
- Add Telegram Trigger → trigger on Message.
- Select your Telegram credential.
- Click Test step, open your bot in Telegram, press Start, and send a message.
Simplified output:
{
"message": {
"chat": { "id": 518204771 },
"from": { "first_name": "Sara" },
"text": "Hi! Do you build chatbots for clinics?"
}
}The two fields that drive the bot:
message.text: what the person wrotemessage.chat.id: who sent it, and where to reply
Part 4: Connect the AI Agent
- Add the AI Agent after the trigger.
- Source for prompt: Define below. Prompt (user message):
{{ $json.message.text }} - Keep the Gemini model, system message and tools from Episode 2.
- Simple Memory → Session ID: Define below. Key:
{{ $('Telegram Trigger').item.json.message.chat.id }}Part 5: Send the reply
- Add Telegram → Send a text message.
- Chat ID:
{{ $('Telegram Trigger').item.json.message.chat.id }} - Text:
{{ $json.output }} - Additional fields: turn off Append n8n Attribution so the bot looks like your own product.
Test from your phone, send a follow-up (it remembers), and ask a friend to try (they get separate memory).
Part 6: Turn it into a product clients pay for
| Upgrade | How |
|---|---|
| Knowledge tool | Put FAQ, services and prices in a Google Sheet; give the agent a tool to read it |
| Lead capture | A Google Sheets tool that appends name, email and need; tell the agent to collect them |
| Human handover | A Gmail tool; instruct the agent to email the owner when someone asks for a human |
| Typing indicator | A Telegram node right after the trigger: Send Chat Action → typing |
| Non-text messages | An If node: if there is no message.text, reply "Please send a text message" |
Part 7: Go live, and avoid these traps
- Activate / Publish the workflow. This registers the production webhook with Telegram. The bot then runs even with your browser closed.
- One webhook per bot. Clicking Test step while the workflow is active can take over the webhook and stop the live bot. Use a separate test bot (its own token) for development.
- 4,096 characters is Telegram's message limit. Tell the agent to keep answers short.
- Free-tier limits: Gemini's free tier is great for learning and demos. Check your quota as users grow.
- Executions tab: every real conversation is logged there for debugging.
Troubleshooting
| Problem | Fix |
|---|---|
| Trigger never fires | n8n is not reachable over public HTTPS, or WEBHOOK_URL is wrong |
| Live bot stopped after testing | The test listener took the webhook; re-activate, or use a separate test bot |
| Users see each other's context | The memory key is not set to the chat ID |
| Reply goes nowhere | The Chat ID expression in the Send node is wrong |
| Workflow errors on stickers or photos | Add the If node for missing text |
Practice
- Add the lead capture tool and check rows appear in your sheet.
- Add the typing indicator.
- Restrict the bot to your own chat ID with an If node (a private assistant).
Next episode: deploy n8n on your own VPS with Docker + HTTPS, production ready.
Key takeaways
- Create the bot with @BotFather and keep its token secret
- Telegram needs a public HTTPS URL for n8n (n8n Cloud, or a VPS with a domain and WEBHOOK_URL)
- Use the chat ID as the memory key so every user gets a private conversation
- Use a separate test bot: testing an active bot can take over its webhook