Skip to content
Lesson 3 of 845 min video

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

  1. In Telegram, search @BotFather (official, blue tick).
  2. Send /newbot.
  3. Give a display name (for example Lead Desk) and a username that ends in bot (for example leaddesk_demo_bot).
  4. Copy the bot token.
  5. In n8n, create a Telegram API credential and paste the token.
Security: the token controls your bot. Never share it or show it in screenshots or videos.

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/
Testing on a laptop? A tunnel such as cloudflared gives a temporary HTTPS URL.

Part 3: Telegram Trigger

  1. New workflow (or duplicate your Episode 2 agent).
  2. Add Telegram Trigger → trigger on Message.
  3. Select your Telegram credential.
  4. 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 wrote
  • message.chat.id: who sent it, and where to reply

Part 4: Connect the AI Agent

  1. Add the AI Agent after the trigger.
  2. Source for prompt: Define below. Prompt (user message): {{ $json.message.text }}
  3. Keep the Gemini model, system message and tools from Episode 2.
  4. Simple Memory → Session ID: Define below. Key:
{{ $('Telegram Trigger').item.json.message.chat.id }}
Why this matters: without the chat ID as the key, every user shares one memory. With it, each person gets a private conversation.

Part 5: Send the reply

  1. Add Telegram → Send a text message.
  2. Chat ID: {{ $('Telegram Trigger').item.json.message.chat.id }}
  3. Text: {{ $json.output }}
  4. 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

UpgradeHow
Knowledge toolPut FAQ, services and prices in a Google Sheet; give the agent a tool to read it
Lead captureA Google Sheets tool that appends name, email and need; tell the agent to collect them
Human handoverA Gmail tool; instruct the agent to email the owner when someone asks for a human
Typing indicatorA Telegram node right after the trigger: Send Chat Action → typing
Non-text messagesAn 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

ProblemFix
Trigger never firesn8n is not reachable over public HTTPS, or WEBHOOK_URL is wrong
Live bot stopped after testingThe test listener took the webhook; re-activate, or use a separate test bot
Users see each other's contextThe memory key is not set to the chat ID
Reply goes nowhereThe Chat ID expression in the Send node is wrong
Workflow errors on stickers or photosAdd the If node for missing text

Practice

  1. Add the lead capture tool and check rows appear in your sheet.
  2. Add the typing indicator.
  3. 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