{
  "name": "Host AI Concierge \u2014 WhatsApp \u2192 Dify \u2192 Chatwoot",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "whatsapp",
        "responseMode": "responseNode",
        "options": {}
      },
      "id": "wh-in",
      "name": "Webhook WhatsApp (Evolution)",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        240,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "const body = $json.body ?? $json;\nconst data = body.data ?? body;\nconst remoteJid = data.key?.remoteJid ?? data.from ?? '';\nconst phone = remoteJid.split('@')[0];\nconst text = data.message?.conversation\n  ?? data.message?.extendedTextMessage?.text\n  ?? data.body\n  ?? '';\nreturn [{ json: {\n  phone, remoteJid, text,\n  instance: body.instance ?? data.instance ?? 'default',\n  pushName: data.pushName ?? data.notifyName ?? phone,\n  messageId: data.key?.id ?? null,\n  fromMe: Boolean(data.key?.fromMe)\n}}];"
      },
      "id": "fn-norm",
      "name": "Normalize",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        460,
        300
      ]
    },
    {
      "parameters": {
        "conditions": {
          "boolean": [
            {
              "value1": "={{$json.fromMe}}",
              "value2": false
            }
          ]
        }
      },
      "id": "if-notme",
      "name": "Ignorar pr\u00f3prias",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        680,
        300
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "={{$env.CHATWOOT_URL}}/public/api/v1/inboxes/{{$env.CHATWOOT_INBOX_IDENTIFIER}}/contacts",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"identifier\": \"{{$json.phone}}\",\n  \"name\": \"{{$json.pushName}}\",\n  \"phone_number\": \"+{{$json.phone}}\"\n}",
        "options": {
          "response": {
            "response": {
              "neverError": true
            }
          }
        }
      },
      "id": "cw-contact",
      "name": "Chatwoot \u00b7 upsert contact",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        900,
        200
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "={{$env.CHATWOOT_URL}}/public/api/v1/inboxes/{{$env.CHATWOOT_INBOX_IDENTIFIER}}/contacts/{{$json.source_id}}/conversations",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={}"
      },
      "id": "cw-conv",
      "name": "Chatwoot \u00b7 open conversation",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1120,
        200
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "={{$env.CHATWOOT_URL}}/public/api/v1/inboxes/{{$env.CHATWOOT_INBOX_IDENTIFIER}}/contacts/{{$node['Chatwoot \u00b7 upsert contact'].json.source_id}}/conversations/{{$json.id}}/messages",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"content\": \"{{$node['Normalize'].json.text}}\",\n  \"message_type\": \"incoming\"\n}"
      },
      "id": "cw-msg-in",
      "name": "Chatwoot \u00b7 push incoming",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1340,
        200
      ]
    },
    {
      "parameters": {
        "method": "GET",
        "url": "={{$env.CHATWOOT_URL}}/api/v1/accounts/{{$env.CHATWOOT_ACCOUNT_ID}}/conversations/{{$node['Chatwoot \u00b7 open conversation'].json.id}}",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "api_access_token",
              "value": "={{$env.CHATWOOT_API_TOKEN}}"
            }
          ]
        }
      },
      "id": "cw-get",
      "name": "Chatwoot \u00b7 fetch conversation",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1560,
        200
      ]
    },
    {
      "parameters": {
        "conditions": {
          "boolean": [
            {
              "value1": "={{ $json.custom_attributes && $json.custom_attributes.ai_handling === false ? false : true }}",
              "value2": true
            }
          ]
        }
      },
      "id": "if-ai",
      "name": "AI handling?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        1780,
        200
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "={{$env.DIFY_URL}}/v1/chat-messages",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "Bearer {{$env.DIFY_API_KEY}}"
            },
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"inputs\": {},\n  \"query\": \"{{$node['Normalize'].json.text}}\",\n  \"user\": \"wa-{{$node['Normalize'].json.phone}}\",\n  \"response_mode\": \"blocking\",\n  \"conversation_id\": \"\"\n}"
      },
      "id": "dify",
      "name": "Dify \u00b7 ask RAG",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        2000,
        100
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "={{$env.EVOLUTION_URL}}/message/sendText/{{$node['Normalize'].json.instance}}",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "apikey",
              "value": "={{$env.EVOLUTION_API_KEY}}"
            },
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"number\": \"{{$node['Normalize'].json.phone}}\",\n  \"text\": \"{{$json.answer}}\"\n}"
      },
      "id": "evo-reply",
      "name": "Evolution \u00b7 reply",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        2220,
        100
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "={{$env.CHATWOOT_URL}}/api/v1/accounts/{{$env.CHATWOOT_ACCOUNT_ID}}/conversations/{{$node['Chatwoot \u00b7 open conversation'].json.id}}/messages",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "api_access_token",
              "value": "={{$env.CHATWOOT_API_TOKEN}}"
            },
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"content\": \"{{$node['Dify \u00b7 ask RAG'].json.answer}}\",\n  \"message_type\": \"outgoing\",\n  \"private\": false,\n  \"content_attributes\": { \"ai\": true, \"reasoning\": \"Resposta gerada por Concierge IA via Dify\" }\n}"
      },
      "id": "cw-msg-out",
      "name": "Chatwoot \u00b7 log AI reply",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        2440,
        100
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "={{$env.CHATWOOT_URL}}/api/v1/accounts/{{$env.CHATWOOT_ACCOUNT_ID}}/conversations/{{$node['Chatwoot \u00b7 open conversation'].json.id}}/messages",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "api_access_token",
              "value": "={{$env.CHATWOOT_API_TOKEN}}"
            },
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"content\": \"[automation] IA pausada \u2014 aguardando atendimento humano.\",\n  \"message_type\": \"outgoing\",\n  \"private\": true\n}"
      },
      "id": "cw-paused",
      "name": "Chatwoot \u00b7 note (IA pausada)",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        2000,
        320
      ]
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={ \"ok\": true }"
      },
      "id": "ok",
      "name": "OK",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1,
      "position": [
        2660,
        200
      ]
    }
  ],
  "connections": {
    "Webhook WhatsApp (Evolution)": {
      "main": [
        [
          {
            "node": "Normalize",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Normalize": {
      "main": [
        [
          {
            "node": "Ignorar pr\u00f3prias",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Ignorar pr\u00f3prias": {
      "main": [
        [
          {
            "node": "Chatwoot \u00b7 upsert contact",
            "type": "main",
            "index": 0
          }
        ],
        []
      ]
    },
    "Chatwoot \u00b7 upsert contact": {
      "main": [
        [
          {
            "node": "Chatwoot \u00b7 open conversation",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Chatwoot \u00b7 open conversation": {
      "main": [
        [
          {
            "node": "Chatwoot \u00b7 push incoming",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Chatwoot \u00b7 push incoming": {
      "main": [
        [
          {
            "node": "Chatwoot \u00b7 fetch conversation",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Chatwoot \u00b7 fetch conversation": {
      "main": [
        [
          {
            "node": "AI handling?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "AI handling?": {
      "main": [
        [
          {
            "node": "Dify \u00b7 ask RAG",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Chatwoot \u00b7 note (IA pausada)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Dify \u00b7 ask RAG": {
      "main": [
        [
          {
            "node": "Evolution \u00b7 reply",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Evolution \u00b7 reply": {
      "main": [
        [
          {
            "node": "Chatwoot \u00b7 log AI reply",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Chatwoot \u00b7 log AI reply": {
      "main": [
        [
          {
            "node": "OK",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Chatwoot \u00b7 note (IA pausada)": {
      "main": [
        [
          {
            "node": "OK",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  }
}