{
  "name": "Ai-powered Telegram trivia bot with auto question generation & user management",
  "nodes": [
    {
      "id": "d9902b39-9d8d-4109-bfda-0a255c72cf1f",
      "name": "Telegram Trigger",
      "type": "n8n-nodes-base.telegramTrigger",
      "position": [
        -1200,
        -720
      ]
    },
    {
      "id": "78ac5309-7baa-4002-ab26-10c5c1242cd3",
      "name": "Parse Telegram Data",
      "type": "n8n-nodes-base.code",
      "position": [
        -1000,
        -720
      ]
    },
    {
      "id": "a98966f7-c63a-4376-bc50-cddf4764009b",
      "name": "Check Existing User",
      "type": "n8n-nodes-base.nocoDb",
      "position": [
        -780,
        -720
      ]
    },
    {
      "id": "e61286d1-94ed-40f5-ba5c-1211fd8247a6",
      "name": "User Exists?",
      "type": "n8n-nodes-base.if",
      "position": [
        -580,
        -720
      ]
    },
    {
      "id": "77097a18-d05d-4211-9c85-e1bbc821f960",
      "name": "Create New User",
      "type": "n8n-nodes-base.nocoDb",
      "position": [
        -380,
        -520
      ]
    },
    {
      "id": "ebfffcd2-1a37-4aa9-9bd3-052552de448f",
      "name": "Merge User Data",
      "type": "n8n-nodes-base.code",
      "position": [
        -180,
        -740
      ]
    },
    {
      "id": "6803deb8-17bd-42f5-9981-d48f6db3f2e5",
      "name": "Is Command?",
      "type": "n8n-nodes-base.if",
      "position": [
        0,
        -740
      ]
    },
    {
      "id": "79f6d733-2f08-4992-a129-7672674bc28d",
      "name": "Command Router",
      "type": "n8n-nodes-base.switch",
      "position": [
        260,
        -800
      ]
    },
    {
      "id": "e1eeecea-f282-4651-9354-94418aa867e0",
      "name": "Handle Basic Commands",
      "type": "n8n-nodes-base.code",
      "position": [
        1400,
        -900
      ]
    },
    {
      "id": "d5831643-79a8-4671-a90c-c5c01014cb4f",
      "name": "Format Question",
      "type": "n8n-nodes-base.code",
      "position": [
        1220,
        -760
      ]
    },
    {
      "id": "3e96876a-d775-47e5-85ad-a2d3f221a930",
      "name": "Update User Game State",
      "type": "n8n-nodes-base.nocoDb",
      "position": [
        1240,
        -480
      ]
    },
    {
      "id": "1e03df0d-5ead-4f82-8565-a1dd9391dcb1",
      "name": "Get Leaderboard",
      "type": "n8n-nodes-base.nocoDb",
      "position": [
        660,
        -440
      ]
    },
    {
      "id": "ea723525-a09e-429e-86b2-c4b13db0f4b6",
      "name": "Format Leaderboard",
      "type": "n8n-nodes-base.code",
      "position": [
        1420,
        -280
      ]
    },
    {
      "id": "4dfc640c-2b70-4265-839c-f9ec296a7982",
      "name": "Valid Answer?",
      "type": "n8n-nodes-base.if",
      "position": [
        280,
        -280
      ]
    },
    {
      "id": "e7b6d610-667d-4317-83c8-64ae9e1b9e7d",
      "name": "Get Current Question",
      "type": "n8n-nodes-base.nocoDb",
      "position": [
        660,
        -260
      ]
    },
    {
      "id": "70cd94a3-d8c6-4a0c-a236-008808887ce8",
      "name": "Process Answer",
      "type": "n8n-nodes-base.code",
      "position": [
        880,
        -240
      ]
    },
    {
      "id": "06af6d12-2ce5-4092-9f20-d8a7cb6a1500",
      "name": "Update User Stats",
      "type": "n8n-nodes-base.nocoDb",
      "position": [
        1060,
        160
      ]
    },
    {
      "id": "bc1d784f-e937-40f2-ae81-a2e70c0c73fb",
      "name": "Handle Unknown Text",
      "type": "n8n-nodes-base.code",
      "position": [
        1240,
        340
      ]
    },
    {
      "id": "bbf362a8-f2da-4023-bce1-f454263de631",
      "name": "Telegram",
      "type": "n8n-nodes-base.telegram",
      "position": [
        1780,
        -265
      ]
    },
    {
      "id": "1f3c7039-bbd7-43b8-a1f7-23f3aaac84a7",
      "name": "Merge",
      "type": "n8n-nodes-base.merge",
      "position": [
        1420,
        -460
      ]
    },
    {
      "id": "8986f8aa-6051-4cf8-a1c9-a3c7dfdf0827",
      "name": "OpenAI",
      "type": "@n8n/n8n-nodes-langchain.openAi",
      "position": [
        -640,
        -20
      ]
    },
    {
      "id": "50ef158d-f86a-4292-8c46-967eeb3db772",
      "name": "NocoDB",
      "type": "n8n-nodes-base.nocoDbTool",
      "position": [
        -540,
        200
      ]
    },
    {
      "id": "6efe0704-ce7c-436b-89e7-d783733e5af0",
      "name": "Daily Question Generator",
      "type": "n8n-nodes-base.scheduleTrigger",
      "position": [
        -1080,
        -20
      ]
    },
    {
      "id": "a6229303-f5ee-47b5-999d-f634b78a21ac",
      "name": "Get Possible Categories",
      "type": "n8n-nodes-base.code",
      "position": [
        -860,
        -20
      ]
    },
    {
      "id": "38f5bce5-9ba9-4b2c-9278-cc42a6cd5ad0",
      "name": "Send New Questions Available Notification",
      "type": "n8n-nodes-base.telegram",
      "position": [
        -260,
        -20
      ]
    },
    {
      "id": "a6a6a589-515b-4641-8943-8a508f1c56c6",
      "name": "Merge1",
      "type": "n8n-nodes-base.merge",
      "position": [
        1400,
        -80
      ]
    },
    {
      "id": "84013da6-ddef-47b7-aa81-544fe5c15c16",
      "name": "Aggregate",
      "type": "n8n-nodes-base.aggregate",
      "position": [
        1260,
        -280
      ]
    },
    {
      "id": "7ed0881f-3ca0-4e2e-ad6f-4db3c311cf86",
      "name": "Get User History",
      "type": "n8n-nodes-base.nocoDb",
      "position": [
        660,
        -640
      ]
    },
    {
      "id": "6f8e4bee-6aea-421b-96e8-63a4e9c41b2d",
      "name": "Mark Question As Answered",
      "type": "n8n-nodes-base.nocoDb",
      "position": [
        1100,
        -60
      ]
    },
    {
      "id": "27e9643b-03d4-44f8-b18c-a7991ddac076",
      "name": "Aggregate1",
      "type": "n8n-nodes-base.aggregate",
      "position": [
        840,
        -640
      ]
    },
    {
      "id": "1f03aba3-2bd0-4c32-bf17-cf5380c58c54",
      "name": "Get Random Question",
      "type": "n8n-nodes-base.nocoDb",
      "position": [
        1020,
        -640
      ]
    },
    {
      "id": "a8f0472f-25db-470b-8c65-58cadc50b3c3",
      "name": "Database Setup Instructions",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -2000,
        -1240
      ],
      "parameters": {
        "width": 600,
        "height": 940,
        "content": "## 📊 **NocoDB Database Setup Required**\n\n### **Table 1: Users**\n- `Id` - Auto Number (Primary Key)\n- `telegram_id` - Number (Unique)\n- `username` - Single Line Text\n- `first_name` - Single Line Text  "
      }
    },
    {
      "id": "3e6758f9-064a-44fa-bbb3-8ab77fcd12ca",
      "name": "AI Generation System",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1160,
        200
      ],
      "parameters": {
        "width": 480,
        "height": 440,
        "content": "## 🤖 **AI Question Generation System**\n\n**Purpose**: Automatically generates fresh trivia questions daily\n\n**Schedule**: Runs once per day\n**Output**: 5 questions per category (8 categories = 40 quest"
      }
    },
    {
      "id": "022a4501-5b95-4a51-a9b3-cab2e50b9464",
      "name": "Message Processing",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1180,
        -520
      ],
      "parameters": {
        "width": 400,
        "height": 320,
        "content": "## 📨 **Message Processing Pipeline**\n\n**Step 1**: Telegram webhook receives message\n**Step 2**: Parse user data and message content\n**Step 3**: Check if user exists in database\n**Step 4**: Create new "
      }
    },
    {
      "id": "846f7032-c510-4e5f-b04d-28cc38ea172e",
      "name": "Smart Question System",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        440,
        -1240
      ],
      "parameters": {
        "width": 580,
        "height": 380,
        "content": "## 🎮 **Smart Question System**\n\n**Features**:\n✅ **No Repeats**: Users never see correctly answered questions again\n✅ **History Tracking**: Every answer is logged\n✅ **Dynamic Selection**: Always fresh "
      }
    },
    {
      "id": "68a50473-cab9-4a3a-818f-1b37db782c3a",
      "name": "Leaderboard System",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1680,
        -860
      ],
      "parameters": {
        "width": 380,
        "height": 340,
        "content": "## 📊 **Leaderboard System**\n\n**Features**:\n- Top 10 players by score\n- Shows current user's position\n- Real-time ranking updates\n- Emoji medals for top 3\n\n**Display**:\n🥇 1st place\n🥈 2nd place  \n🥉 3rd "
      }
    },
    {
      "id": "5930ed78-fd46-402d-845c-653a680332c4",
      "name": "Answer Processing",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        360,
        -40
      ],
      "parameters": {
        "width": 320,
        "height": 460,
        "content": "## ✅ **Answer Processing Engine**\n\n**Input Validation**:\n- Only accepts A, B, C, D answers\n- Must be in \"waiting_answer\" state\n- Case-insensitive processing\n\n**Scoring System**:\n⭐ = 1 point\n⭐⭐ = 2 poi"
      }
    },
    {
      "id": "7edc0a6b-42a2-4290-b683-99a09e8c764d",
      "name": "Game State Management",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -900,
        -1140
      ],
      "parameters": {
        "width": 480,
        "height": 300,
        "content": "## 🔄 **Game State Management**\n\n**States**:\n- `idle` - Ready for new question\n- `waiting_answer` - Question active, awaiting response\n\n**Tracking**:\n- Current question ID\n- When question was asked\n- U"
      }
    },
    {
      "id": "0e5219c5-af7a-4c9f-b984-a33abdc2353e",
      "name": "Response System",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1980,
        -400
      ],
      "parameters": {
        "width": 340,
        "height": 400,
        "content": "## 📤 **Response System**\n\n**Features**:\n- Markdown formatting for rich messages\n- Emoji integration for visual appeal\n- Consistent message structure\n- Error handling and fallbacks\n\n**Message Types**:\n"
      }
    },
    {
      "id": "8d1c5306-cb5a-4631-9dac-23932f7c7e80",
      "name": "History Tracking System",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -220,
        -1240
      ],
      "parameters": {
        "width": 600,
        "height": 380,
        "content": "## 🎲 **Question History Tracking**\n\n**Purpose**: Ensures users never see the same correctly answered question twice\n\n**Process**:\n1. Get user's answer history\n2. Collect all correctly answered questio"
      }
    },
    {
      "id": "4ea2a44e-c741-4d5f-ad4b-870ee562c394",
      "name": "Data Flow Overview",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1220,
        -1340
      ],
      "parameters": {
        "width": 300,
        "height": 500,
        "content": "## 🔄 **Data Flow Summary**\n\n**Input**: Telegram message\n**Processing**: \n1. Parse & validate user\n2. Route command or answer\n3. Process business logic\n4. Update database\n5. Format response\n\n**Output**"
      }
    }
  ],
  "connections": {
    "Merge": {
      "main": [
        [
          {
            "node": "Telegram",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Merge1": {
      "main": [
        [
          {
            "node": "Telegram",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "NocoDB": {
      "ai_tool": [
        [
          {
            "node": "OpenAI",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI": {
      "main": [
        [
          {
            "node": "Send New Questions Available Notification",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Aggregate": {
      "main": [
        [
          {
            "node": "Format Leaderboard",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Aggregate1": {
      "main": [
        [
          {
            "node": "Get Random Question",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Is Command?": {
      "main": [
        [
          {
            "node": "Command Router",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Valid Answer?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "User Exists?": {
      "main": [
        [
          {
            "node": "Merge User Data",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Create New User",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Valid Answer?": {
      "main": [
        [
          {
            "node": "Get Current Question",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Handle Unknown Text",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Command Router": {
      "main": [
        [
          {
            "node": "Handle Basic Commands",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Handle Basic Commands",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Handle Basic Commands",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Handle Basic Commands",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Get User History",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Get Leaderboard",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Process Answer": {
      "main": [
        [
          {
            "node": "Merge1",
            "type": "main",
            "index": 0
          },
          {
            "node": "Update User Stats",
            "type": "main",
            "index": 0
          },
          {
            "node": "Mark Question As Answered",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create New User": {
      "main": [
        [
          {
            "node": "Merge User Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Format Question": {
      "main": [
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Leaderboard": {
      "main": [
        [
          {
            "node": "Aggregate",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Merge User Data": {
      "main": [
        [
          {
            "node": "Is Command?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get User History": {
      "main": [
        [
          {
            "node": "Aggregate1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Telegram Trigger": {
      "main": [
        [
          {
            "node": "Parse Telegram Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Update User Stats": {
      "main": [
        [
          {
            "node": "Merge1",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "Format Leaderboard": {
      "main": [
        [
          {
            "node": "Telegram",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check Existing User": {
      "main": [
        [
          {
            "node": "User Exists?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Random Question": {
      "main": [
        [
          {
            "node": "Format Question",
            "type": "main",
            "index": 0
          },
          {
            "node": "Update User Game State",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Handle Unknown Text": {
      "main": [
        [
          {
            "node": "Telegram",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Parse Telegram Data": {
      "main": [
        [
          {
            "node": "Check Existing User",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Current Question": {
      "main": [
        [
          {
            "node": "Process Answer",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Handle Basic Commands": {
      "main": [
        [
          {
            "node": "Telegram",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Update User Game State": {
      "main": [
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "Get Possible Categories": {
      "main": [
        [
          {
            "node": "OpenAI",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Daily Question Generator": {
      "main": [
        [
          {
            "node": "Get Possible Categories",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}