Telegram Module Quiz Engine with Persistent PostgreSQL Storage - n8n Workflow

Build a comprehensive Telegram quiz bot using this robust n8n workflow. Automate question delivery, store results in PostgreSQL, and manage user progress with this advanced n8n template.

Workflow Preview

Ready to automate?

Download this n8n workflow template and start using it instantly.

Who is this best for?

Educators and Trainers: Who need to administer automated quizzes or assessments.
Developers: Seeking advanced examples of database state management within an n8n workflow.
Automation Specialists: Looking for a complex n8n template combining messaging bots and persistent storage.
Marketing Teams: Deploying interactive content campaigns via Telegram.

Overview

This sophisticated n8n workflow solves the challenge of running interactive, stateful quizzes within a stateless environment like a messaging application. By leveraging PostgreSQL, this n8n automation tracks user progress, randomizes questions, accepts answers, and calculates final scores accurately.

The core value provided by this n8n template is transforming a simple Telegram bot into a complex quiz engine. Every action, from starting the test to submitting an answer, is managed by a specific n8n node, ensuring data integrity. This approach guarantees that even if a user pauses the quiz, their progress is maintained within the database, ready to be resumed when the n8n trigger fires again.

How it Works

This n8n workflow initiates when the Telegram Trigger n8n node receives an incoming message (a command or button click).


  1. Trigger and Initialization: The Telegram Trigger activates, followed by Variables TG and Initialization Set nodes, which extract critical data like Chat ID and message content.

  2. Routing Commands: The Is Start? If node checks for a welcome command. If not starting, the flow routes through the main Switch node to distinguish between textual commands (like requesting a list of tests) and inline button presses (like selecting an answer).

  3. Displaying Tests: If the user requests tests, Get Tests (a PostgreSQL n8n node) retrieves the available modules. Data manipulation nodes (Union Number with Question, Union list) format this list before the List tests Telegram n8n node presents it to the user.

  4. Starting a Quiz: When a user selects a quiz, the flow hits the For Tests Switch, then Get Test (PostgreSQL), and sends a confirmation using a Telegram n8n node.

  5. Question Loop: Get Questions AND Answers fetches the quiz content. The Randomize questions Sort node shuffles the order, enhancing the testing experience. The Variables and Code nodes prepare the item structure.

  6. Answer Submission: The Upsert pred Answer PostgreSQL n8n node records the question state. The Question Telegram n8n node sends the question and awaits a response. The Wait n8n node pauses the execution until the user replies.

  7. Progress Tracking and Continuation: The response updates the user's answer via the Update Answer PostgreSQL n8n node. Get Non-Answered Questions checks for remaining items. The Any questions? If node decides the next step: if true, the n8n workflow loops back to send the next question; if false, the quiz is complete.

  8. Final Score: Upon completion, Calculate answers uses PostgreSQL logic to score the quiz, and the Result Test Telegram n8n node delivers the final results to the user.

Installation Guide

To deploy this comprehensive n8n workflow, follow these steps:


  1. Import the n8n template: Copy the provided JSON into your n8n instance using the 'New' -> 'Import from JSON' option.

  2. Telegram Credentials: You must configure a credential for the Telegram service. This is required for the Telegram Trigger n8n trigger and all Telegram output n8n node instances (Test, Question, List tests, etc.).

  3. PostgreSQL Credentials: Set up the required PostgreSQL credentials. This n8n workflow uses numerous PostgreSQL n8n node operations for state management, scoring, and data retrieval. Ensure your database tables (for tests, questions, and user answers) are set up correctly to match the SQL queries used within the PostgreSQL n8n node parameters.

  4. Webhook Configuration: For the Telegram Trigger to function, ensure your n8n instance is accessible externally and the Telegram bot is configured to send updates to the generated webhook URL.

  5. Activate: Save the n8n workflow and toggle the status to 'Active'.

Node Details

This advanced n8n workflow utilizes dozens of nodes to manage complex state:

Telegram Trigger (n8n trigger): The entry point. Listens for user input (messages, commands, button clicks) from the configured Telegram bot.
PostgreSQL Node (Get Tests, Update Answer, Calculate answers, etc.): The backbone of this n8n template. Used for persistent storage, retrieving quiz questions, saving user responses, and calculating final scores using database-side logic. Essential for maintaining the state throughout the multi-step quiz.
Switch Node (Commands, Buttons, For Tests): Core flow control n8n node used extensively to route incoming Telegram data based on whether it is a user command, an inline button click, or a specific test action.
Set Node (Variables TG, Initialization): Used to define and normalize key variables, such as user IDs and message metadata, at the start of the n8n workflow.
If Node (Is Start?, Any questions?): Determines the execution path. For instance, Any questions? checks if the quiz is finished before sending the final results.
Sort Node (Randomize questions): Ensures that questions retrieved from the database are presented in a randomized order before being sent by the Telegram n8n node.


  • Wait Node: Used after sending a question via the Question Telegram n8n node to pause the n8n workflow execution until the required user response is received, enabling synchronous interaction within the asynchronous chat environment.

Related n8n Workflows

Paid

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

Analyst-Developer

Featured*