Basic_N8N_connection to telegram via gmail

A one-click self-hosted n8n build, plus a daily 7 AM workflow that reads unread Gmail, triages it with AI, and sends the digest to Telegram.

Schedule 7:00 Gmail: get unread Aggregate AI triage Telegram

1 Get the server (one-click n8n)

1.1 Get the VPS

1.2 Deploy n8n (one-click)

⚠️ After install, Docker Manager shows two containers: n8n-xxxx and traefik. Traefik is the reverse proxy that auto-provisions HTTPS — SSL is handled out of the box.

1.3 First login

Verify SSH access while you're here

ssh root@YOUR_SERVER_IP

2 Your first workflow: the 7 AM digest (W2)

What we're building: a workflow that wakes up at 7 AM, reads your unread Gmail, has AI sort it by urgency, and sends the digest to your Telegram — every day, without you.

Schedule (7:00) Gmail: get unread Aggregate AI triage Telegram

2.1 Prerequisites (10 min, one-time)

A. Telegram bot — the assistant's mouth

B. Google OAuth — n8n's permission to read your mail

⚠️ "Google hasn't verified this app" is expected — it's your app accessing your account: Advanced → Go to <project> (unsafe) → Allow → back in n8n: "Account connected" ✅.

2.2 Create the workflow + Schedule Trigger

2.3 Node 2 — Gmail: get the unread mail

2.4 Node 3 — Aggregate: 30 emails → 1 item

Why: n8n runs the NEXT node once per item. 30 emails would mean 30 separate AI calls. Aggregate merges them into one item first → one AI call.

2.5 Node 4 — the AI brain (Basic LLM Chain)

You are an email triage assistant. Here are today's unread emails as JSON:

{{ JSON.stringify($json.data) }}

Write a short Telegram morning digest:
- Start with "☀️ Good morning Rahul. X unread emails."
- "🔴 Need reply:" (sender + one-line why) — only genuinely personal/actionable
- "🟡 Worth reading:" max 3
- "⚫ Ignore:" just the count (newsletters, promos, notifications)
Keep it under 150 words. No markdown headers, just the emoji lines.

Execute step → OUTPUT shows your actual digest text. First AI run of the day. 🧠

2.6 Node 5 — deliver to Telegram

2.7 Test end-to-end, then make it live

🔍 The Executions tab logs every run — click one to inspect exactly what each node received and produced. Your best friend when debugging.

3 Custom domain + SSL (Cloudflare)

Routing your domain through Cloudflare gives you free edge SSL, DDoS protection, and webhook delivery that never depends on a lone VPS IP. This guide uses jarvis.jhooq.org — substitute your own domain everywhere.

3.1 Domain on Cloudflare

3.2 Point n8n at the new domain

SSH into the VPS (or use hPanel's Browser terminal) and run — adjust the domain if yours differs:

cd /docker/n8n-*        # your app dir — e.g. /docker/n8n-nohc
cp docker-compose.yml docker-compose.yml.bak

# 1) Traefik: also answer on jarvis.jhooq.org (the original URL keeps working)
sed -i 's#rule=Host(`${COMPOSE_PROJECT_NAME}.${TRAEFIK_HOST}`)#rule=Host(`${COMPOSE_PROJECT_NAME}.${TRAEFIK_HOST}`) || Host(`jarvis.jhooq.org`)#' docker-compose.yml

# 2) n8n: register all webhooks (Telegram!) on the armored address
sed -i 's#WEBHOOK_URL=https://${COMPOSE_PROJECT_NAME}.${TRAEFIK_HOST}/#WEBHOOK_URL=https://jarvis.jhooq.org/#' docker-compose.yml

docker compose up -d    # recreate — workflows & credentials survive (n8n_data volume)
EditEffect
|| Host(`jarvis.jhooq.org`)Traefik (reverse proxy) accepts requests for the new hostname
WEBHOOK_URL=https://jarvis.jhooq.org/n8n registers future webhooks (Telegram trigger!) on the proxied domain

3.3 Verify

💡 Set the proxied domain up before building the Telegram-triggered assistant. Telegram learns the Cloudflare address on its first webhook registration — no stale-DNS gotchas.

4 The assistant: brain + memory (W1)

4.1 Create the Telegram bot

4.2 Telegram Trigger

4.3 AI Agent — brain and memory

You are Jarvis, Rahul's personal AI assistant. Be concise, friendly,
slightly witty. Answer in short messages suitable for Telegram.
If asked to do something you have no tool for yet, say that power
is coming soon.

4.4 Reply + publish

4.5 Give the assistant your inbox (Gmail tool)

You can read Rahul's Gmail with the Gmail tool. When asked about
email, fetch messages first, then summarize: group by urgency
(needs reply / important / ignore). Never invent emails.

Publish (edits aren't live until published!) → from your phone: "summarize my unread emails" → the 47→2 moment 🔥

5 Backups, updates & the time-travel demo

5.1 Snapshots & weekly backups

5.2 The time-travel demo

  1. Snapshot the server.
  2. Change something visible — add Always answer like a pirate. to the assistant's system message → publish → the bot answers "Arrr, ahoy!" 🏴‍☠️
  3. Restore the snapshot (~2–5 min downtime) → change is gone, bot is normal again.
⚠️ Restore reverts the entire server to snapshot time — workflows, executions, everything after it.

5.3 Updating n8n safely

# 1. snapshot first (hPanel)
# 2. then:
cd /docker/n8n-* && docker compose pull && docker compose up -d

Version bumps, workflows intact. Turn on 2FA in n8n (Settings → Personal) while you're at it.

? Troubleshooting (webhooks, OAuth, gotchas)

Telegram trigger: "Listening…" but nothing arrives

The #1 gotcha. Check what Telegram thinks (paste in a browser with your token):

https://api.telegram.org/bot<TOKEN>/getWebhookInfo

Webhook timeouts: the host's network drops Telegram's traffic

Symptom: bot replies sometimes, then goes silent; local curl to the webhook is instant, but tcpdump -n 'tcp port 443 and (net 149.154.160.0/20 or net 91.108.4.0/22)' shows zero inbound packets during Telegram retries → filtering upstream of your VPS.

Permanent fix — put webhooks behind Cloudflare (Part 3): with the record Proxied, Telegram connects to Cloudflare's network, which is never blocked. Verify with getWebhookInfo: ip_address should be a Cloudflare IP (188.114.x.x / 172.67.x.x).

DNS-cache gotcha: Telegram pins resolved IPs for hours. If it already learned your direct IP, re-register the webhook on a brand-new subdomain (fresh name = fresh lookup = Cloudflare immediately).

Google OAuth

AI Agent answers undefined

The Agent's prompt defaults to n8n's built-in chat (chatInput). With a Telegram trigger you must set Source for Prompt → "Define below" and map {{ $json.message.text }} — the preview under the field must show your actual message.

OpenAI "Rate limit reached"

Usually billing, not rate: API credits are separate from ChatGPT Plus. Check platform.openai.com → Billing. For big payloads (30 emails), trim fields with Aggregate → Specified Fields first.