{
  "name": "Pora Studio: enquiry to AI reply, Google Sheets and Telegram",
  "nodes": [
    {
      "parameters": {
        "content": "## Setup (about 15 minutes)\n1. **Website form**: copy the Production URL and POST the form to it as JSON: name, contact, service, message, channel.\n2. **AI: intent + reply**: add your OpenAI credential. Edit the price list and hours in the system message.\n3. **Log to Google Sheets**: add a Google Sheets credential, paste your sheet ID. Row 1 headers: Date/Time | Channel | Name | Message | Bot reply | Status.\n4. **Alert owner in Telegram**: create a bot with @BotFather, add the token as a credential, put your chat ID in Chat ID.\n5. Activate the workflow.\n\nIf Sheets or Telegram fail, the client still gets the reply (On Error: continue).",
        "height": 380,
        "width": 420
      },
      "id": "7c1d2a55-8a0e-4c39-9b6f-1f2e4a6b0001",
      "name": "Setup",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [-520, -420]
    },
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "pora-lead",
        "responseMode": "responseNode",
        "options": {}
      },
      "id": "7c1d2a55-8a0e-4c39-9b6f-1f2e4a6b0002",
      "name": "Website form",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [-480, 0],
      "webhookId": "5b0f7e2c-3d41-4a8e-a6c2-9e1f0d7b4a10"
    },
    {
      "parameters": {
        "mode": "runOnceForEachItem",
        "jsCode": "// Clean the form fields and add the time the enquiry arrived.\nconst body = $json.body || {};\nconst clean = (value, max) => String(value || '').replace(/\\s+/g, ' ').trim().slice(0, max);\n\nreturn {\n  json: {\n    receivedAt: $now.setZone('Europe/Warsaw').toFormat('yyyy-MM-dd HH:mm'),\n    channel: clean(body.channel, 40) || 'Website form',\n    name: clean(body.name, 80),\n    contact: clean(body.contact, 120),\n    service: clean(body.service, 60),\n    message: clean(body.message, 2000)\n  }\n};"
      },
      "id": "7c1d2a55-8a0e-4c39-9b6f-1f2e4a6b0003",
      "name": "Prepare lead",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [-260, 0]
    },
    {
      "parameters": {
        "modelId": {
          "__rl": true,
          "mode": "list",
          "value": "gpt-4o-mini"
        },
        "messages": {
          "values": [
            {
              "role": "system",
              "content": "You answer enquiries for Pora Studio, a hair and beauty salon in Krakow.\nPrice list: haircut 120 PLN, colouring from 260 PLN, manicure 110 PLN, brows and lashes 90 PLN.\nOpening hours: Monday to Saturday, 9:00 to 20:00.\n\nRules:\n- Reply in the language of the client's message: Polish, English or Ukrainian.\n- 2 or 3 short, friendly sentences. Sign as Pora Studio.\n- Never invent free slots. For a booking, ask for a preferred day and time and say the team confirms within 15 minutes.\n- For a complaint, apologise and say the manager will call within 1 hour.\n\nReturn ONLY valid JSON, no markdown, in exactly this shape:\n{\"intent\": \"booking | price | question | complaint\", \"language\": \"pl | en | uk\", \"reply\": \"text for the client\", \"status\": \"Replied | Waiting for slot | Urgent: call back\"}"
            },
            {
              "content": "=Name: {{ $json.name }}\nService: {{ $json.service }}\nMessage: {{ $json.message }}"
            }
          ]
        },
        "options": {}
      },
      "id": "7c1d2a55-8a0e-4c39-9b6f-1f2e4a6b0004",
      "name": "AI: intent + reply",
      "type": "@n8n/n8n-nodes-langchain.openAi",
      "typeVersion": 1.8,
      "position": [-40, 0]
    },
    {
      "parameters": {
        "mode": "runOnceForEachItem",
        "jsCode": "// The OpenAI node returns the answer at $json.message.content.\nconst lead = $('Prepare lead').item.json;\nlet ai;\ntry {\n  ai = JSON.parse($json.message.content);\n} catch (error) {\n  // Unexpected AI output: send a safe reply and flag the row for a person.\n  ai = {\n    intent: 'question',\n    language: 'en',\n    reply: 'Thank you for your message! We will get back to you within 15 minutes. Pora Studio',\n    status: 'Check manually'\n  };\n}\n\nreturn { json: { ...lead, ...ai } };"
      },
      "id": "7c1d2a55-8a0e-4c39-9b6f-1f2e4a6b0005",
      "name": "Parse AI answer",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [180, 0]
    },
    {
      "parameters": {
        "operation": "append",
        "documentId": {
          "__rl": true,
          "mode": "id",
          "value": "YOUR_GOOGLE_SHEET_ID"
        },
        "sheetName": {
          "__rl": true,
          "mode": "name",
          "value": "Leads"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "Date/Time": "={{ $json.receivedAt }}",
            "Channel": "={{ $json.channel }}",
            "Name": "={{ $json.name }} ({{ $json.contact }})",
            "Message": "={{ $json.message }}",
            "Bot reply": "={{ $json.reply }}",
            "Status": "={{ $json.status }}"
          },
          "matchingColumns": [],
          "schema": [
            { "id": "Date/Time", "displayName": "Date/Time", "required": false, "defaultMatch": false, "display": true, "type": "string", "canBeUsedToMatch": true },
            { "id": "Channel", "displayName": "Channel", "required": false, "defaultMatch": false, "display": true, "type": "string", "canBeUsedToMatch": true },
            { "id": "Name", "displayName": "Name", "required": false, "defaultMatch": false, "display": true, "type": "string", "canBeUsedToMatch": true },
            { "id": "Message", "displayName": "Message", "required": false, "defaultMatch": false, "display": true, "type": "string", "canBeUsedToMatch": true },
            { "id": "Bot reply", "displayName": "Bot reply", "required": false, "defaultMatch": false, "display": true, "type": "string", "canBeUsedToMatch": true },
            { "id": "Status", "displayName": "Status", "required": false, "defaultMatch": false, "display": true, "type": "string", "canBeUsedToMatch": true }
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {}
      },
      "id": "7c1d2a55-8a0e-4c39-9b6f-1f2e4a6b0006",
      "name": "Log to Google Sheets",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.5,
      "position": [400, 0],
      "onError": "continueRegularOutput"
    },
    {
      "parameters": {
        "chatId": "YOUR_TELEGRAM_CHAT_ID",
        "text": "={{ `New enquiry: ${$('Parse AI answer').item.json.channel}\n\nName: ${$('Parse AI answer').item.json.name}\nContact: ${$('Parse AI answer').item.json.contact}\nService: ${$('Parse AI answer').item.json.service}\nIntent: ${$('Parse AI answer').item.json.intent} (${$('Parse AI answer').item.json.language})\n\nMessage:\n${$('Parse AI answer').item.json.message}\n\nBot replied:\n${$('Parse AI answer').item.json.reply}\n\nStatus: ${$('Parse AI answer').item.json.status}` }}",
        "additionalFields": {
          "appendAttribution": false
        }
      },
      "id": "7c1d2a55-8a0e-4c39-9b6f-1f2e4a6b0007",
      "name": "Alert owner in Telegram",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [620, 0],
      "webhookId": "5b0f7e2c-3d41-4a8e-a6c2-9e1f0d7b4a11",
      "onError": "continueRegularOutput"
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={{ JSON.stringify({ reply: $('Parse AI answer').item.json.reply, intent: $('Parse AI answer').item.json.intent }) }}",
        "options": {}
      },
      "id": "7c1d2a55-8a0e-4c39-9b6f-1f2e4a6b0008",
      "name": "Reply to website",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [840, 0]
    }
  ],
  "connections": {
    "Website form": { "main": [[{ "node": "Prepare lead", "type": "main", "index": 0 }]] },
    "Prepare lead": { "main": [[{ "node": "AI: intent + reply", "type": "main", "index": 0 }]] },
    "AI: intent + reply": { "main": [[{ "node": "Parse AI answer", "type": "main", "index": 0 }]] },
    "Parse AI answer": { "main": [[{ "node": "Log to Google Sheets", "type": "main", "index": 0 }]] },
    "Log to Google Sheets": { "main": [[{ "node": "Alert owner in Telegram", "type": "main", "index": 0 }]] },
    "Alert owner in Telegram": { "main": [[{ "node": "Reply to website", "type": "main", "index": 0 }]] }
  },
  "pinData": {},
  "settings": {
    "executionOrder": "v1"
  }
}
