{
  "name": "WhatsApp IA com Audio",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "whatsapp-webhook",
        "options": {}
      },
      "name": "Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 1,
      "position": [
        -860,
        400
      ],
      "id": "webhook-whatsapp"
    },
    {
      "parameters": {
        "jsCode": "// Extrair dados do webhook incluindo \u00e1udio\nlet message = \"\";\nlet from = \"\";\nlet profileName = \"\";\nlet waId = \"\";\nlet destinatario = \"\";\nlet mediaUrl = \"\";\nlet mediaType = \"\";\n\nconst body = $input.first().json.body || $input.first().json;\n\nif (body.Body) {\n  message = body.Body || \"\";\n  from = body.From || \"\";\n  profileName = body.ProfileName || \"\";\n  waId = body.WaId || \"\";\n  destinatario = body.To;\n  \n  // Verificar se \u00e9 \u00e1udio\n  if (body.MediaUrl0) {\n    mediaUrl = body.MediaUrl0;\n    mediaType = body.MediaContentType0 || \"\";\n  }\n}\n\nreturn [{\n  json: {\n    message: message,\n    from: from,\n    profileName: profileName,\n    waId: \"+\" + waId,\n    destinatario: destinatario,\n    mediaUrl: mediaUrl,\n    mediaType: mediaType,\n    isAudio: mediaType.includes('audio')\n  }\n}];"
      },
      "name": "Extrair Dados",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        -640,
        400
      ],
      "id": "extrair-dados"
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict"
          },
          "conditions": [
            {
              "leftValue": "={{$json.isAudio}}",
              "rightValue": true,
              "operator": {
                "type": "boolean",
                "operation": "equals"
              }
            }
          ]
        }
      },
      "name": "\u00c9 \u00c1udio?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [
        -420,
        400
      ],
      "id": "check-audio"
    },
    {
      "parameters": {
        "method": "GET",
        "url": "={{$json.mediaUrl}}",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpBasicAuth",
        "options": {
          "response": {
            "response": {
              "responseFormat": "file"
            }
          }
        }
      },
      "name": "Baixar \u00c1udio",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4,
      "position": [
        -200,
        300
      ],
      "id": "download-audio",
      "credentials": {
        "httpBasicAuth": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "model": "whisper-1",
        "options": {}
      },
      "name": "Transcrever \u00c1udio",
      "type": "@n8n/n8n-nodes-langchain.openAiWhisper",
      "typeVersion": 1,
      "position": [
        20,
        300
      ],
      "id": "transcribe-audio",
      "credentials": {
        "openAiApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "// Combinar dados do \u00e1udio transcrito\nconst audioData = $input.first().json;\nconst originalData = $node[\"Extrair Dados\"].json;\n\nreturn [{\n  json: {\n    ...originalData,\n    message: audioData.text,\n    isTranscribed: true\n  }\n}];"
      },
      "name": "Processar Transcri\u00e7\u00e3o",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        240,
        300
      ],
      "id": "process-transcription"
    },
    {
      "parameters": {
        "sessionIdType": "customKey",
        "sessionKey": "={{ $json.waId}}"
      },
      "type": "@n8n/n8n-nodes-langchain.memoryBufferWindow",
      "typeVersion": 1.3,
      "position": [
        680,
        400
      ],
      "id": "memory",
      "name": "Simple Memory"
    },
    {
      "parameters": {
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.chatOpenAi",
      "typeVersion": 1,
      "position": [
        900,
        400
      ],
      "id": "chat-openai",
      "name": "Message an assistant",
      "credentials": {
        "openAiApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict"
          },
          "conditions": [
            {
              "leftValue": "={{$node[\"Extrair Dados\"].json.isAudio}}",
              "rightValue": true,
              "operator": {
                "type": "boolean",
                "operation": "equals"
              }
            }
          ]
        }
      },
      "name": "Responder com \u00c1udio?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [
        1120,
        400
      ],
      "id": "check-audio-response"
    },
    {
      "parameters": {
        "model": "tts-1",
        "input": "={{$node[\"Message an assistant\"].json.output}}",
        "voice": "alloy",
        "options": {}
      },
      "name": "Gerar \u00c1udio",
      "type": "@n8n/n8n-nodes-langchain.openAiTts",
      "typeVersion": 1,
      "position": [
        1340,
        300
      ],
      "id": "generate-audio",
      "credentials": {
        "openAiApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "from": "={{$node[\"Webhook\"].json.body.To.replaceAll('whatsapp:', '')}}",
        "to": "={{$node[\"Extrair Dados\"].json.waId}}",
        "toWhatsapp": true,
        "mediaUrl": "={{$node[\"Gerar \u00c1udio\"].json.url}}",
        "options": {}
      },
      "name": "Enviar \u00c1udio",
      "type": "n8n-nodes-base.twilio",
      "typeVersion": 1,
      "position": [
        1560,
        300
      ],
      "id": "send-audio",
      "credentials": {
        "twilioApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "from": "={{$node[\"Webhook\"].json.body.To.replaceAll('whatsapp:', '')}}",
        "to": "={{$node[\"Extrair Dados\"].json.waId}}",
        "toWhatsapp": true,
        "message": "={{$node[\"Message an assistant\"].json.output}}",
        "options": {}
      },
      "name": "Enviar Texto",
      "type": "n8n-nodes-base.twilio",
      "typeVersion": 1,
      "position": [
        1560,
        500
      ],
      "id": "send-text",
      "credentials": {
        "twilioApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "method": "POST",
        "url": "=http://supabase-server:3001/api/registrar-mensagem",
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "whatsappNumero",
              "value": "={{$node[\"Extrair Dados\"].json.from.replace(\"whatsapp:\", \"\")}}"
            },
            {
              "name": "pergunta",
              "value": "={{$node[\"Extrair Dados\"].json.message}}"
            },
            {
              "name": "resposta",
              "value": "={{$node[\"Message an assistant\"].json.output}}"
            },
            {
              "name": "numeroDestino",
              "value": "={{$node[\"Webhook\"].json.body.To.replaceAll('whatsapp:','')}}"
            },
            {
              "name": "nome",
              "value": "={{$node[\"Extrair Dados\"].json.profileName}}"
            },
            {
              "name": "tipoMensagem",
              "value": "={{$node[\"Extrair Dados\"].json.isAudio ? 'audio' : 'texto'}}"
            }
          ]
        }
      },
      "name": "Registrar Mensagem",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 3,
      "position": [
        1780,
        400
      ],
      "id": "register-message"
    }
  ],
  "connections": {
    "Webhook": {
      "main": [
        [
          {
            "node": "Extrair Dados",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extrair Dados": {
      "main": [
        [
          {
            "node": "\u00c9 \u00c1udio?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "\u00c9 \u00c1udio?": {
      "main": [
        [
          {
            "node": "Baixar \u00c1udio",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Simple Memory",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Baixar \u00c1udio": {
      "main": [
        [
          {
            "node": "Transcrever \u00c1udio",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Transcrever \u00c1udio": {
      "main": [
        [
          {
            "node": "Processar Transcri\u00e7\u00e3o",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Processar Transcri\u00e7\u00e3o": {
      "main": [
        [
          {
            "node": "Simple Memory",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Simple Memory": {
      "main": [
        [
          {
            "node": "Message an assistant",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Message an assistant": {
      "main": [
        [
          {
            "node": "Responder com \u00c1udio?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Responder com \u00c1udio?": {
      "main": [
        [
          {
            "node": "Gerar \u00c1udio",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Enviar Texto",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Gerar \u00c1udio": {
      "main": [
        [
          {
            "node": "Enviar \u00c1udio",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Enviar \u00c1udio": {
      "main": [
        [
          {
            "node": "Registrar Mensagem",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Enviar Texto": {
      "main": [
        [
          {
            "node": "Registrar Mensagem",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}