- n8n
- ai
- automation
- vps
Your own AI automation server: building a Telegram assistant with n8n and AI
Installing n8n on a VPS and connecting it to the Claude or OpenAI API: a Telegram assistant that answers with AI and a flow that sorts incoming email. Step by step, with no code or very little.
Every day you do the same tasks: reading incoming email and working out who it is for, answering the same questions, copying data from one system to another. Today most of this work can be automated with an AI model and a few "blocks".
One of the most convenient tools for this is n8n: a visual automation platform where you build a workflow by connecting blocks, and write JavaScript when you need to. It is similar to Zapier and Make, but with one big difference: it can run on your own server.
In this post we will:
- set up n8n on a VPS with Docker, with HTTPS;
- build a Telegram assistant that answers with AI;
- build a second flow that sorts incoming email with AI and sends the important ones to Telegram.
Why your own server
- Price. With cloud services the bill grows with the number of executions. On your own server the flows run as much as you like, and all you pay is the server's monthly price.
- Your data stays with you. The flows, credentials and history are on your server.
- No limits. As many flows as you want, as often as you want.
One honest note: the AI model itself does not run on a small VPS, that takes a powerful GPU. We will call the AI through an API (Claude, OpenAI and so on). The text you send it goes to that service's servers, so keep this in mind when working with confidential data.
What you need
- VPS: at least 1 vCPU and 2 GB RAM, Ubuntu 24.04. For n8n this is comfortably enough.
- A domain or subdomain, for example
n8n.example.com. Telegram webhooks only work over HTTPS, so HTTPS is a must. - An AI API key: from an Anthropic (Claude) or OpenAI account.
- A Telegram bot: created in a minute with
/newbotin @BotFather, which gives you a token.
Step 1: Docker and n8n
Install Docker:
curl -fsSL https://get.docker.com | sudo sh
sudo usermod -aG docker $USERLog out of the server and back in, then create a folder:
sudo mkdir -p /opt/n8n && sudo chown $USER /opt/n8n
cd /opt/n8ndocker-compose.yml:
services:
n8n:
image: docker.n8n.io/n8nio/n8n
restart: unless-stopped
ports:
- "127.0.0.1:5678:5678"
environment:
- N8N_HOST=n8n.example.com
- N8N_PROTOCOL=https
- WEBHOOK_URL=https://n8n.example.com/
- GENERIC_TIMEZONE=Asia/Baku
- TZ=Asia/Baku
volumes:
- n8n_data:/home/node/.n8n
caddy:
image: caddy:2
restart: unless-stopped
network_mode: host
volumes:
- ./Caddyfile:/etc/caddy/Caddyfile:ro
- caddy_data:/data
volumes:
n8n_data:
caddy_data:Caddyfile:
n8n.example.com {
reverse_proxy 127.0.0.1:5678
}n8n listens only on 127.0.0.1, so it cannot be reached directly from the internet. Only Caddy is exposed, and it obtains and renews the HTTPS certificate by itself. WEBHOOK_URL matters: when n8n tells Telegram "send the messages to this address", this is exactly the address it will give.
The domain's A record must point to the server, and ports 80 and 443 must be open. Start it:
docker compose up -dA shorter way on JezaHost: without Caddy. The portal has a Domains & SSL → Add domain section. In your domain's DNS, point theArecord to95.86.162.6(for a subdomain, theCNAMErecord totenant-web.jezacloud.com), pick your server from the list and enter5678as the port. The SSL certificate is issued automatically and renews itself. In this case remove thecaddyservice fromdocker-compose.ymland change n8n's port line to"5678:5678"so the portal's forwarding can reach it. If you use Cloudflare, set the cloud to grey ("DNS only") for the domain. Details: Domains & SSL.
Step 2: first login, right away
Open https://n8n.example.com. On first launch n8n offers to create an owner account. Do it right away: until the owner account is created, the panel is open to anyone who knows the address.
Set a strong password and turn on two-factor login (2FA) in the account settings. Your API keys will be stored on this server.
Step 3: a Telegram assistant that answers with AI
In the Credentials section, add two credentials:
- Telegram API: the token BotFather gave you;
- Anthropic or OpenAI: your API key.
Create a new workflow and connect three blocks:
- Telegram Trigger: choose
messageas "Updates". The flow runs every time the bot receives a message. - AI Agent: connect an Anthropic or OpenAI model as the "Chat Model". Give the user's message to the "Prompt" field:
{{ $json.message.text }}. The "System message" is the assistant's character, for example: "You are our company's internal assistant. Answer briefly and clearly in English. Do not make up what you do not know, say "I don't know"." - Telegram → Send Message: put
{{ $('Telegram Trigger').item.json.message.chat.id }}in the "Chat ID" field and the AI Agent's answer ({{ $json.output }}) in the "Text" field.
Make the flow Active and write to the bot. The answer will arrive in a few seconds.
If the bot is only for you and your team, put an IF block after the Telegram Trigger and let through only the IDs of allowed users. Otherwise anyone who finds your bot will be using your API account.
Step 4: sorting email with AI
The second flow is more practical: email arriving in the mailbox is classified by AI, and only the important messages reach Telegram.
- Email Trigger (IMAP): connect to your mailbox. The flow starts when a new email arrives.
- AI Agent (or a simple "Basic LLM Chain"), with the instruction: "Classify this email. Return only one word: URGENT, CUSTOMER, INVOICE, SPAM, OTHER. Then, on a new line, write a one-sentence summary."
- Switch: split the flow into branches by the first word of the answer.
- In the URGENT and CUSTOMER branches, Telegram → Send Message: the sender, the subject and the AI summary.
As a result you no longer open your mail ten times a day: what matters finds you by itself.
What it costs
- Server: a fixed monthly price.
- AI API: by usage. For short messages and email summaries small, cheap models are enough, and even hundreds of requests a day usually come to a few dollars a month. Do not forget to set a spending limit in your API account.
Updates and backups
Updating takes two commands:
cd /opt/n8n
docker compose pull && docker compose up -dAll flows, credentials and history live in the n8n_data volume. Back it up regularly:
docker run --rm -v n8n_n8n_data:/data -v /opt/backup:/backup alpine \
tar czf /backup/n8n-$(date +%F).tar.gz -C /data .If you put this command in cron and also copy the backup somewhere else, your flows will not be lost whatever happens to the server.
A short checklist
- n8n listens only on
127.0.0.1, only HTTPS is open to the outside. - The owner account is created, 2FA is on.
- The Telegram bot has an allowed-users filter.
- A monthly spending limit is set in the API account.
- The
n8n_datavolume is being backed up. - Confidential data is not sent to the AI, or that is a conscious decision.
It is one evening's work, but it saves minutes every day. Once the first flow is running, ideas come by themselves: writing invoices into a spreadsheet, passing enquiries from the website to the CRM, preparing the morning report automatically.
Having the server in Baku means your flows work fast with local systems. On JezaHost a VPS is ready in a few minutes, and connecting a domain and a free SSL certificate are included in the portal.