Many teams already talk to customers on Telegram. With n8n, an open-source workflow automation tool, you can build a Telegram bot that answers questions with an AI model, remembers the conversation, follows your instructions about tone and scope, and hands over to a human when needed, without writing a full application. This guide installs n8n on your server behind HTTPS, creates the bot, and builds the workflow with either a local model in Ollama or an AI provider's API.
In Telegram, talk to @BotFather, send /newbot, choose a name and a username ending in "bot", and copy the token. With /setdescription and /setuserpic you can make the bot look professional. Keep the token secret; anyone with it controls the bot.
n8n runs in Docker (see our Docker installation guide). Replace the domain and time zone with your own.
docker volume create n8n_data
docker run -d --name n8n --restart always \
-p 127.0.0.1:5678:5678 \
--add-host=host.docker.internal:host-gateway \
-e N8N_HOST=n8n.example.com \
-e N8N_PROTOCOL=https \
-e WEBHOOK_URL=https://n8n.example.com/ \
-e GENERIC_TIMEZONE=Asia/Hong_Kong \
-e TZ=Asia/Hong_Kong \
-e N8N_PROXY_HOPS=1 \
-v n8n_data:/home/node/.n8n \
docker.n8n.io/n8nio/n8n
WEBHOOK_URL must be the public HTTPS address; n8n uses it when it registers the webhook with Telegram.
server {
listen 443 ssl;
http2 on;
server_name n8n.example.com;
ssl_certificate /etc/letsencrypt/live/n8n.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/n8n.example.com/privkey.pem;
location / {
proxy_pass http://127.0.0.1:5678;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_buffering off;
proxy_read_timeout 300s;
}
}
Open https://n8n.example.com and create the owner account immediately. Certificates are covered in free SSL with Certbot or acme.sh.
http://host.docker.internal:11434 (Ollama set up as in running a local LLM with Ollama, including the step that lets Docker containers connect and its firewall rules).Create a new workflow and add these nodes (names may differ slightly between n8n versions):
qwen2.5:7b, or your provider's chat model) and a memory node (Simple Memory) keyed by the chat ID, so each customer has their own conversation history.The expressions to use in the node fields:
AI Agent - prompt (user message): {{ $json.message.text }}
Simple Memory - session key: {{ $('Telegram Trigger').item.json.message.chat.id }}
Telegram - chat ID: {{ $('Telegram Trigger').item.json.message.chat.id }}
Telegram - text: {{ $json.output }}
A good system message keeps answers short and on topic, for example:
You are the customer service assistant of ACME Hosting.
Answer briefly and politely in the customer's language.
Only answer questions about our services, orders and technical support.
If you are not sure, or the customer asks for a refund or a person,
say that a colleague will reply soon. Never invent prices or promises.
Save the workflow and activate it; n8n then registers the webhook with Telegram. Send your bot a message. If nothing happens, check the webhook status:
curl -s "https://api.telegram.org/bot<YOUR_BOT_TOKEN>/getWebhookInfo"
The url should be your n8n domain, and last_error_message explains failures such as certificate problems or an unreachable server.
{{ $json.message.chat.id }} is one of your team's chat IDs.Only one webhook can be set per bot token. Another workflow, test run or script using the same token replaces it. Check getWebhookInfo, keep one active workflow per bot, and use a separate bot for testing.
On CPU, smaller models answer faster. Keep the system message short, limit the memory window to the last 10 messages, and keep the model loaded in Ollama.
Yes. The same workflow can look up orders in your database, create support tickets, post to a group or send daily reports. n8n's server requirements are covered in Dify, n8n and AI agent server requirements.
Still stuck after following these steps? Open a support ticket and the IMIDC 24/7 technical team will help. Include the server IP, your n8n domain, the output of getWebhookInfo and docker logs n8n so we can pinpoint the issue faster.