{
  "name": "WhatsApp \u2192 Mimotes AI",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "whatsapp-webhook",
        "responseMode": "responseNode",
        "options": {}
      },
      "id": "webhook-receiver",
      "name": "WhatsApp Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict"
          },
          "conditions": [
            {
              "id": "condition-entry",
              "leftValue": "={{ $json.body.entry }}",
              "rightValue": "",
              "operator": {
                "type": "array",
                "operation": "exists",
                "singleValue": false
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "check-entry",
      "name": "Has Entry?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [
        470,
        300
      ]
    },
    {
      "parameters": {
        "jsCode": "// Parse WhatsApp webhook payload\nconst entry = $input.first().json.body.entry[0];\nconst changes = entry.changes || [];\n\nconst messages = [];\n\nfor (const change of changes) {\n  if (change.field !== 'messages') continue;\n  \n  const value = change.value;\n  const incomingMessages = value.messages || [];\n  \n  for (const msg of incomingMessages) {\n    messages.push({\n      json: {\n        messageId: msg.id,\n        from: msg.from,\n        timestamp: msg.timestamp,\n        type: msg.type,\n        text: msg.text?.body || '',\n        phone: msg.from,\n        contacts: value.contacts || [],\n        metadata: value.metadata || {}\n      }\n    });\n  }\n}\n\nreturn messages.length > 0 ? messages : [{ json: { skip: true } }];"
      },
      "id": "parse-messages",
      "name": "Parse Messages",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        690,
        240
      ]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict"
          },
          "conditions": [
            {
              "id": "condition-skip",
              "leftValue": "={{ $json.skip }}",
              "rightValue": true,
              "operator": {
                "type": "boolean",
                "operation": "notTrue"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "check-skip",
      "name": "Not Skip?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [
        910,
        240
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "http://mimotes-app-1:3000/api/whatsapp/n8n",
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "message",
              "value": "={{ $json.text }}"
            },
            {
              "name": "phone",
              "value": "={{ $json.phone }}"
            },
            {
              "name": "workspaceId",
              "value": "YOUR_WORKSPACE_ID"
            }
          ]
        },
        "options": {
          "timeout": 30000
        }
      },
      "id": "call-mimotes",
      "name": "Call Mimotes AI",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1130,
        180
      ]
    },
    {
      "parameters": {
        "jsCode": "// Prepare WhatsApp reply\nconst response = $input.first().json;\nconst phone = $('Parse Messages').first().json.phone;\n\nreturn [{\n  json: {\n    phone,\n    message: response.response || 'Maaf, terjadi kesalahan.',\n    sessionId: response.sessionId\n  }\n}];"
      },
      "id": "prepare-reply",
      "name": "Prepare Reply",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1350,
        180
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "=https://graph.facebook.com/v18.0/{{ $json.metadata?.phone_number_id || 'YOUR_PHONE_NUMBER_ID' }}/messages",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"messaging_product\": \"whatsapp\",\n  \"to\": \"{{ $json.phone }}\",\n  \"type\": \"text\",\n  \"text\": {\n    \"body\": \"{{ $json.message }}\"\n  }\n}",
        "options": {}
      },
      "id": "send-whatsapp",
      "name": "Send WhatsApp Reply",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1570,
        180
      ],
      "credentials": {
        "httpHeaderAuth": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={\n  \"status\": \"ok\",\n  \"message\": \"Message processed\"\n}",
        "options": {}
      },
      "id": "respond-ok",
      "name": "Respond OK",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [
        910,
        380
      ]
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={\n  \"status\": \"ok\",\n  \"message\": \"No messages to process\"\n}",
        "options": {}
      },
      "id": "respond-skip",
      "name": "Respond Skip",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [
        1130,
        380
      ]
    }
  ],
  "connections": {
    "WhatsApp Webhook": {
      "main": [
        [
          {
            "node": "Has Entry?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Has Entry?": {
      "main": [
        [
          {
            "node": "Parse Messages",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Respond OK",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Parse Messages": {
      "main": [
        [
          {
            "node": "Not Skip?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Not Skip?": {
      "main": [
        [
          {
            "node": "Call Mimotes AI",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Respond Skip",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Call Mimotes AI": {
      "main": [
        [
          {
            "node": "Prepare Reply",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Prepare Reply": {
      "main": [
        [
          {
            "node": "Send WhatsApp Reply",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  }
}