Linux Server

Build an AI Customer Service Telegram Bot with n8n: Self-Hosted Install, AI Agent, Memory and Human Handover

7 steps 14 min read 30 views 0
On this page

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.

Telegram delivers messages to n8n through a webhook, which requires a public domain with a valid HTTPS certificate. Set up DNS and the certificate first. If you use a commercial AI API, use it only from regions the provider supports and follow its usage policies; for confidential conversations prefer a local model.

Step 1: Create the Telegram Bot

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.

Step 2: Install n8n

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.

Step 3: Put n8n Behind HTTPS

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.

Step 4: Add Credentials

  • Telegram: create a Telegram credential with the bot token from Step 1.
  • Local model: create an Ollama credential with base URL 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).
  • Or an AI API: create the credential for your provider; providers with an OpenAI-compatible API can use the OpenAI credential with a custom base URL.

Step 5: Build the Workflow

Create a new workflow and add these nodes (names may differ slightly between n8n versions):

  1. Telegram Trigger, listening for message updates.
  2. AI Agent, with the user message taken from the trigger and a system message describing the bot's job.
  3. Attach a chat model to the agent (Ollama Chat Model with 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.
  4. Telegram node, operation Send Message, replying to the same chat with the agent's output.

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.

Step 6: Activate and Test

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.

Step 7: Control Who Can Use the Bot and Add a Human Handover

  • Allow-list: for an internal bot, add an If node after the trigger that continues only when {{ $json.message.chat.id }} is one of your team's chat IDs.
  • Handover: add a branch that forwards the conversation to your support group when the message contains words such as "refund", "human" or "complaint", and tell the customer that a colleague will answer.
  • Knowledge: for answers based on your own documents, combine this with a knowledge base as described in building a private RAG chatbot with AnythingLLM.

FAQ

The bot worked, then stopped answering

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.

Replies take too long

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.

Can n8n do more than chat?

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.

Was this answer helpful?

Related Tutorials