{
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "vapi-call-end",
        "options": {}
      },
      "id": "692475c9-fb68-4a81-8e5c-433a80e8a394",
      "name": "VAPI Call-End Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2.1,
      "position": [
        -240,
        280
      ]
    },
    {
      "parameters": {
        "resource": "chat",
        "operation": "create",
        "model": "gpt-4o",
        "messages": {
          "messageValues": [
            {
              "content": "\u0623\u0646\u062a \u0645\u062d\u0644\u0644 \u062c\u0648\u062f\u0629 \u0645\u0643\u0627\u0644\u0645\u0627\u062a \u0644\u0645\u0631\u0643\u0632 \u0627\u062a\u0635\u0627\u0644 \u0630\u0643\u064a (AI Call Center).\n\n\u0627\u0644\u0645\u0637\u0644\u0648\u0628 \u0645\u0646\u0643 \u062a\u062d\u0644\u064a\u0644 \u0646\u0635 \u0627\u0644\u0645\u0643\u0627\u0644\u0645\u0629 (Transcript) \u0627\u0644\u0645\u0631\u0641\u0642 \u0648\u0627\u0633\u062a\u062e\u0631\u0627\u062c \u0627\u0644\u0628\u064a\u0627\u0646\u0627\u062a \u0627\u0644\u062a\u0627\u0644\u064a\u0629 \u0628\u062f\u0642\u0629 \u0639\u0644\u0649 \u0634\u0643\u0644 \u0643\u0627\u0626\u0646 JSON (JSON Object) \u0641\u0642\u0637\u060c \u0628\u062f\u0648\u0646 \u0623\u064a \u0646\u0635\u0648\u0635 \u0625\u0636\u0627\u0641\u064a\u0629:\n\n1. `summary`: \u0645\u0644\u062e\u0635 \u062a\u0641\u0635\u064a\u0644\u064a \u0644\u0644\u0645\u0643\u0627\u0644\u0645\u0629 \u0648\u0645\u0627 \u062d\u062f\u062b \u0641\u064a\u0647\u0627.\n2. `two_word_summary`: \u0645\u0644\u062e\u0635 \u0642\u0635\u064a\u0631 \u062c\u062f\u0627\u064b \u0644\u0644\u0645\u0643\u0627\u0644\u0645\u0629 \u0645\u0646 \u0643\u0644\u0645\u062a\u064a\u0646 \u0623\u0648 3 \u0643\u0644\u0645\u0627\u062a \u0628\u062d\u062f \u0623\u0642\u0635\u0649 \u0644\u062a\u0643\u0648\u0646 \u0639\u0646\u0648\u0627\u0646\u0627\u064b \u0644\u0644\u0645\u0643\u0627\u0644\u0645\u0629.\n3. `ended_prematurely`: (true/false) \u0647\u0644 \u0642\u0627\u0645 \u0627\u0644\u0639\u0645\u064a\u0644 \u0628\u0625\u0646\u0647\u0627\u0621 \u0627\u0644\u0645\u0643\u0627\u0644\u0645\u0629 \u0642\u0628\u0644 \u0627\u0643\u062a\u0645\u0627\u0644 \u0627\u0644\u0645\u062d\u0627\u062f\u062b\u0629 \u0648\u0627\u0644\u0648\u0635\u0648\u0644 \u0644\u0644\u0647\u062f\u0641 \u0627\u0644\u0645\u0631\u062c\u0648\u061f (true \u064a\u0639\u0646\u064a \u0623\u0646\u0647\u0627 \u0627\u0646\u062a\u0647\u062a \u0642\u0628\u0644 \u0627\u0644\u0623\u0648\u0627\u0646 \u0648\u0644\u0645 \u064a\u062a\u0645 \u062d\u062c\u0632 \u0627\u0644\u0645\u0648\u0639\u062f \u0623\u0648 \u0625\u063a\u0644\u0627\u0642 \u0627\u0644\u0645\u0643\u0627\u0644\u0645\u0629 \u0628\u0634\u0643\u0644 \u0637\u0628\u064a\u0639\u064a\u060c false \u064a\u0639\u0646\u064a \u0623\u0646\u0647\u0627 \u0627\u0643\u062a\u0645\u0644\u062a \u0628\u0634\u0643\u0644 \u0637\u0628\u064a\u0639\u064a).\n4. `appointment_booked`: (true/false) \u0647\u0644 \u0642\u0627\u0645 \u0627\u0644\u0639\u0645\u064a\u0644 \u0628\u062d\u062c\u0632 \u0645\u0648\u0639\u062f \u0628\u0634\u0643\u0644 \u0641\u0639\u0644\u064a \u0648\u0645\u0624\u0643\u062f\u061f\n5. `appointment_datetime`: \u0641\u064a \u062d\u0627\u0644 \u0643\u0627\u0646 `appointment_booked` \u0635\u062d\u064a\u062d\u0627\u064b\u060c \u0627\u0633\u062a\u062e\u0631\u062c \u062a\u0627\u0631\u064a\u062e \u0648\u0648\u0642\u062a \u0627\u0644\u0645\u0648\u0639\u062f. \u0625\u0646 \u0644\u0645 \u064a\u0648\u062c\u062f\u060c \u0627\u062c\u0639\u0644\u0647 \u0641\u0627\u0631\u063a\u0627\u064b (\"\").\n6. `customer_email`: \u0627\u0644\u0628\u0631\u064a\u062f \u0627\u0644\u0625\u0644\u0643\u062a\u0631\u0648\u0646\u064a \u0644\u0644\u0639\u0645\u064a\u0644 \u0625\u0630\u0627 \u0630\u0643\u0631\u0647 \u0641\u064a \u0627\u0644\u0645\u0643\u0627\u0644\u0645\u0629\u060c \u0625\u0646 \u0644\u0645 \u064a\u0648\u062c\u062f \u0627\u062a\u0631\u0643\u0647 \u0641\u0627\u0631\u063a\u0627\u064b (\"\").\n7. `customer_name`: \u0627\u0633\u0645 \u0627\u0644\u0639\u0645\u064a\u0644 \u0625\u0630\u0627 \u0630\u0643\u0631\u0647 \u0641\u064a \u0627\u0644\u0645\u0643\u0627\u0644\u0645\u0629\u060c \u0625\u0646 \u0644\u0645 \u064a\u0648\u062c\u062f \u0627\u062a\u0631\u0643\u0647 \u0641\u0627\u0631\u063a\u0627\u064b (\"\").",
              "role": "system"
            },
            {
              "content": "={{ $json.body.message.transcript }}",
              "role": "user"
            }
          ]
        },
        "jsonOutput": true,
        "options": {
          "temperature": 0.1
        }
      },
      "id": "ai-analysis-node",
      "name": "AI Call Analysis (OpenAI)",
      "type": "n8n-nodes-base.openAi",
      "typeVersion": 1.2,
      "position": [
        20,
        280
      ],
      "notesInFlow": true,
      "notes": "\u0627\u0644\u0630\u0643\u0627\u0621 \u0627\u0644\u0627\u0635\u0637\u0646\u0627\u0639\u064a \u064a\u062d\u0644\u0644 \u0627\u0644\u0645\u0643\u0627\u0644\u0645\u0629"
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "1",
              "name": "tenant_id",
              "value": "={{ $('VAPI Call-End Webhook').item.json.body.message.call.assistantId }}",
              "type": "string"
            },
            {
              "id": "2",
              "name": "caller_name",
              "value": "={{ JSON.parse($json.message.content).customer_name || $('VAPI Call-End Webhook').item.json.body.message.customer.name || $('VAPI Call-End Webhook').item.json.body.message.customer.number }}",
              "type": "string"
            },
            {
              "id": "3",
              "name": "call_duration",
              "value": "={{ Math.round($('VAPI Call-End Webhook').item.json.body.message.durationSeconds || 0) }}",
              "type": "number"
            },
            {
              "id": "4",
              "name": "total_conversation",
              "value": "={{ $('VAPI Call-End Webhook').item.json.body.message.transcript }}",
              "type": "string"
            },
            {
              "id": "5",
              "name": "detailed_summary",
              "value": "={{ JSON.parse($json.message.content).summary }}",
              "type": "string"
            },
            {
              "id": "12",
              "name": "two_word_summary",
              "value": "={{ JSON.parse($json.message.content).two_word_summary }}",
              "type": "string"
            },
            {
              "id": "6",
              "name": "ended_reason",
              "value": "={{ JSON.parse($json.message.content).ended_prematurely ? 'customer-hung-up' : 'assistant-hung-up' }}",
              "type": "string"
            },
            {
              "id": "13",
              "name": "status",
              "value": "={{ JSON.parse($json.message.content).ended_prematurely ? 'missed' : 'completed' }}",
              "type": "string"
            },
            {
              "id": "7",
              "name": "custom_data",
              "value": "={{ $('VAPI Call-End Webhook').item.json.body.message }}",
              "type": "object"
            },
            {
              "id": "8",
              "name": "recording_url",
              "value": "={{ $('VAPI Call-End Webhook').item.json.body.message.recordingUrl || $('VAPI Call-End Webhook').item.json.body.message.call.recordingUrl }}",
              "type": "string"
            },
            {
              "id": "9",
              "name": "appointment_booked",
              "value": "={{ JSON.parse($json.message.content).appointment_booked }}",
              "type": "boolean"
            },
            {
              "id": "10",
              "name": "appointment_datetime",
              "value": "={{ JSON.parse($json.message.content).appointment_datetime }}",
              "type": "string"
            },
            {
              "id": "11",
              "name": "customer_email",
              "value": "={{ JSON.parse($json.message.content).customer_email }}",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "id": "6a7eb36c-da82-4b89-8dfc-91af8940bf73",
      "name": "Extract Call Data",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        280,
        280
      ]
    },
    {
      "parameters": {
        "tableId": "calls",
        "fieldsUi": {
          "fieldValues": [
            {
              "fieldId": "tenant_id",
              "fieldValue": "={{ $json.tenant_id }}"
            },
            {
              "fieldId": "caller_name",
              "fieldValue": "={{ $json.caller_name }}"
            },
            {
              "fieldId": "call_duration",
              "fieldValue": "={{ $json.call_duration }}"
            },
            {
              "fieldId": "total_conversation",
              "fieldValue": "={{ $json.total_conversation }}"
            },
            {
              "fieldId": "detailed_summary",
              "fieldValue": "={{ $json.detailed_summary }}"
            },
            {
              "fieldId": "two_word_summary",
              "fieldValue": "={{ $json.two_word_summary }}"
            },
            {
              "fieldId": "ended_reason",
              "fieldValue": "={{ $json.ended_reason }}"
            },
            {
              "fieldId": "status",
              "fieldValue": "={{ $json.status }}"
            },
            {
              "fieldId": "recording_url",
              "fieldValue": "={{ $json.recording_url }}"
            },
            {
              "fieldId": "custom_data",
              "fieldValue": "={{ JSON.stringify($json.custom_data) }}"
            }
          ]
        }
      },
      "id": "da0eb480-f748-4169-9f99-e593d1867cbd",
      "name": "Save Call to Supabase",
      "type": "n8n-nodes-base.supabase",
      "typeVersion": 1,
      "position": [
        560,
        140
      ],
      "credentials": {
        "supabaseApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict"
          },
          "conditions": [
            {
              "id": "db8fa439-d419-4b0d-b153-fb80df8da503",
              "leftValue": "={{ $json.appointment_booked }}",
              "rightValue": true,
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "if-appointment-booked",
      "name": "If Appointment Booked?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 3,
      "position": [
        560,
        420
      ]
    },
    {
      "parameters": {
        "resource": "calendarEvent",
        "operation": "create",
        "calendarId": "default",
        "start": "={{ $json.appointment_datetime }}",
        "end": "={{ $json.appointment_datetime }}",
        "subject": "={{ 'Appointment with ' + $json.caller_name }}",
        "content": "={{ 'Booked via AI Assistant.\\n\\nSummary: ' + $json.detailed_summary }}",
        "options": {}
      },
      "id": "outlook-calendar",
      "name": "Outlook Calendar",
      "type": "n8n-nodes-base.microsoftOutlook",
      "typeVersion": 2,
      "position": [
        820,
        340
      ]
    },
    {
      "parameters": {
        "sendTo": "={{ $json.customer_email }}",
        "subject": "\u062a\u0623\u0643\u064a\u062f \u0645\u0648\u0639\u062f\u0643 - Voxa AI",
        "message": "={{ '\u0623\u0647\u0644\u0627\u064b \u0628\u0643 ' + $json.caller_name + '\u060c\\n\\n\u062a\u0645 \u062a\u0623\u0643\u064a\u062f \u0645\u0648\u0639\u062f\u0643 \u0628\u0646\u062c\u0627\u062d \u0641\u064a \u0627\u0644\u062a\u0627\u0631\u064a\u062e \u0648\u0627\u0644\u0648\u0642\u062a \u0627\u0644\u062a\u0627\u0644\u064a: ' + $json.appointment_datetime + '.\\n\\n\u0646\u062a\u0637\u0644\u0639 \u0644\u0631\u0624\u064a\u062a\u0643 \u0642\u0631\u064a\u0628\u0627\u064b!\\n\\n\u0645\u0639 \u062a\u062d\u064a\u0627\u062a\u060c\\n\u0641\u0631\u064a\u0642 Voxa AI' }}",
        "options": {}
      },
      "id": "send-email",
      "name": "Send Email",
      "type": "n8n-nodes-base.emailSend",
      "typeVersion": 2.1,
      "position": [
        1060,
        340
      ]
    },
    {
      "parameters": {
        "operation": "sendTemplate",
        "phoneNumberId": "REPLACE_WITH_YOUR_PHONE_NUMBER_ID",
        "recipientPhoneNumber": "={{ $json.custom_data.customer.number }}",
        "template": "appointment_confirmation",
        "components": {
          "component": [
            {
              "type": "body",
              "parameters": {
                "parameter": [
                  {
                    "type": "text",
                    "text": "={{ $json.caller_name }}"
                  },
                  {
                    "type": "text",
                    "text": "={{ $json.appointment_datetime }}"
                  }
                ]
              }
            }
          ]
        }
      },
      "id": "whatsapp",
      "name": "WhatsApp",
      "type": "n8n-nodes-base.whatsApp",
      "typeVersion": 1,
      "position": [
        1280,
        340
      ],
      "notesInFlow": true,
      "notes": "\u064a\u062d\u062a\u0627\u062c \u0625\u0636\u0627\u0641\u0629 Credentials \u0642\u0627\u0644\u0628 \u0648\u0627\u062a\u0633\u0627\u0628"
    }
  ],
  "connections": {
    "VAPI Call-End Webhook": {
      "main": [
        [
          {
            "node": "AI Call Analysis (OpenAI)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "AI Call Analysis (OpenAI)": {
      "main": [
        [
          {
            "node": "Extract Call Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract Call Data": {
      "main": [
        [
          {
            "node": "Save Call to Supabase",
            "type": "main",
            "index": 0
          },
          {
            "node": "If Appointment Booked?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "If Appointment Booked?": {
      "main": [
        [
          {
            "node": "Outlook Calendar",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Outlook Calendar": {
      "main": [
        [
          {
            "node": "Send Email",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send Email": {
      "main": [
        [
          {
            "node": "WhatsApp",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}