Multimodal Telegram Chatbot using Gemini AI, PostgreSQL, and Document Processing - n8n Workflow

Build an advanced multimodal Telegram chatbot using n8n that processes text, audio, images, videos, and various document types (PDF, CSV, HTML, XLSX). This n8n workflow uses PostgreSQL for state management and Gemini AI for intelligent analysis and chat memory.

Workflow Preview

Ready to automate?

Download this n8n workflow template and start using it instantly.

Who is this best for?


  • Developers and technical users seeking complex, stateful n8n templates.

  • Organizations requiring a powerful, multimodal chatbot deployed via Telegram.

  • Automators needing to integrate file processing (PDF, CSV, etc.) directly into conversational AI using n8n.

  • Users running self-hosted n8n and needing robust database integration (PostgreSQL) for chat history and media queuing.

Overview

This comprehensive n8n workflow transforms a standard Telegram bot into an advanced multimodal AI assistant. The core challenge solved here is synchronizing and processing messages sent as a media group (multiple files in a single Telegram message), which traditional chatbots struggle with. This n8n workflow uses PostgreSQL as a temporary queue and permanent chat memory store.

It handles a vast range of inputs: simple text messages, voice notes, video clips, and documents like PDF, CSV, and XLSX. It leverages Google Gemini for image, audio, and video analysis, generating a detailed description that is passed to a conversational AI agent. For structured documents, it employs built-in Extract from File functionality within an n8n node to pull data before sending it to the AI for summarization, ensuring rich context for every interaction. This complex flow demonstrates best practices for building robust, stateful applications with n8n.

How it Works

This n8n workflow operates on two parallel tracks, orchestrated by two different types of n8n trigger nodes:


  1. Telegram Input Flow (Primary Trigger): The Telegram Trigger node initiates the flow upon receiving any message. It first sends a 'Typing…' signal. An Input Message Router checks the message type (Text, Voice, Video, Document, etc.).

Text/Voice/Video Notes: These messages are processed immediately. Voice/Video files are downloaded, and their binary data is sent to a Gemini AI node for analysis/transcription. The resulting text is routed directly to the AI Agent.
Documents (Red Section): Files (PDF, CSV, XML, etc.) are downloaded. A Code node classifies the file extension, routing it through specialized Extract from File nodes. This extraction is converted into a standard data string.
* Media Group Synchronization (Yellow Section): If the input includes a mediagroupid (multiple files), the workflow temporarily stores the file description in the mediagroup PostgreSQL table. If this is the first item in the group, it inserts an entry into the mediaqueue table, triggering the second flow.


  1. Media Group Processing Flow (Secondary Trigger): A Mediaqueue Trigger node constantly monitors the PostgreSQL mediaqueue table. When a new media group ID is detected, it waits a few seconds (ensuring all parts of the group are received), then queries the mediagroup table to retrieve all individual file descriptions and captions.


  1. AI Processing (Purple Section): The consolidated input (either a single text message, a media description, or a full media group summary) is sent to the AI Agent1 node. This LangChain-based n8n node uses the Google Gemini Chat Model and retrieves conversation history from the Postgres Chat Memory node (chathistories table) to maintain context. The AI's final response is passed through a custom Code node that handles MarkdownV2 formatting for Telegram and automatically chunks messages longer than 4096 characters, ensuring delivery integrity. Finally, the response is sent back to the user via the Send a text message1 node.

Installation Guide

To deploy this powerful n8n template, follow these installation steps:


  1. Import the n8n Workflow: Copy the provided JSON data and import it into your self-hosted or cloud n8n instance using the 'New' -> 'Import from JSON' option.

  2. PostgreSQL Setup: This workflow requires a PostgreSQL database instance. You must create two credentials:

Postgres account: Used for the Postgres Trigger (Media Queue), Postgres Chat Memory, and table creation nodes. Configure the connection details (host, port, user, database).

  1. Telegram Setup: Create or use an existing Telegram Bot. You must create one credential:

Telegram account: Provide your Bot Token.

  1. Google Gemini Setup (AI): The workflow relies on Google Gemini (via the LangChain nodes).

* Google Gemini (PaLM) Api account: Provide your API Key for the Gemini service. Ensure your chosen models (gemini-2.5-pro, gemini-2.5-flash) are available.

  1. Initialize Database: Run the workflow manually once (using the 'When clicking ‘Execute workflow’' n8n node) to execute the 'Create Tables' node and initialize the necessary mediagroup, mediaqueue, and chat_histories tables in your PostgreSQL database.

  2. Activate: Save and activate the n8n workflow.

Node Details


  • Telegram Trigger (n8n trigger): The starting point. Listens for new messages. Key setting: updates: message.

  • Postgres Trigger (n8n trigger): Secondary n8n trigger. Monitors the mediaqueue table for new media group IDs requiring consolidated processing.

  • PostgreSQL Node (Create Tables): Runs custom SQL queries (Operation: Execute Query) via a manualTrigger to set up the three required database tables for state management and chat history.

  • Input Message Router (Switch): Core logic n8n node that dynamically routes the workflow based on the incoming Telegram message type (Text, Voice, Document, etc.).

  • Telegram Node (Download File): Used across multiple branches to download binary files (audio, image, documents) referenced by their Telegram fileid.

  • Code Nodes (Fix MIME): Custom JavaScript n8n node used before Gemini multimodal analysis to manually correct or assign standard MIME types to files, improving AI analysis reliability.

  • Google Gemini (LangChain Nodes): Specialized n8n node types (Analyze image, Analyze voice message, Analyze document) that leverage Gemini's multimodal capabilities to generate textual descriptions from binary media files.

  • Extract from File Node: An essential n8n node for parsing structured data from documents like CSV, JSON, ODS, and PDF into consumable text for the AI.

  • AI Agent1 (LangChain Agent): The primary conversational n8n node. It integrates the Google Gemini Chat Model and the Postgres Chat Memory to handle complex, contextual conversations.

  • Code Node (MarkdownV2): Performs critical output cleanup: escapes non-safe characters for Telegram MarkdownV2 and chunks long AI responses into multiple messages to respect Telegram's character limits.

Related n8n Workflows

Free

Nodes: 19 Nodes
Updated: December 26 2025
View all
Created by
John Alejandro SIlva
John Alejandro SIlva

Featured*