{
  "name": "WaLink AI Chatbot - Text, Voice, Images & PDFs",
  "meta": { "templateCredsSetupCompleted": false },
  "nodes": [
    {
      "id": "node-sticky-setup",
      "name": "Setup Instructions",
      "type": "n8n-nodes-base.stickyNote",
      "position": [-1100, -400],
      "parameters": {
        "color": 3,
        "width": 560,
        "height": 820,
        "content": "## ⚙️ WaLink AI Chatbot Setup\n\n### 1. Configure WaLink Webhook URL\nIn your WaLink dashboard, set the webhook URL to:\n```\nhttps://YOUR-N8N-URL/webhook/walink-ai\n```\n\n### 2. Set Your WaLink URL\nIn every **HTTP Request** node that sends replies, update the URL from:\n```\nhttp://localhost:3000/api/messages/send\n```\nTo your actual WaLink URL (e.g., ngrok or VPS IP).\n\n### 3. Add OpenAI Credentials\nAll **OpenAI nodes** need an `openAiApi` credential with your API key.\n\n### 4. WaLink Webhook Payload\nThis workflow expects WaLink to send:\n```json\n{\n  \"event\": \"message.received\",\n  \"sessionId\": \"your-session\",\n  \"from\": \"1234@s.whatsapp.net\",\n  \"type\": \"text\",\n  \"content\": \"Hello!\",\n  \"mediaBase64\": null,\n  \"mediaMimeType\": null\n}\n```\nMake sure you are running **WaLink v2+** which includes `mediaBase64` support.\n\n### 5. Supported Message Types\n- ✅ Text messages\n- ✅ Voice/Audio messages (transcribed + AI responds as voice)\n- ✅ Images (described by AI)\n- ✅ PDF Documents (extracted + analyzed by AI)"
      },
      "typeVersion": 1
    },
    {
      "id": "node-sticky-text",
      "name": "Sticky Note Text",
      "type": "n8n-nodes-base.stickyNote",
      "position": [80, -560],
      "parameters": { "width": 760, "height": 200, "content": "## 📝 Text" },
      "typeVersion": 1
    },
    {
      "id": "node-sticky-voice",
      "name": "Sticky Note Voice",
      "type": "n8n-nodes-base.stickyNote",
      "position": [80, -260],
      "parameters": { "width": 1060, "height": 200, "content": "## 🎤 Voice" },
      "typeVersion": 1
    },
    {
      "id": "node-sticky-image",
      "name": "Sticky Note Image",
      "type": "n8n-nodes-base.stickyNote",
      "position": [80, 60],
      "parameters": { "width": 1060, "height": 200, "content": "## 🖼️ Image" },
      "typeVersion": 1
    },
    {
      "id": "node-sticky-doc",
      "name": "Sticky Note Document",
      "type": "n8n-nodes-base.stickyNote",
      "position": [80, 360],
      "parameters": { "width": 1060, "height": 320, "content": "## 📄 Document (PDF Only)" },
      "typeVersion": 1
    },
    {
      "id": "node-sticky-response",
      "name": "Sticky Note Response",
      "type": "n8n-nodes-base.stickyNote",
      "position": [80, 740],
      "parameters": { "color": 5, "width": 1460, "height": 560, "content": "## 💬 AI Response" },
      "typeVersion": 1
    },
    {
      "id": "node-webhook",
      "name": "WaLink Webhook",
      "type": "n8n-nodes-base.webhook",
      "position": [-700, 80],
      "webhookId": "walink-ai-chatbot",
      "parameters": {
        "path": "walink-ai",
        "httpMethod": "POST",
        "responseMode": "onReceived",
        "options": {}
      },
      "typeVersion": 2
    },
    {
      "id": "node-switch",
      "name": "Input type",
      "type": "n8n-nodes-base.switch",
      "position": [-420, 80],
      "parameters": {
        "rules": {
          "values": [
            {
              "outputKey": "Text",
              "renameOutput": true,
              "conditions": {
                "options": { "version": 2, "caseSensitive": true, "typeValidation": "strict" },
                "combinator": "and",
                "conditions": [{ "id": "s1", "operator": { "type": "string", "operation": "equals" }, "leftValue": "={{ $json.body.type }}", "rightValue": "text" }]
              }
            },
            {
              "outputKey": "Voice",
              "renameOutput": true,
              "conditions": {
                "options": { "version": 2, "caseSensitive": true, "typeValidation": "strict" },
                "combinator": "and",
                "conditions": [{ "id": "s2", "operator": { "type": "string", "operation": "equals" }, "leftValue": "={{ $json.body.type }}", "rightValue": "audio" }]
              }
            },
            {
              "outputKey": "Image",
              "renameOutput": true,
              "conditions": {
                "options": { "version": 2, "caseSensitive": true, "typeValidation": "strict" },
                "combinator": "and",
                "conditions": [{ "id": "s3", "operator": { "type": "string", "operation": "equals" }, "leftValue": "={{ $json.body.type }}", "rightValue": "image" }]
              }
            },
            {
              "outputKey": "Document",
              "renameOutput": true,
              "conditions": {
                "options": { "version": 2, "caseSensitive": true, "typeValidation": "strict" },
                "combinator": "and",
                "conditions": [{ "id": "s4", "operator": { "type": "string", "operation": "equals" }, "leftValue": "={{ $json.body.type }}", "rightValue": "document" }]
              }
            }
          ]
        },
        "options": { "fallbackOutput": "extra" }
      },
      "typeVersion": 3.2
    },
    {
      "id": "node-not-supported",
      "name": "Not Supported",
      "type": "n8n-nodes-base.httpRequest",
      "position": [-200, 380],
      "parameters": {
        "method": "POST",
        "url": "http://localhost:3000/api/messages/send",
        "sendBody": true,
        "contentType": "json",
        "bodyParameters": {
          "parameters": [
            { "name": "sessionId", "value": "={{ $json.body.sessionId }}" },
            { "name": "to", "value": "={{ $json.body.from }}" },
            { "name": "type", "value": "text" },
            { "name": "text", "value": "Sorry, I can only handle text messages, images, voice notes and PDF documents." }
          ]
        },
        "options": {}
      },
      "typeVersion": 4.2
    },
    {
      "id": "node-text",
      "name": "Text",
      "type": "n8n-nodes-base.set",
      "position": [120, -440],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [{ "id": "t1", "name": "text", "type": "string", "value": "={{ $('WaLink Webhook').item.json.body.content }}" }]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "node-audio-binary",
      "name": "Audio to Binary",
      "type": "n8n-nodes-base.code",
      "position": [120, -160],
      "parameters": {
        "jsCode": "const base64Data = $('WaLink Webhook').item.json.body.mediaBase64;\nif (!base64Data) throw new Error('No media data in webhook payload. Ensure WaLink is v2+ with media download enabled.');\nconst mimeType = $('WaLink Webhook').item.json.body.mediaMimeType || 'audio/ogg; codecs=opus';\nconst ext = mimeType.includes('ogg') ? 'ogg' : mimeType.includes('mp4') ? 'mp4' : mimeType.includes('mpeg') ? 'mp3' : 'audio';\nconst buffer = Buffer.from(base64Data, 'base64');\nconst binaryData = await this.helpers.prepareBinaryData(buffer, `audio.${ext}`, mimeType);\nreturn [{ json: $input.item.json, binary: { data: binaryData } }];"
      },
      "typeVersion": 2
    },
    {
      "id": "node-transcribe",
      "name": "Transcribe Audio",
      "type": "@n8n/n8n-nodes-langchain.openAi",
      "position": [360, -160],
      "parameters": { "options": {}, "resource": "audio", "operation": "transcribe" },
      "credentials": { "openAiApi": { "id": "credential-id", "name": "openAiApi Credential" } },
      "typeVersion": 1.8
    },
    {
      "id": "node-audio",
      "name": "Audio",
      "type": "n8n-nodes-base.set",
      "position": [600, -160],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [{ "id": "a1", "name": "text", "type": "string", "value": "={{ $json.text }}" }]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "node-image-binary",
      "name": "Image to Binary",
      "type": "n8n-nodes-base.code",
      "position": [120, 140],
      "parameters": {
        "jsCode": "const base64Data = $('WaLink Webhook').item.json.body.mediaBase64;\nif (!base64Data) throw new Error('No media data in webhook payload. Ensure WaLink is v2+ with media download enabled.');\nconst mimeType = $('WaLink Webhook').item.json.body.mediaMimeType || 'image/jpeg';\nconst ext = mimeType.includes('png') ? 'png' : mimeType.includes('gif') ? 'gif' : mimeType.includes('webp') ? 'webp' : 'jpg';\nconst buffer = Buffer.from(base64Data, 'base64');\nconst binaryData = await this.helpers.prepareBinaryData(buffer, `image.${ext}`, mimeType);\nreturn [{ json: $input.item.json, binary: { data: binaryData } }];"
      },
      "typeVersion": 2
    },
    {
      "id": "node-analyze",
      "name": "Analyze Image",
      "type": "@n8n/n8n-nodes-langchain.openAi",
      "position": [380, 140],
      "parameters": {
        "text": "You are an advanced image description AI assistant . Your primary function is to provide detailed, accurate descriptions of images submitted through WhatsApp.\n\nCORE FUNCTIONALITY:\n- When presented with an image, you will analyze it thoroughly and provide a comprehensive description in English.\n- Your descriptions should capture both the obvious and subtle elements within the image.\n\nIMAGE DESCRIPTION GUIDELINES:\n- Begin with a broad overview of what the image contains\n- Describe key subjects, people, objects, and their relationships\n- Note significant visual elements such as colors, lighting, composition, and perspective\n- Identify any text visible in the image\n- Describe the setting or environment\n- Mention any notable actions or events taking place\n- Comment on mood, tone, or atmosphere when relevant\n- If applicable, identify landmarks, famous people, or cultural references\n\nRESPONSE FORMAT:\n- Start with \"Image Description:\" followed by your analysis\n- Structure your description in a logical manner (general to specific)\n- Use clear, precise language appropriate for visual description\n- Format longer descriptions with paragraphs to enhance readability\n- End with any notable observations that might require special attention\n\nLIMITATIONS:\n- If the image is blurry, low resolution, or difficult to interpret, acknowledge these limitations\n- If an image contains potentially sensitive content, provide a factual description without judgment\n- Do not make assumptions about elements that cannot be clearly determined\n\nYour descriptions should be informative, objective, and thorough, enabling someone who cannot see the image to form an accurate mental picture of its contents.",
        "modelId": { "__rl": true, "mode": "list", "value": "gpt-4o-mini", "cachedResultName": "GPT-4O-MINI" },
        "options": { "detail": "auto" },
        "resource": "image",
        "inputType": "base64",
        "operation": "analyze"
      },
      "credentials": { "openAiApi": { "id": "credential-id", "name": "openAiApi Credential" } },
      "typeVersion": 1.8
    },
    {
      "id": "node-image",
      "name": "Image",
      "type": "n8n-nodes-base.set",
      "position": [640, 140],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [{
            "id": "i1", "name": "text", "type": "string",
            "value": "=User request on the image:\n{{ $('WaLink Webhook').item.json.body.content || 'Describe the following image' }}\n\nImage description:\n{{ $json.content }}"
          }]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "node-pdf-check",
      "name": "Only PDF File",
      "type": "n8n-nodes-base.if",
      "position": [120, 440],
      "parameters": {
        "options": {},
        "conditions": {
          "options": { "version": 2, "caseSensitive": true, "typeValidation": "strict" },
          "combinator": "and",
          "conditions": [{
            "id": "p1",
            "operator": { "name": "filter.operator.equals", "type": "string", "operation": "equals" },
            "leftValue": "={{ $json.body.mediaMimeType }}",
            "rightValue": "application/pdf"
          }]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "node-doc-binary",
      "name": "Document to Binary",
      "type": "n8n-nodes-base.code",
      "position": [360, 420],
      "parameters": {
        "jsCode": "const base64Data = $('WaLink Webhook').item.json.body.mediaBase64;\nif (!base64Data) throw new Error('No media data in webhook payload.');\nconst filename = $('WaLink Webhook').item.json.body.content || 'document.pdf';\nconst buffer = Buffer.from(base64Data, 'base64');\nconst binaryData = await this.helpers.prepareBinaryData(buffer, filename, 'application/pdf');\nreturn [{ json: $input.item.json, binary: { data: binaryData } }];"
      },
      "typeVersion": 2
    },
    {
      "id": "node-extract",
      "name": "Extract from File",
      "type": "n8n-nodes-base.extractFromFile",
      "position": [600, 420],
      "parameters": { "options": {}, "operation": "pdf" },
      "typeVersion": 1
    },
    {
      "id": "node-file",
      "name": "File",
      "type": "n8n-nodes-base.set",
      "position": [840, 420],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [{
            "id": "f1", "name": "text", "type": "string",
            "value": "=User request on the file:\n{{ $('WaLink Webhook').item.json.body.content || 'Analyze this document' }}\n\nFile content:\n{{ $json.text }}"
          }]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "node-not-pdf",
      "name": "Incorrect Format",
      "type": "n8n-nodes-base.httpRequest",
      "position": [360, 620],
      "parameters": {
        "method": "POST",
        "url": "http://localhost:3000/api/messages/send",
        "sendBody": true,
        "contentType": "json",
        "bodyParameters": {
          "parameters": [
            { "name": "sessionId", "value": "={{ $('WaLink Webhook').item.json.body.sessionId }}" },
            { "name": "to", "value": "={{ $('WaLink Webhook').item.json.body.from }}" },
            { "name": "type", "value": "text" },
            { "name": "text", "value": "Sorry, I can only analyze PDF files. Please send a PDF document." }
          ]
        },
        "options": {}
      },
      "typeVersion": 4.2
    },
    {
      "id": "node-ai",
      "name": "AI Agent",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [1060, 200],
      "parameters": {
        "text": "={{ $json.text }}",
        "options": {
          "systemMessage": "You are an intelligent assistant. Your purpose is to analyze various types of input and provide helpful, accurate responses.\n\nCAPABILITIES:\n- Process and respond to text messages\n- Analyze uploaded files\n- Interpret and describe images\n- Transcribe and understand voice messages\n\nINPUT HANDLING:\n1. For text messages: Analyze the content, understand the intent, and provide a relevant response.\n2. For file analysis: Examine the file content, extract key information, and summarize important points also based on the questions asked.\n3. For image analysis: Describe what you see in the image, identify key elements, and respond to any questions about the image.\n4. For voice messages: Transcribe the audio, understand the message, and respond appropriately.\n\nRESPONSE GUIDELINES:\n- Be concise but thorough\n- Prioritize accuracy over speculation\n- Maintain a professional and helpful tone\n- When uncertain, acknowledge limitations\n- Format responses for easy reading on mobile devices\n- Include actionable information when appropriate\n\nLIMITATIONS:\n- Mention if you're unable to process certain file formats\n- Indicate if an image is unclear or if details are difficult to discern\n- Note if audio quality impacts transcription accuracy\n\nSECURITY & PRIVACY:\n- Do not store or remember sensitive information shared in files, images, or voice notes\n- Do not share personal information across different user interactions\n- Inform users about data privacy limitations when relevant\n\nAnalyze all inputs carefully before responding. Your goal is to provide value through accurate information and helpful assistance."
        },
        "promptType": "define"
      },
      "typeVersion": 1.8
    },
    {
      "id": "node-llm",
      "name": "OpenAI Chat Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "position": [1060, 420],
      "parameters": {
        "model": { "__rl": true, "mode": "list", "value": "gpt-4o-mini" },
        "options": {}
      },
      "credentials": { "openAiApi": { "id": "credential-id", "name": "openAiApi Credential" } },
      "typeVersion": 1.2
    },
    {
      "id": "node-memory",
      "name": "Simple Memory",
      "type": "@n8n/n8n-nodes-langchain.memoryBufferWindow",
      "position": [1260, 420],
      "parameters": {
        "sessionKey": "=memory_{{ $('WaLink Webhook').item.json.body.from }}",
        "sessionIdType": "customKey",
        "contextWindowLength": 10
      },
      "typeVersion": 1.3
    },
    {
      "id": "node-audio-check",
      "name": "From audio to audio?",
      "type": "n8n-nodes-base.if",
      "position": [1320, 200],
      "parameters": {
        "options": {},
        "conditions": {
          "options": { "version": 2, "caseSensitive": true, "typeValidation": "strict" },
          "combinator": "and",
          "conditions": [{
            "id": "ac1",
            "operator": { "type": "string", "operation": "equals" },
            "leftValue": "={{ $('WaLink Webhook').item.json.body.type }}",
            "rightValue": "audio"
          }]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "node-tts",
      "name": "Generate Audio Response",
      "type": "@n8n/n8n-nodes-langchain.openAi",
      "position": [1560, 60],
      "parameters": {
        "input": "={{ $('AI Agent').item.json.output }}",
        "voice": "onyx",
        "options": {},
        "resource": "audio"
      },
      "credentials": { "openAiApi": { "id": "credential-id", "name": "openAiApi Credential" } },
      "typeVersion": 1.8
    },
    {
      "id": "node-fix-mime",
      "name": "Fix mimeType for Audio",
      "type": "n8n-nodes-base.code",
      "position": [1800, 60],
      "parameters": {
        "jsCode": "for (const item of $input.all()) {\n  if (item.binary) {\n    const binaryPropertyNames = Object.keys(item.binary);\n    for (const propName of binaryPropertyNames) {\n      if (item.binary[propName].mimeType === 'audio/mp3') {\n        item.binary[propName].mimeType = 'audio/mpeg';\n      }\n    }\n  }\n}\nreturn $input.all();"
      },
      "typeVersion": 2
    },
    {
      "id": "node-audio-b64",
      "name": "Audio to Base64",
      "type": "n8n-nodes-base.code",
      "position": [2040, 60],
      "parameters": {
        "jsCode": "const binaryKey = Object.keys($input.item.binary)[0];\nconst base64 = $input.item.binary[binaryKey].data;\nreturn [{ json: { ...($input.item.json), audioBase64: base64 } }];"
      },
      "typeVersion": 2
    },
    {
      "id": "node-send-audio",
      "name": "Send Audio Reply",
      "type": "n8n-nodes-base.httpRequest",
      "position": [2280, 60],
      "parameters": {
        "method": "POST",
        "url": "http://localhost:3000/api/messages/send",
        "sendBody": true,
        "contentType": "json",
        "bodyParameters": {
          "parameters": [
            { "name": "sessionId", "value": "={{ $('WaLink Webhook').item.json.body.sessionId }}" },
            { "name": "to", "value": "={{ $('WaLink Webhook').item.json.body.from }}" },
            { "name": "type", "value": "audio" },
            { "name": "base64", "value": "={{ $json.audioBase64 }}" },
            { "name": "voiceNote", "value": "=true" }
          ]
        },
        "options": {}
      },
      "typeVersion": 4.2
    },
    {
      "id": "node-send-text",
      "name": "Send Text Reply",
      "type": "n8n-nodes-base.httpRequest",
      "position": [1560, 340],
      "parameters": {
        "method": "POST",
        "url": "http://localhost:3000/api/messages/send",
        "sendBody": true,
        "contentType": "json",
        "bodyParameters": {
          "parameters": [
            { "name": "sessionId", "value": "={{ $('WaLink Webhook').item.json.body.sessionId }}" },
            { "name": "to", "value": "={{ $('WaLink Webhook').item.json.body.from }}" },
            { "name": "type", "value": "text" },
            { "name": "text", "value": "={{ $('AI Agent').item.json.output }}" }
          ]
        },
        "options": {}
      },
      "typeVersion": 4.2
    }
  ],
  "connections": {
    "WaLink Webhook": {
      "main": [[{ "node": "Input type", "type": "main", "index": 0 }]]
    },
    "Input type": {
      "main": [
        [{ "node": "Text", "type": "main", "index": 0 }],
        [{ "node": "Audio to Binary", "type": "main", "index": 0 }],
        [{ "node": "Image to Binary", "type": "main", "index": 0 }],
        [{ "node": "Only PDF File", "type": "main", "index": 0 }],
        [{ "node": "Not Supported", "type": "main", "index": 0 }]
      ]
    },
    "Text": {
      "main": [[{ "node": "AI Agent", "type": "main", "index": 0 }]]
    },
    "Audio to Binary": {
      "main": [[{ "node": "Transcribe Audio", "type": "main", "index": 0 }]]
    },
    "Transcribe Audio": {
      "main": [[{ "node": "Audio", "type": "main", "index": 0 }]]
    },
    "Audio": {
      "main": [[{ "node": "AI Agent", "type": "main", "index": 0 }]]
    },
    "Image to Binary": {
      "main": [[{ "node": "Analyze Image", "type": "main", "index": 0 }]]
    },
    "Analyze Image": {
      "main": [[{ "node": "Image", "type": "main", "index": 0 }]]
    },
    "Image": {
      "main": [[{ "node": "AI Agent", "type": "main", "index": 0 }]]
    },
    "Only PDF File": {
      "main": [
        [{ "node": "Document to Binary", "type": "main", "index": 0 }],
        [{ "node": "Incorrect Format", "type": "main", "index": 0 }]
      ]
    },
    "Document to Binary": {
      "main": [[{ "node": "Extract from File", "type": "main", "index": 0 }]]
    },
    "Extract from File": {
      "main": [[{ "node": "File", "type": "main", "index": 0 }]]
    },
    "File": {
      "main": [[{ "node": "AI Agent", "type": "main", "index": 0 }]]
    },
    "AI Agent": {
      "main": [[{ "node": "From audio to audio?", "type": "main", "index": 0 }]]
    },
    "OpenAI Chat Model": {
      "ai_languageModel": [[{ "node": "AI Agent", "type": "ai_languageModel", "index": 0 }]]
    },
    "Simple Memory": {
      "ai_memory": [[{ "node": "AI Agent", "type": "ai_memory", "index": 0 }]]
    },
    "From audio to audio?": {
      "main": [
        [{ "node": "Generate Audio Response", "type": "main", "index": 0 }],
        [{ "node": "Send Text Reply", "type": "main", "index": 0 }]
      ]
    },
    "Generate Audio Response": {
      "main": [[{ "node": "Fix mimeType for Audio", "type": "main", "index": 0 }]]
    },
    "Fix mimeType for Audio": {
      "main": [[{ "node": "Audio to Base64", "type": "main", "index": 0 }]]
    },
    "Audio to Base64": {
      "main": [[{ "node": "Send Audio Reply", "type": "main", "index": 0 }]]
    }
  },
  "settings": { "executionOrder": "v1" },
  "pinData": {},
  "active": false
}
