{
  "nodes": [
    {
      "id": "588b691b-8305-4761-89b2-e5e1d2d0699e",
      "name": "Telegram Trigger",
      "type": "n8n-nodes-base.telegramTrigger",
      "position": [
        -752,
        656
      ],
      "parameters": {
        "updates": [
          "message"
        ],
        "additionalFields": {}
      },
      "typeVersion": 1.2
    },
    {
      "id": "2a241435-d6d1-41ba-8432-d44f4d837ef3",
      "name": "Google Gemini Chat Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatGoogleGemini",
      "position": [
        224,
        1184
      ],
      "parameters": {
        "options": {}
      },
      "typeVersion": 1
    },
    {
      "id": "802a6326-3858-4d8f-9d85-d2e7dc78e80c",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -816,
        496
      ],
      "parameters": {
        "color": 4,
        "height": 288,
        "content": "## STEP 1: RECEIVE USER MESSAGE\nThis workflow starts when a user sends a message to the Telegram bot."
      },
      "typeVersion": 1
    },
    {
      "id": "ab604f07-25fc-4580-af0d-07f26dd120eb",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        80,
        304
      ],
      "parameters": {
        "color": 5,
        "width": 272,
        "height": 176,
        "content": "## PATH FOR NEW USERS\nIf the user is not found in the database, this path runs. It creates a new user record and sends a one-time welcome message."
      },
      "typeVersion": 1
    },
    {
      "id": "08891444-00d4-46f0-b539-7aa3298b84ce",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -384,
        496
      ],
      "parameters": {
        "color": 4,
        "width": 336,
        "height": 288,
        "content": "## STEP 2: CHECK IF USER IS NEW\nLooks up the user's Telegram ID in the database (Supabase). The 'If' node then checks if the user was found."
      },
      "typeVersion": 1
    },
    {
      "id": "2ec0132b-6c2f-444c-834c-219ccd785996",
      "name": "Sticky Note3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        432,
        1152
      ],
      "parameters": {
        "color": 3,
        "height": 208,
        "content": "## PATH FOR EXISTING USERS\nIf the user already exists, their question is processed by the AI to find an answer."
      },
      "typeVersion": 1
    },
    {
      "id": "d03a256a-7bbf-493c-83bd-4e2fa5738e23",
      "name": "Sticky Note4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        48,
        736
      ],
      "parameters": {
        "color": 4,
        "width": 432,
        "height": 336,
        "content": "## STEP 3: GENERATE AI ANSWER\nThe user's question is sent to the Google Gemini model. The AI Agent uses its knowledge base to generate a relevant answer for the FAQ."
      },
      "typeVersion": 1
    },
    {
      "id": "76eaef61-75d6-4ce3-8d11-436fe4e75665",
      "name": "Sticky Note5",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        528,
        768
      ],
      "parameters": {
        "color": 4,
        "width": 368,
        "height": 272,
        "content": "## STEP 4: SEND AI RESPONSE\nThe final answer generated by the AI is sent back to the user via Telegram."
      },
      "typeVersion": 1
    },
    {
      "id": "e3c8e646-890d-45db-b3ef-6e9689352c15",
      "name": "Check if User Exists in Supabase",
      "type": "n8n-nodes-base.supabase",
      "position": [
        -528,
        656
      ],
      "parameters": {
        "filters": {
          "conditions": [
            {
              "keyName": "chat_id",
              "keyValue": "={{ $json.message.chat.id }}"
            }
          ]
        },
        "tableId": "telegram_users",
        "operation": "get"
      },
      "credentials": {
        "supabaseApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "601fc8b2-886b-436a-a337-3f47e3ba52e5",
      "name": "Route: New or Existing User?",
      "type": "n8n-nodes-base.if",
      "position": [
        -304,
        656
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "f4e144b5-d1b4-4752-9ad3-4c85d9e246cb",
              "operator": {
                "type": "number",
                "operation": "empty",
                "singleValue": true
              },
              "leftValue": "={{ $('Telegram Trigger').item.json.message.chat.id }}",
              "rightValue": ""
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "ff0ec30f-d2df-402a-a442-cd548102c1dc",
      "name": "Save New User to Supabase",
      "type": "n8n-nodes-base.supabase",
      "position": [
        144,
        512
      ],
      "parameters": {
        "tableId": "telegram_users",
        "fieldsUi": {
          "fieldValues": [
            {
              "fieldId": "chat_id",
              "fieldValue": "={{ $('Telegram Trigger').item.json.message.chat.id }}"
            }
          ]
        }
      },
      "credentials": {
        "supabaseApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "8314f5f5-f8a4-412e-93f7-571ca2efebb5",
      "name": "Send Welcome Message (New User)",
      "type": "n8n-nodes-base.telegram",
      "position": [
        368,
        512
      ],
      "parameters": {
        "text": "Thank you for trying my project. You can also check out the FAQ or ask any questions you may have. If you want to see the FAQ here, just click this link |/Who_are_you?/What_can_you_do?/Who_created_you?/Are_you_free?",
        "chatId": "={{ $('Telegram Trigger').item.json.message.from.id }}",
        "additionalFields": {}
      },
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "bdffd8ec-9f55-435a-8063-c683ad45ac68",
      "name": "Load FAQ Context for AI",
      "type": "n8n-nodes-base.set",
      "position": [
        -80,
        896
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "994d25f7-381c-4bb2-94c1-cd99e72948dd",
              "name": "faq_context",
              "type": "string",
              "value": "Q: Who are you? A: I am an automated assistant bot created to learn n8n and AI.  Q: What can you do? A: Currently, I can answer basic questions about myself. In the future, I will be developed to do other things.  Q: Who created you? A: I was created by the owner of this n8n server using Google Gemini and n8n.  Q: Are you free? A: Yes, I am a learning project and free to use."
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "0f6a6366-543d-4e74-86e1-07c856bd2249",
      "name": "Process Question with Gemini",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        144,
        896
      ],
      "parameters": {
        "text": "=Anda adalah asisten bot yang ramah dan membantu. Tugas Anda adalah menjawab pertanyaan pengguna berdasarkan Konteks FAQ yang diberikan. Jika pertanyaan pengguna tidak bisa dijawab dari konteks, jawab dengan benar pertanyaan tersebut\nJika pengguna bertanya terkait \"apa saja faq nya\" kamu bisa memberi semua pertanyaan yang tertera di faq dengan mengawali pertanyaan dengan / dan hilangkan spasi yang terkandung dalam pertanyaan ganti lah dengan underscore\"_\" \n--- KONTEKS FAQ ---\n{{ $json.faq_context }}\n--- AKHIR KONTEKS ---\n\nPertanyaan Pengguna: {{ $('Telegram Trigger').item.json.message.text }}",
        "options": {},
        "promptType": "define"
      },
      "typeVersion": 2.2
    },
    {
      "id": "5cd2bd5b-fcff-4be9-9341-16e52e65cdc1",
      "name": "Send AI Answer to User",
      "type": "n8n-nodes-base.telegram",
      "position": [
        640,
        896
      ],
      "parameters": {
        "text": "={{ $json.output.replace(/[*_`[\\]()]/g, \"\") }}",
        "chatId": "={{ $('Telegram Trigger').item.json.message.from.id }}",
        "additionalFields": {}
      },
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.2
    }
  ],
  "connections": {
    "Telegram Trigger": {
      "main": [
        [
          {
            "node": "Check if User Exists in Supabase",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Load FAQ Context for AI": {
      "main": [
        [
          {
            "node": "Process Question with Gemini",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Google Gemini Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "Process Question with Gemini",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Save New User to Supabase": {
      "main": [
        [
          {
            "node": "Send Welcome Message (New User)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Process Question with Gemini": {
      "main": [
        [
          {
            "node": "Send AI Answer to User",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Route: New or Existing User?": {
      "main": [
        [
          {
            "node": "Save New User to Supabase",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Load FAQ Context for AI",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check if User Exists in Supabase": {
      "main": [
        [
          {
            "node": "Route: New or Existing User?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}