whatsapp bot

{
  "name": "WhatsApp Agent - OpenRouter Adapted",
  "nodes": [
    {
      "parameters": {
        "path": "whatsapp-webhook",
        "options": {}
      },
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        -640,
        -100
      ],
      "id": "webhook-receive-id",
      "name": "Webhook Receive",
      "webhookId": "whatsapp-bot-v2"
    },
    {
      "parameters": {
        "jsCode": "// Extrai dados do Webhook do WhatsApp Cloud API\nconst items = $input.all();\nconst results = [];\n\nfor (const item of items) {\n const data = item.json;\n let phone = \"\";\n let text = \"\";\n\n // Estrutura padrão do webhook\n try {\n if (data.entry && data.entry[0] && data.entry[0].changes) {\n const change = data.entry[0].changes[0];\n if (change.value && change.value.messages && change.value.messages[0]) {\n const msg = change.value.messages[0];\n phone = msg.from;\n text = msg.text ? msg.text.body : \"\";\n }\n }\n } catch (e) {}\n \n phone = (phone || \"\").replace(/\\D/g, \"\");\n\n if (phone && text) {\n results.push({\n json: {\n phone: phone,\n userText: text,\n raw: data\n }\n });\n }\n}\nreturn results;"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        -420,
        -100
      ],
      "id": "extract-message-id",
      "name": "Extract Message"
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://openrouter.ai/api/v1/chat/completions",
        "authentication": "none",
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "Bearer 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"
            },
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "body": "={{ JSON.stringify({\n \"model\": \"mistralai/mistral-7b-instruct:free\",\n \"messages\": [\n {\n \"role\": \"system\",\n \"content\": \"You are a helpful assistant called AA for Binary Light Inc. Respond in a natural and friendly tone.\"\n },\n {\n \"role\": \"user\",\n \"content\": $json.userText\n }\n ],\n \"temperature\": 0.7,\n \"max_tokens\": 150\n}) }}",
        "options": {} },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        -200,
        -100
      ],
      "id": "openrouter-ai-id",
      "name": "Call OpenRouter AI"
    },
    {
      "parameters": {
        "jsCode": "// Extrai a resposta da IA\nconst items = $input.all();\nreturn items.map(item => {\n const aiData = item.json;\n let aiText = \"Desculpe, não entendi.\";\n \n if (aiData.choices && aiData.choices[0] && aiData.choices[0].message) {\n aiText = aiData.choices[0].message.content;\n }\n \n return {\n json: {\n phone: item.json.phone,\n userText: item.json.userText,\n aiText: aiText\n }\n };\n});"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        20,
        -100
      ],
      "id": "parse-response-id",
      "name": "Parse Response"
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://graph.facebook.com/v18.0/1116551651546777/messages",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "headerParameters": {
          "parameters": [
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "body": "={{ JSON.stringify({\n \"messaging_product\": \"whatsapp\",\n \"to\": $json.phone,\n \"type\": \"text\",\n \"text\": {\n \"body\": $json.aiText\n }\n}) }}",
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        240,
        -100
      ],
      "id": "reply-whatsapp-id",
      "name": "Reply WhatsApp",
      "credentials": {
        "httpHeaderAuth": { "id": "6ZqDO1Wp5Dlsfs5N",
          "name": "Header Auth account"
        }
      }
    }
  ],
  "connections": {
    "Webhook Receive": {
      "main": [
        [
          {
            "node": "Extract Message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract Message": {
      "main": [
        [
          {
            "node": "Call OpenRouter AI",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Call OpenRouter AI": {
      "main": [
        [
          {
            "node": "Parse Response",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Parse Response": {
      "main": [
        [
          {
            "node": "Reply WhatsApp",
            "type": "main",
            "index": 0
          }
        ]
      ] }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "adapted-v1",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "id": "whatsapp-openrouter-adapted",
  "tags": []
}

Postagens mais visitadas deste blog

Política de Privacidade

Política de privacidade Odonto View

Conversor de Moedas