n8n

How to Automate Telegram Email Search?

Chat with your email history in Telegram or inside n8n. The assistant searches both a database of email text and your structured email tables, then replies with clear answers. Great for operations and team managers who need fast answers from past emails.

New messages enter through two triggers, a Telegram bot and the n8n chat panel. An AI agent with short term memory and a chat model understands the question. It uses two tools to find facts. A PGVector search in PostgreSQL finds similar email content, and a linked helper workflow turns the question into SQL for exact lookups. Results are cleaned, split into safe size chunks, escaped for Telegram Markdown, and sent back in order.

Set up a PostgreSQL database with PGVector and load your email content into the emails_embeddings table. Connect your own SQL helper workflow that receives a natural language question and returns SQL results from your email tables. Expect faster responses, fewer manual searches, and a simple chat experience for everyday email intelligence.

What are the key features?

  • Dual triggers accept questions from Telegram and the n8n chat panel
  • AI agent with short term memory and a chat model to understand intent
  • PGVector retrieval from PostgreSQL to search email embeddings with topK 100
  • Embeddings generated by an Ollama model for local or private processing
  • Linked tool workflow that converts natural language into SQL and returns rows
  • Conditional routing to format responses for Telegram or in app chat
  • Text chunking and batch sends to respect platform limits
  • Markdown escaping to prevent Telegram formatting errors
  • Session id generation to keep each chat thread consistent

What are the benefits?

  • Reduce manual email digging from 30 minutes to 1 minute per question
  • Automate up to 80% of email lookup requests through chat
  • Improve answer accuracy by combining vector matches and SQL results
  • Serve both mobile users on Telegram and desk users in n8n chat
  • Avoid Telegram message limits with safe chunked replies

How do you set it up?

  1. Import the template into n8n: Create a new workflow in n8n > Click the three dots menu > Select 'Import from File' > Choose the downloaded JSON file.
  2. You'll need accounts with Telegram, OpenAI, Ollama and PostgreSQL. See the Tools Required section above for links to create accounts with these services.
  3. Create a Telegram bot with BotFather and copy the bot token. In the n8n credentials manager, create a Telegram credential and paste the token. Open the Telegram Trigger and Telegram Send nodes and select this credential. Set the chatIds field in the trigger to your chat or group id.
  4. In the n8n credentials manager, create an OpenAI API credential. If you use an OpenAI compatible endpoint, set the base URL and API key. Open the OpenAI Chat Model node and choose your credential and preferred model.
  5. Install and run Ollama on your server. Pull the nomic-embed-text model. In n8n, create an Ollama API credential pointing to your Ollama host. In the Embeddings node, select this credential and the model name.
  6. Prepare PostgreSQL with the pgvector extension. Create your database and the emails_embeddings table that holds text chunks and vectors. In n8n, add a PostgreSQL credential and select it in the PGVector Store node. Confirm the tableName is emails_embeddings and the mode is retrieve as tool.
  7. Link the SQL helper workflow. Import or build a workflow that receives a natural language query, composes SQL, and returns results from your email tables. In the Call the SQL composer Workflow node, select that workflow and map the natural_language_query input.
  8. Check routing. In the Came from Telegram node, keep the logic that directs Telegram messages to the chunking and send path, and UI chats to the formatting path.
  9. Run a quick test in the n8n chat panel. Ask a question like meetings next week. Verify you get combined answers from vector search and SQL.
  10. Test in Telegram by messaging your bot. Confirm replies arrive in multiple messages when long. If you see formatting errors, review the Escape Markdown node and keep parse mode as MarkdownV2.
  11. Troubleshoot common issues: no results means your embeddings table may be empty or misnamed, Telegram no reply often means a wrong chat id, and database errors usually point to credential or network settings. Keep chunk size at or below 500 characters to stay well within Telegram limits.

Tools Required

$24 / mo or $20 / mo billed annually to use n8n in the cloud. However, the local or self-hosted n8n Community Edition is free.

Ollama

Sign up

Free tier: $0 (self-hosted local API)

OpenAI

Sign up

Pay-as-you-go: GPT-5 at $1.25 per 1M input tokens and $10 per 1M output tokens

PostgreSQL

Sign up

Free: $0 (open-source PostgreSQL License; self-hosted)

Telegram

Sign up

Free: $0, Telegram Bot API usage is free for developers

Similar Templates

Join Futurise to access 1,200+ automation templates

Get instant access to ready-made automation workflows for n8n, Make.com, AI agents, and more. Download, customise, and deploy in minutes.