{
  "name": "form-website",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "form-site",
        "authentication": "basicAuth",
        "options": {}
      },
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2.1,
      "position": [
        -848,
        -704
      ],
      "id": "d70f54d7-2563-4a5a-95e6-dd35093dd262",
      "name": "Webhook",
      "webhookId": "3142df9e-d10b-4a95-8602-73b48e7a2c70",
      "credentials": {
        "httpBasicAuth": {
          "id": "1Bw4URTLpbDVB8u4",
          "name": "Website Forms Webhook credential"
        }
      }
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "03fa6023-f760-47bf-ad01-ff737842ee1b",
              "name": "chatfluxx.url",
              "value": "app.neofluxx.com",
              "type": "string"
            },
            {
              "id": "2011128c-50ec-4216-a58a-f5355782be81",
              "name": "chatfluxx.account_id",
              "value": "16",
              "type": "string"
            },
            {
              "id": "df05deaf-8947-4c0c-a9c6-5982831c7b22",
              "name": "chatfluxx.api_access_token",
              "value": "iZtHR98XNgqEdsUUDvTSNb4j",
              "type": "string"
            },
            {
              "id": "4185cfb9-0de5-47c9-a297-77a28456fb5c",
              "name": "chatfluxx.team_id",
              "value": "14",
              "type": "string"
            },
            {
              "id": "c4470306-615d-44fc-8a21-ace001cf96a4",
              "name": "chatfluxx.inbox_id",
              "value": "96",
              "type": "string"
            },
            {
              "id": "d998f8b8-e1da-47dc-91fa-593b240b360c",
              "name": "chatfluxx.contact_id",
              "value": "72971",
              "type": "string"
            },
            {
              "id": "f1d3ac4f-c318-4144-ba89-b9571f8740a5",
              "name": "chatfluxx.source_id",
              "value": "fed3a956-d3cb-4abe-8c01-e041bb9e9aa2",
              "type": "string"
            },
            {
              "id": "a8dae68c-e77c-40a1-88c8-53f68d250ec2",
              "name": "chatfluxx.assignee_id",
              "value": "2",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        -400,
        -704
      ],
      "id": "921edc34-e163-4864-9e82-e0a79d02d86c",
      "name": "w-cfg"
    },
    {
      "parameters": {
        "method": "POST",
        "url": "=https://{{ $('w-cfg').item.json.chatfluxx.url }}/api/v1/accounts/{{ $('w-cfg').item.json.chatfluxx.account_id }}/conversations",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "api_access_token",
              "value": "={{ $('w-cfg').item.json.chatfluxx.api_access_token }}"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"source_id\": \"{{ $json.source_id }}\",\n  \"inbox_id\": {{ $json.inbox_id }},\n  \"contact_id\": {{ $json.contact_id }},\n  \"status\": \"open\",\n  \"team_id\": {{ $('w-cfg').item.json.chatfluxx.team_id }},\n  \"assignee_id\": {{ $('w-cfg').item.json.chatfluxx.assignee_id }}\n}",
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.4,
      "position": [
        1392,
        -704
      ],
      "id": "80cf7454-6887-47fe-a0d8-361ca90a8242",
      "name": "Create Conversation",
      "retryOnFail": true
    },
    {
      "parameters": {
        "method": "POST",
        "url": "=https://{{ $('w-cfg').item.json.chatfluxx.url }}/api/v1/accounts/{{ $('w-cfg').item.json.chatfluxx.account_id }}/conversations/{{ $('Create Conversation').first().json.id }}/messages\n\n",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "api_access_token",
              "value": "={{ $('w-cfg').item.json.chatfluxx.api_access_token }}"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"content\": {{ JSON.stringify($('Normaliza Infos').item.json.content) }},\n  \"message_type\": \"outgoing\",\n  \"private\": false,\n  \"content_type\": \"text\"\n}",
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.4,
      "position": [
        2064,
        -704
      ],
      "id": "45f88286-c79d-4acf-8451-fd605d37686a",
      "name": "Create New Message",
      "retryOnFail": true
    },
    {
      "parameters": {
        "method": "POST",
        "url": "=https://{{ $('w-cfg').item.json.chatfluxx.url }}/api/v1/accounts/{{ $('w-cfg').item.json.chatfluxx.account_id }}/conversations/{{$('Create Conversation').item.json.id}}/toggle_priority",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "api_access_token",
              "value": "={{ $('w-cfg').item.json.chatfluxx.api_access_token }}"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"priority\": \"medium\"\n}",
        "options": {
          "response": {
            "response": {
              "fullResponse": true,
              "responseFormat": "json"
            }
          }
        }
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.4,
      "position": [
        1840,
        -704
      ],
      "id": "8b10966a-27ce-4797-84e2-f17a36fc1dac",
      "name": "Toggle Priority",
      "retryOnFail": true
    },
    {
      "parameters": {
        "jsCode": "const body = $json.body;\n\nconst textoFormatado = Object.entries(body)\n  .map(([chave, valor]) => {\n    if (Array.isArray(valor)) {\n      return `• ${chave}: ${valor.join(', ')}`;\n    }\n\n    if (typeof valor === 'boolean') {\n      return `• ${chave}: ${valor ? 'true' : 'false'}`;\n    }\n\n    return `• ${chave}: ${valor}`;\n  })\n  .join('\\n');\n\nreturn [\n  {\n    json: {\n      content: textoFormatado\n    }\n  }\n];"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        -624,
        -704
      ],
      "id": "729b97f5-ea70-48e7-ad7a-75857856c397",
      "name": "Normaliza Infos"
    },
    {
      "parameters": {
        "method": "POST",
        "url": "=https://{{$('w-cfg').item.json.chatfluxx.url}}/api/v1/accounts/{{$('w-cfg').item.json.chatfluxx.account_id}}/contacts",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "api_access_token",
              "value": "={{ $('w-cfg').item.json.chatfluxx.api_access_token }}"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"inbox_id\": {{ $('w-cfg').item.json.chatfluxx.inbox_id }},\n  \"name\": \"{{ $('Webhook').item.json.body.FullName }}\",\n  \"email\": \"{{ $('Webhook').item.json.body.Email }}\",\n  \"blocked\": false,\n  \"phone_number\": \"+{{ $('Webhook').item.json.body.WhatsApp }}\"\n}",
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.4,
      "position": [
        720,
        -560
      ],
      "id": "b22de568-1d2e-4e88-b878-aa69aee427fd",
      "name": "Create Contact",
      "retryOnFail": true
    },
    {
      "parameters": {
        "method": "POST",
        "url": "=https://{{ $('w-cfg').item.json.chatfluxx.url }}/api/v1/accounts/{{ $('w-cfg').item.json.chatfluxx.account_id }}/contacts/{{ $json.payload.contact.id }}/labels",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "api_access_token",
              "value": "={{ $('w-cfg').item.json.chatfluxx.api_access_token }}"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"labels\": [\n    \"lead-site\",\n    {{ JSON.stringify($('Webhook').item.json.body.Tags[0].toLowerCase()) }}\n  ]\n}",
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.4,
      "position": [
        944,
        -560
      ],
      "id": "8439d91e-06b2-4017-a368-74bdfc28855a",
      "name": "Add Labels to Contact",
      "retryOnFail": true
    },
    {
      "parameters": {
        "method": "POST",
        "url": "=https://{{ $('w-cfg').item.json.chatfluxx.url }}/api/v1/accounts/{{ $('w-cfg').item.json.chatfluxx.account_id }}/conversations/{{ $json.id}}/labels",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "api_access_token",
              "value": "={{ $('w-cfg').item.json.chatfluxx.api_access_token }}"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"labels\": [\n    \"lead-site\",\n    {{ JSON.stringify($('Webhook').item.json.body.Tags[0].toLowerCase()) }}\n  ]\n}",
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.4,
      "position": [
        1616,
        -704
      ],
      "id": "53bf9fd1-f5b0-4241-9330-5f10e475fe40",
      "name": "Add Labels to Conversation",
      "retryOnFail": true
    },
    {
      "parameters": {
        "content": "## Webhook Basic Auth\n\n## user: bestpass.travel\n## token: J0eX0jkrP50YZzfj\n## endpoint: https://n8n.bestpass.travel/webhook/form-site",
        "height": 240,
        "width": 704
      },
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -944,
        -1024
      ],
      "typeVersion": 1,
      "id": "08457d81-d023-4f2e-af2a-53539c95f086",
      "name": "Sticky Note"
    },
    {
      "parameters": {
        "method": "=GET",
        "url": "=https://{{ $json.chatfluxx.url }}/api/v1/accounts/{{ $json.chatfluxx.account_id }}/contacts/search?q={{ $('Webhook').item.json.body.Email }}",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "api_access_token",
              "value": "={{ $json.chatfluxx.api_access_token }}"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.4,
      "position": [
        -176,
        -704
      ],
      "id": "87e940af-c518-41fd-889e-66e32bf10478",
      "name": "Search Contacts by e-mail"
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 3
          },
          "conditions": [
            {
              "id": "16a252d4-8941-49f3-8296-125b5219eb57",
              "leftValue": "={{ $json.meta.count }}",
              "rightValue": 0,
              "operator": {
                "type": "number",
                "operation": "gt"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.3,
      "position": [
        48,
        -704
      ],
      "id": "c24171f2-620a-402d-8d89-d1a644e107a9",
      "name": "Existe Contato por e-mail?"
    },
    {
      "parameters": {
        "method": "=GET",
        "url": "=https://{{ $('w-cfg').item.json.chatfluxx.url }}/api/v1/accounts/{{ $('w-cfg').item.json.chatfluxx.account_id }}/contacts/search?q={{ $('Webhook').item.json.body.WhatsApp }}",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "api_access_token",
              "value": "={{ $('w-cfg').item.json.chatfluxx.api_access_token }}"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.4,
      "position": [
        272,
        -640
      ],
      "id": "f77adc3c-ab41-493e-adfe-021248e4e704",
      "name": "Search Contacts by phone"
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 3
          },
          "conditions": [
            {
              "id": "16a252d4-8941-49f3-8296-125b5219eb57",
              "leftValue": "={{ $json.meta.count }}",
              "rightValue": 0,
              "operator": {
                "type": "number",
                "operation": "gt"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.3,
      "position": [
        496,
        -640
      ],
      "id": "9be9869d-3b44-4882-836c-14a3a9904dd5",
      "name": "Existe Contato por phone?"
    },
    {
      "parameters": {
        "jsCode": "const inboxId = Number($('w-cfg').item.json.chatfluxx.inbox_id);\n\nlet contact = null;\nlet sourceId = null;\nlet origin = null;\n\n// 1) Se o contato acabou de ser criado pelo fluxo, o Chatwoot normalmente devolve payload.contact e payload.contact_inbox.\ntry {\n  const createdPayload = $('Create Contact').item.json.payload;\n\n  if (createdPayload?.contact) {\n    contact = createdPayload.contact;\n    origin = 'created_contact';\n\n    const createdContactInbox = createdPayload.contact_inbox;\n\n    if (createdContactInbox?.source_id) {\n      return [\n        {\n          json: {\n            contact_id: contact.id,\n            source_id: createdContactInbox.source_id,\n            inbox_id: inboxId,\n            has_contact_inbox: true,\n            origin,\n          },\n        },\n      ];\n    }\n  }\n} catch (error) {}\n\n// 2) Se encontrou por e-mail, usa esse contato.\ntry {\n  const emailPayload = $('Search Contacts by e-mail').item.json.payload;\n\n  if (Array.isArray(emailPayload) && emailPayload.length > 0) {\n    contact = emailPayload[0];\n    origin = 'found_by_email';\n  }\n} catch (error) {}\n\n// 3) Se não encontrou por e-mail, tenta usar o contato encontrado por telefone.\ntry {\n  const phonePayload = $('Search Contacts by phone').item.json.payload;\n\n  if (!contact && Array.isArray(phonePayload) && phonePayload.length > 0) {\n    contact = phonePayload[0];\n    origin = 'found_by_phone';\n  }\n} catch (error) {}\n\nif (!contact) {\n  throw new Error('Nenhum contato encontrado ou criado antes de criar a conversa.');\n}\n\nconst contactInboxes = contact.contact_inboxes || [];\n\n// Ponto principal da correção:\n// não usar contact_inboxes[0]; usar o contact_inbox pertencente ao inbox_id configurado.\nconst contactInbox = contactInboxes.find((ci) => {\n  const currentInboxId = Number(ci.inbox?.id || ci.inbox_id);\n  return currentInboxId === inboxId;\n});\n\nsourceId = contactInbox?.source_id || null;\n\nreturn [\n  {\n    json: {\n      contact_id: contact.id,\n      source_id: sourceId,\n      inbox_id: inboxId,\n      has_contact_inbox: Boolean(contactInbox && sourceId),\n      origin,\n    },\n  },\n];"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        720,
        -704
      ],
      "id": "e9dba520-773e-492b-9a9d-fcb6ef9c5cba",
      "name": "Resolve Contact Inbox"
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 3
          },
          "conditions": [
            {
              "id": "e97a3be4-4294-453e-822e-b527f20f2700",
              "leftValue": "={{ $json.has_contact_inbox }}",
              "rightValue": true,
              "operator": {
                "type": "boolean",
                "operation": "equals"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.3,
      "position": [
        944,
        -704
      ],
      "id": "87e8b373-007b-4b69-be0e-a095db67aa8c",
      "name": "Tem Contact Inbox do Inbox?"
    },
    {
      "parameters": {
        "method": "POST",
        "url": "=https://{{ $('w-cfg').item.json.chatfluxx.url }}/api/v1/accounts/{{ $('w-cfg').item.json.chatfluxx.account_id }}/contacts/{{ $('Resolve Contact Inbox').item.json.contact_id }}/contact_inboxes",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "api_access_token",
              "value": "={{ $('w-cfg').item.json.chatfluxx.api_access_token }}"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"inbox_id\": {{ $('w-cfg').item.json.chatfluxx.inbox_id }},\n  \"source_id\": \"form_site_{{ $('Webhook').item.json.body.WhatsApp }}\"\n}",
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.4,
      "position": [
        1168,
        -560
      ],
      "id": "dba94ea0-7ef8-4061-b522-2301c64ebc01",
      "name": "Create Contact Inbox",
      "retryOnFail": true
    },
    {
      "parameters": {
        "jsCode": "const response = $json;\n\nconst sourceId =\n  response.source_id ||\n  response.payload?.source_id ||\n  response.payload?.contact_inbox?.source_id ||\n  response.contact_inbox?.source_id ||\n  `form_site_${$('Webhook').item.json.body.WhatsApp}`;\n\nreturn [\n  {\n    json: {\n      contact_id: $('Resolve Contact Inbox').item.json.contact_id,\n      source_id: sourceId,\n      inbox_id: Number($('w-cfg').item.json.chatfluxx.inbox_id),\n      has_contact_inbox: true,\n      origin: 'created_contact_inbox',\n    },\n  },\n];"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1392,
        -560
      ],
      "id": "4cff1697-a72e-4a28-869d-08f203c03a4f",
      "name": "Normaliza Contact Inbox"
    }
  ],
  "pinData": {
    "Webhook": [
      {
        "json": {
          "headers": {
            "connection": "Keep-Alive",
            "host": "n8n.bestpass.travel",
            "x-forwarded-scheme": "http",
            "x-forwarded-proto": "http",
            "x-forwarded-for": "20.226.64.105, 104.22.10.5",
            "x-real-ip": "104.22.10.5",
            "content-length": "117",
            "cf-ray": "9f1804b8ef587ec3-GRU",
            "content-type": "application/json; charset=utf-8",
            "authorization": "Basic YmVzdHBhc3MudHJhdmVsOkowZVgwamtyUDUwWVp6Zmo=",
            "cdn-loop": "cloudflare; loops=1",
            "cf-connecting-ip": "20.226.64.105",
            "cf-ipcountry": "BR",
            "cf-visitor": "{\"scheme\":\"https\"}",
            "accept-encoding": "gzip, br"
          },
          "params": {},
          "query": {},
          "body": {
            "FullName": "Osmar Vieira",
            "WhatsApp": "5511987044376",
            "Tags": [
              "B2C"
            ],
            "Email": "teste@testse.com.br",
            "AgreeTerms": true
          },
          "webhookUrl": "https://n8n.bestpass.travel/webhook/form-site",
          "executionMode": "production"
        },
        "pairedItem": {
          "item": 0
        }
      }
    ]
  },
  "connections": {
    "w-cfg": {
      "main": [
        [
          {
            "node": "Search Contacts by e-mail",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Normaliza Infos": {
      "main": [
        [
          {
            "node": "w-cfg",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Webhook": {
      "main": [
        [
          {
            "node": "Normaliza Infos",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create Contact": {
      "main": [
        [
          {
            "node": "Add Labels to Contact",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Search Contacts by e-mail": {
      "main": [
        [
          {
            "node": "Existe Contato por e-mail?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Search Contacts by phone": {
      "main": [
        [
          {
            "node": "Existe Contato por phone?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Existe Contato por e-mail?": {
      "main": [
        [
          {
            "node": "Resolve Contact Inbox",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Search Contacts by phone",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Existe Contato por phone?": {
      "main": [
        [
          {
            "node": "Resolve Contact Inbox",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Create Contact",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Add Labels to Contact": {
      "main": [
        [
          {
            "node": "Resolve Contact Inbox",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Resolve Contact Inbox": {
      "main": [
        [
          {
            "node": "Tem Contact Inbox do Inbox?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Tem Contact Inbox do Inbox?": {
      "main": [
        [
          {
            "node": "Create Conversation",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Create Contact Inbox",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create Contact Inbox": {
      "main": [
        [
          {
            "node": "Normaliza Contact Inbox",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Normaliza Contact Inbox": {
      "main": [
        [
          {
            "node": "Create Conversation",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create Conversation": {
      "main": [
        [
          {
            "node": "Add Labels to Conversation",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Add Labels to Conversation": {
      "main": [
        [
          {
            "node": "Toggle Priority",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Toggle Priority": {
      "main": [
        [
          {
            "node": "Create New Message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": true,
  "settings": {
    "executionOrder": "v1",
    "binaryMode": "separate"
  },
  "versionId": "a2723336-93ed-4c00-a308-2c2c822c4cd4",
  "meta": {
    "templateCredsSetupCompleted": true,
    "instanceId": "231b62ab86295517a316dcbab72860738fdaeb87a0b16f5d61f8cedb008de282"
  },
  "id": "3HmNnMRAeTtr6TJ9",
  "tags": [
    {
      "updatedAt": "2026-04-23T19:27:59.422Z",
      "createdAt": "2026-04-23T19:27:59.422Z",
      "id": "EYMJQsVgfJqDcgnv",
      "name": "Chatfluxx"
    }
  ]
}