Telegram + PostgreSQL: every chat logged and routed
Support chats get messy fast. A customer sends a Telegram message, someone replies late (or not at all), and later you can’t reconstruct what happened because the context is scattered across devices and people.
This Telegram Postgres logging setup hits support leads first, honestly. But agency owners managing client inboxes and ops managers trying to standardize responses feel the same drag. You get cleaner history, fewer missed follow-ups, and replies that stay consistent even when your team is tired.
This workflow routes every incoming message through rules, optional voice output, and a database log in PostgreSQL. You’ll see what it automates, what it changes day to day, and what you need to run it reliably.
How This Automation Works
The full n8n workflow, from trigger to final output:
n8n Workflow Template: Telegram + PostgreSQL: every chat logged and routed
flowchart LR
subgraph sg0["Flow Flow"]
direction LR
n0@{ icon: "mdi:swap-horizontal", form: "rounded", label: "Media Switch", pos: "b", h: 48 }
n1@{ icon: "mdi:play-circle", form: "rounded", label: "Flow Trigger", pos: "b", h: 48 }
n2@{ icon: "mdi:swap-vertical", form: "rounded", label: "Input", pos: "b", h: 48 }
n3@{ icon: "mdi:play-circle", form: "rounded", label: "Test Trigger", pos: "b", h: 48 }
n4@{ icon: "mdi:swap-vertical", form: "rounded", label: "Test Fields", pos: "b", h: 48 }
n5["<div style='background:#f5f5f5;padding:10px;border-radius:8px;display:inline-block;border:1px solid #e0e0e0'><img src='https://flowpast.com/wp-content/uploads/n8n-workflow-icons/httprequest.dark.svg' width='40' height='40' /></div><br/>Download Minimax Audio"]
n6["<div style='background:#f5f5f5;padding:10px;border-radius:8px;display:inline-block;border:1px solid #e0e0e0'><img src='https://flowpast.com/wp-content/uploads/n8n-workflow-icons/telegram.svg' width='40' height='40' /></div><br/>Telegram Voice Output"]
n7@{ icon: "mdi:swap-horizontal", form: "rounded", label: "If Provider No", pos: "b", h: 48 }
n8["<div style='background:#f5f5f5;padding:10px;border-radius:8px;display:inline-block;border:1px solid #e0e0e0'><img src='https://flowpast.com/wp-content/uploads/n8n-workflow-icons/redis.svg' width='40' height='40' /></div><br/>Provider Cache"]
n9["<div style='background:#f5f5f5;padding:10px;border-radius:8px;display:inline-block;border:1px solid #e0e0e0'><img src='https://flowpast.com/wp-content/uploads/n8n-workflow-icons/redis.svg' width='40' height='40' /></div><br/>Save Provider Cache"]
n10@{ icon: "mdi:swap-horizontal", form: "rounded", label: "If Provider Cache", pos: "b", h: 48 }
n11@{ icon: "mdi:swap-vertical", form: "rounded", label: "Provider", pos: "b", h: 48 }
n12["<div style='background:#f5f5f5;padding:10px;border-radius:8px;display:inline-block;border:1px solid #e0e0e0'><img src='https://flowpast.com/wp-content/uploads/n8n-workflow-icons/postgres.svg' width='40' height='40' /></div><br/>Load Provider Data"]
n13@{ icon: "mdi:swap-horizontal", form: "rounded", label: "If Provider Voice", pos: "b", h: 48 }
n14@{ icon: "mdi:swap-horizontal", form: "rounded", label: "Switch", pos: "b", h: 48 }
n15["<div style='background:#f5f5f5;padding:10px;border-radius:8px;display:inline-block;border:1px solid #e0e0e0'><img src='https://flowpast.com/wp-content/uploads/n8n-workflow-icons/postgres.svg' width='40' height='40' /></div><br/>Create Chat Log Output"]
n16["<div style='background:#f5f5f5;padding:10px;border-radius:8px;display:inline-block;border:1px solid #e0e0e0'><img src='https://flowpast.com/wp-content/uploads/n8n-workflow-icons/postgres.svg' width='40' height='40' /></div><br/>Create Chat Log Input"]
n17@{ icon: "mdi:swap-horizontal", form: "rounded", label: "If Input", pos: "b", h: 48 }
n18@{ icon: "mdi:swap-vertical", form: "rounded", label: "Output", pos: "b", h: 48 }
n19@{ icon: "mdi:swap-horizontal", form: "rounded", label: "Media Switch1", pos: "b", h: 48 }
n20@{ icon: "mdi:swap-vertical", form: "rounded", label: "Chinese,Yue", pos: "b", h: 48 }
n21["<div style='background:#f5f5f5;padding:10px;border-radius:8px;display:inline-block;border:1px solid #e0e0e0'><img src='https://flowpast.com/wp-content/uploads/n8n-workflow-icons/httprequest.dark.svg' width='40' height='40' /></div><br/>Minimax TTS"]
n22@{ icon: "mdi:swap-vertical", form: "rounded", label: "Chinese", pos: "b", h: 48 }
n23@{ icon: "mdi:swap-vertical", form: "rounded", label: "Japanese", pos: "b", h: 48 }
n24@{ icon: "mdi:swap-vertical", form: "rounded", label: "English", pos: "b", h: 48 }
n25@{ icon: "mdi:swap-horizontal", form: "rounded", label: "If Reply", pos: "b", h: 48 }
n26["<div style='background:#f5f5f5;padding:10px;border-radius:8px;display:inline-block;border:1px solid #e0e0e0'><img src='https://flowpast.com/wp-content/uploads/n8n-workflow-icons/telegram.svg' width='40' height='40' /></div><br/>Telegram Reply Output"]
n27["<div style='background:#f5f5f5;padding:10px;border-radius:8px;display:inline-block;border:1px solid #e0e0e0'><img src='https://flowpast.com/wp-content/uploads/n8n-workflow-icons/telegram.svg' width='40' height='40' /></div><br/>Telegram Output"]
n28["<div style='background:#f5f5f5;padding:10px;border-radius:8px;display:inline-block;border:1px solid #e0e0e0'><img src='https://flowpast.com/wp-content/uploads/n8n-workflow-icons/code.svg' width='40' height='40' /></div><br/>Parse Cache"]
n2 --> n7
n14 --> n20
n14 --> n22
n14 --> n23
n14 --> n24
n22 --> n21
n24 --> n21
n17 --> n16
n17 --> n15
n25 --> n26
n25 --> n27
n23 --> n21
n11 --> n0
n11 --> n17
n20 --> n21
n21 --> n5
n28 --> n11
n4 --> n2
n1 --> n2
n0 --> n13
n0 --> n18
n3 --> n4
n19 --> n25
n19 --> n18
n7 --> n8
n7 --> n19
n8 --> n10
n27 --> n18
n10 --> n28
n10 --> n12
n13 --> n14
n13 --> n25
n12 --> n9
n9 --> n11
n16 --> n15
n26 --> n18
n6 --> n25
n5 --> n6
n5 --> n25
end
%% Styling
classDef trigger fill:#e8f5e9,stroke:#388e3c,stroke-width:2px
classDef ai fill:#e3f2fd,stroke:#1976d2,stroke-width:2px
classDef aiModel fill:#e8eaf6,stroke:#3f51b5,stroke-width:2px
classDef decision fill:#fff8e1,stroke:#f9a825,stroke-width:2px
classDef database fill:#fce4ec,stroke:#c2185b,stroke-width:2px
classDef api fill:#fff3e0,stroke:#e65100,stroke-width:2px
classDef code fill:#f3e5f5,stroke:#7b1fa2,stroke-width:2px
classDef disabled stroke-dasharray: 5 5,opacity: 0.5
class n1,n3 trigger
class n0,n7,n10,n13,n14,n17,n19,n25 decision
class n8,n9,n12,n15,n16 database
class n5,n21 api
class n28 code
classDef customIcon fill:none,stroke:none
class n5,n6,n8,n9,n12,n15,n16,n21,n26,n27,n28 customIcon
The Problem: Telegram support chats lack reliable context
Telegram is great for fast conversations, but it’s terrible as a system of record. Messages come in across time zones, multiple agents jump in, and suddenly the “source of truth” is whoever remembers what was promised. Then a customer asks, “Can you resend that detail?” and you’re scrolling through days of chat, trying to find one line. It’s not just slow. It creates inconsistent replies, missed handoffs, and awkward follow-ups that make you look disorganized.
It adds up fast. Here’s where it breaks down in real teams.
- Important messages get buried, so replies happen late or not at all when the inbox gets busy.
- Without a PostgreSQL log, you can’t easily audit who said what when a dispute pops up.
- Voice replies are a nice touch, but generating and sending them manually is a hassle you stop doing after day two.
- Provider rules (language, voice availability, routing) live in people’s heads instead of a database you can update once.
The Solution: Route replies and log every chat to PostgreSQL
This workflow acts like a message “output layer” for Telegram. It receives a message payload from another workflow (or a test trigger), looks up the right provider configuration from PostgreSQL (with Redis caching to keep it snappy), then decides how to respond. If voice output is enabled for that provider, it generates a TTS voice clip through the MiniMax API in the correct language, fetches the audio, and sends it to Telegram. It also sends the text reply (either as a normal message or a threaded reply) and writes both the input and output into PostgreSQL line by line so you can search, audit, or reuse the data later.
The workflow starts when a message is handed in through an execute-subworkflow trigger (or the built-in chat test trigger). It then validates the payload, loads provider rules from cache or Postgres, and routes to text-only or text-plus-voice. Finally, it dispatches to Telegram and inserts chat logs into Postgres.
What You Get: Automation vs. Results
| What This Workflow Automates | Results You’ll Get |
|---|---|
|
|
Example: What This Looks Like
Say your Telegram support line gets 40 messages a day. Manually, logging the key details and sending a consistent reply might take about 3 minutes per message once you include context switching, copying text, and writing notes. That’s roughly 2 hours daily. With this workflow, the “work” is basically reviewing and improving your provider rules in Postgres, while n8n handles routing, optional voice creation, and inserts both input/output logs automatically. You still answer customers. You just stop doing the admin glue.
What You’ll Need
- n8n instance (try n8n Cloud free)
- Self-hosting option if you prefer (Hostinger works well)
- Telegram for sending messages and replies
- PostgreSQL to store provider rules and chat logs
- Redis for caching provider lookups
- MiniMax API key (get it from your MiniMax dashboard) for optional TTS voice
- OpenAI API key (get it from the OpenAI platform) if you use the AI Agent/Chat Model nodes
Skill level: Advanced. You’ll connect multiple credentials, set up database tables, and be comfortable debugging payloads when a branch doesn’t match.
Don’t want to set this up yourself? Talk to an automation expert (free 15-minute consultation).
How It Works
A message comes in from a parent workflow. The “Workflow Launch Trigger” (execute sub-workflow trigger) waits for a payload, and the Input Mapper normalizes fields so downstream logic stays predictable.
Provider settings are loaded and cached. The workflow checks if the provider exists, looks it up in Redis, and falls back to PostgreSQL if it’s not cached yet. Once found, it saves the provider record back into Redis so future runs don’t keep hitting the database.
The response path is chosen. Switch and If nodes route based on media type and whether voice output is enabled. If voice is needed, the Language Router selects the right template (English, Japanese, Chinese, Yue), then MiniMax TTS creates audio and the workflow fetches the result for delivery.
Telegram gets the final output, and Postgres gets the receipt. The workflow sends either a normal Telegram message or a reply in-thread, optionally sends a voice note, and inserts both the chat input and output rows into PostgreSQL so your history is searchable later.
You can easily modify the provider lookup logic to pull from a different table based on your needs. See the full implementation guide below for customization options.
Step-by-Step Implementation Guide
Step 1: Configure the Execute Workflow Trigger
Set up the workflow entry points so production executions and chat tests both normalize input data into a shared format.
- Add and position Workflow Launch Trigger as the primary trigger for external workflow calls.
- Add Chat Test Trigger for interactive testing and connect it to Test Field Mapper.
- Connect Test Field Mapper to Input Mapper so both triggers feed the same input schema.
- Connect Workflow Launch Trigger to Input Mapper to keep the production path aligned with test data.
Step 2: Connect Provider Cache and Database Services
Configure provider lookup and caching so the workflow can resolve provider details efficiently.
- From Input Mapper, connect to Provider Missing Check to determine whether provider data exists.
- Route the missing-provider path to Provider Cache Lookup and then to Provider Cache Check.
- Connect the cache-hit path from Provider Cache Check to Parse Cache Data, then to Provider Mapper.
- Connect the cache-miss path from Provider Cache Check to Load Provider Records, then to Cache Provider Save, and finally to Provider Mapper.
- Ensure Provider Mapper outputs to both Media Route and Input Presence Check in parallel.
Credential Required: Connect your Redis credentials for Provider Cache Lookup and Cache Provider Save, and connect your Postgres credentials for Load Provider Records.
Step 3: Set Up Media Routing and Language Selection
Route media handling and choose the correct language settings before requesting audio from Minimax.
- Connect Media Route to Provider Voice Check to determine whether to generate voice output.
- From Provider Voice Check, route voice-enabled requests to Language Router.
- Connect Language Router to Chinese Yue Set, Chinese Set, Japanese Set, and English Set as defined in the workflow.
- Connect each language set node (Chinese Yue Set, Chinese Set, Japanese Set, English Set) to Minimax TTS Request.
- Connect Minimax TTS Request to Fetch Minimax Audio to retrieve the audio file.
- Connect Fetch Minimax Audio to Telegram Voice Dispatch for voice delivery.
Credential Required: Connect the appropriate HTTP authentication or API credentials in Minimax TTS Request and Fetch Minimax Audio for your Minimax endpoint.
Step 4: Configure Reply Logic and Telegram Outputs
Control whether replies are required and dispatch either voice or text messages via Telegram.
- Connect Telegram Voice Dispatch to Reply Needed Check to determine whether to respond with a follow-up.
- Route the non-voice path from Provider Voice Check to Reply Needed Check as defined in the workflow.
- Connect Reply Needed Check to Telegram Reply Dispatch for direct replies, and to Telegram Message Dispatch for standard messages.
- Send both Telegram Reply Dispatch and Telegram Message Dispatch to Final Output Set to standardize the response.
- Ensure Alt Media Route connects to Reply Needed Check for alternate media routing.
Credential Required: Connect your Telegram credentials for Telegram Voice Dispatch, Telegram Reply Dispatch, and Telegram Message Dispatch.
Step 5: Add Input and Output Logging
Log incoming requests and resulting outputs to your database for observability and analytics.
- From the parallel branch, connect Provider Mapper to Input Presence Check.
- Connect Input Presence Check to Log Input Insert to store inbound payload details.
- Chain Log Input Insert to Log Output Insert to capture the workflow response metadata.
Credential Required: Connect your Postgres credentials for Log Input Insert and Log Output Insert.
Step 6: Add Error Handling
This workflow does not include a dedicated error-handling branch, so monitoring should be handled externally or by enabling node-level error outputs.
- Review nodes with error output settings like Fetch Minimax Audio and Minimax TTS Request to decide if you want to add custom error routing.
- Consider adding a separate error workflow in n8n settings if you need centralized failure alerts.
Step 7: Test and Activate Your Workflow
Validate both the chat test path and the production trigger before enabling the workflow.
- Run a manual test using Chat Test Trigger and verify Final Output Set receives the expected response.
- Trigger Workflow Launch Trigger from a calling workflow and confirm provider data flows through Provider Mapper and out to Telegram nodes.
- A successful execution should show the audio dispatch path reaching Telegram Voice Dispatch or the text reply path reaching Telegram Reply Dispatch/Telegram Message Dispatch.
- When satisfied, toggle the workflow to Active in n8n for production use.
Common Gotchas
- Telegram credentials can expire or need specific permissions. If things break, check your n8n credential settings and the bot’s access to the target chat first.
- If you’re using Wait nodes or external rendering, processing times vary. Bump up the wait duration if downstream nodes fail on empty responses.
- Default prompts in AI nodes are generic. Add your brand voice early or you’ll be editing outputs forever.
Frequently Asked Questions
Plan on about 1–2 hours once your database and credentials are ready.
No, but you will need to be comfortable wiring credentials and verifying database fields. A little SQL confidence helps when you import the required tables.
Yes. n8n has a free self-hosted option and a free trial on n8n Cloud. Cloud plans start at $20/month for higher volume. You’ll also need to factor in MiniMax TTS API usage (it varies by audio length) and any OpenAI usage if you keep the AI nodes enabled.
Two options: n8n Cloud (managed, easiest setup) or self-hosting on a VPS. For self-hosting, Hostinger VPS is affordable and handles n8n well. Self-hosting gives you unlimited executions but requires basic server management.
Yes, but you’ll swap the Telegram dispatch nodes (Telegram Message Dispatch, Telegram Reply Dispatch, Telegram Voice Dispatch) for your WhatsApp provider’s send-message actions. Most teams also adjust the media routing switches (Media Route and Alt Media Route) so the “reply vs message” behavior matches the new channel. The PostgreSQL logging pieces can stay the same, which is the nice part.
Usually it’s the bot token or chat permissions. Regenerate or reselect the Telegram credentials in n8n, then confirm the bot is actually allowed to post in the target chat or channel. If replies fail but messages succeed, double-check that the reply metadata (message IDs) is being passed through your Input Mapper. Rate limiting can also show up if you blast out messages during tests.
A lot—practically, it’s limited by your n8n execution capacity and how fast Postgres and the TTS API respond.
For this workflow, n8n is a better fit because it handles branching logic (media routing, “reply needed” checks, language switches) without turning into a spaghetti bill. It also supports a real database-first design, which matters when you want PostgreSQL to be the system of record instead of a hidden step. Redis caching is another big win if you’re looking up provider settings frequently. Zapier or Make can still work for lightweight chat notifications, but voice generation plus logging plus routing gets expensive and awkward fast. If you’re on the fence, Talk to an automation expert and we’ll map the simplest stack for your volume.
Once this is running, Telegram becomes a proper support channel with memory, routing, and receipts in PostgreSQL. Set it up once, then spend your time on real conversations instead of chasing missing context.
Need Help Setting This Up?
Our automation experts can build and customize this workflow for your specific needs. Free 15-minute consultation—no commitment required.