WhatsApp Questionnaire Automation with PostgreSQL Storage - n8n Workflow

Use this comprehensive n8n workflow to build a stateful WhatsApp bot. Automatically collect structured questionnaire responses (Anketa) and persist all session data and answers in PostgreSQL.

Workflow Preview

Ready to automate?

Download this n8n workflow template and start using it instantly.

Who is this best for?

Markdown-formatted list of user personas:

Technical marketers or researchers needing a scalable solution for collecting feedback via WhatsApp.
Businesses using PostgreSQL for backend data storage.
Developers looking for advanced examples of state management within an n8n workflow.
Anyone seeking ready-made n8n templates for building conversational interfaces.

Overview

Building stateful conversational bots requires robust logic to track user progress. This specific n8n workflow, often referred to as an 'Anketa' module, provides a complete solution for initiating a multi-step questionnaire via WhatsApp, tracking the user's progress through database lookups, and saving each individual answer. The power of this n8n template lies in its tight integration between the conversational interface (WhatsApp) and the data storage layer (PostgreSQL), ensuring reliable data capture even if the user pauses the conversation. Every incoming message triggers this n8n workflow, which then intelligently decides if the user is starting the survey, responding to a question, or issuing a command. This implementation uses several PostgreSQL operations to maintain the 'Bot Status' effectively.

How it Works

The entire flow is initiated by the WhatsApp Trigger n8n node whenever a new message is received.


  1. Trigger and Initialization: The WhatsApp Trigger receives the inbound message, and the Initialization Set n8n node prepares the input data.

  2. State Retrieval: The workflow calls the Get Bot Status PostgreSQL n8n node to determine the user's current stage in the questionnaire flow.

  3. Flow Definition: The Define Flow Switch n8n node routes the execution based on the retrieved status or the command in the message (e.g., START, ANKETA, or a specific answer).

  4. Starting the Survey (ANKETA): If the command is to start the test, the flow executes Starts Test, uses the Get First Question PostgreSQL n8n node to fetch the first item, sends it via the First Question WhatsApp n8n node, and uses Update Bot Status on ANKETA to set the conversation context.

  5. Handling Responses: When an answer is received, the workflow uses Get prev Answer to check context. The Is Question found? If n8n node verifies the session.

  6. Saving Data and Fetching Next Question: If valid, the current response is added using the Add Answer PostgreSQL n8n node. Then, Get Available Questions fetches the next question.

  7. Looping or Finishing: The Is Questions available? If n8n node checks the list. If true, the next question is sent via the Question WhatsApp n8n node. If false, the survey is complete, triggering the Finish Anketa WhatsApp n8n node, and the status is reset using Update Bot Status on START.

This robust architecture, utilizing key flow control n8n nodes, ensures seamless data collection.

Installation Guide

To set up this powerful n8n workflow, follow these steps:


  1. Import the n8n Workflow: Copy the provided JSON data and paste it into your n8n instance using the 'New' -> 'Import from JSON' option.

  2. WhatsApp Credentials: You must configure the required WhatsApp credentials for the WhatsApp Trigger node and all subsequent WhatsApp sender nodes.

  3. PostgreSQL Credentials: Set up a shared credential for the PostgreSQL nodes. This connection is vital for the workflow's state management. Ensure your database tables are structured correctly to handle 'Bot Status' and 'Answers' as expected by the SQL queries within the PostgreSQL n8n nodes.

  4. Database Structure: Before running, verify that the database structure matches the queries embedded in nodes like Get Bot Status, Upsert Bot Status on START, and Add Answer to ensure the n8n workflow functions correctly.

  5. Activation: Save the n8n template and set the workflow to 'Active' to start listening for incoming WhatsApp messages.

Node Details

This n8n workflow relies heavily on database interaction and flow control:

WhatsApp Trigger (n8n trigger): The entry point. Listens for incoming messages via a registered webhook endpoint.
Postgres Nodes (Multiple): Used for all state management. Key instances include:
Get Bot Status: Queries the database to retrieve the current state of the user's interaction.
Upsert Bot Status on START: Creates or updates the user's session status upon initiating the interaction.
Add Answer / Add prev Answer: Inserts the user's response into the database. Key Configuration: Uses SQL INSERT or UPDATE commands dynamically based on workflow data.
Get First Question / Get Available Questions: Fetches subsequent questionnaire items from the database.
Switch Node (Define Flow, Commands): Essential flow control n8n node. Determines which branch of the automation to execute based on message content or user state.
If Node (Is Question found?, Is Questions available?): Conditional branching n8n node used to determine if the conversation should continue to the next question or terminate.


  • WhatsApp Node (Starts, First Question, Question, Finish Anketa): Responsible for sending structured messages, questions, and completion notifications back to the user.

Related n8n Workflows

Paid

Nodes: 7 Nodes
Updated: December 26 2025
View all
Created by
Andrew
Andrew

Analyst-Developer

Featured*