Whatsapp AI Agent

This commit is contained in:
Leon van Zyl
2025-04-16 14:54:47 +02:00
commit 1e1c47bd92
2 changed files with 949 additions and 0 deletions

View File

@@ -0,0 +1,34 @@
# WhatsApp AI Agent with n8n
A project based on the n8n tutorial for integrating AI agents with WhatsApp.
## Overview
This project demonstrates how to set up an AI-powered WhatsApp bot using n8n. With this implementation, you can create automated responses and intelligent interactions through WhatsApp messaging.
## Tutorial
This implementation follows the tutorial available on YouTube:
[Adding AI Agents to WhatsApp using n8n](https://youtu.be/dZe6dKMS-vY)
## Fix mime type
```javascript
// Loop over input items and change the MIME type of binary data
for (const item of $input.all()) {
// Check if the item has binary data
if (item.binary) {
// Find the binary property name (assuming there's at least one)
const binaryPropertyNames = Object.keys(item.binary);
for (const propName of binaryPropertyNames) {
// If the MIME type is 'audio/mp3', change it to 'audio/mpeg'
if (item.binary[propName].mimeType === "audio/mp3") {
item.binary[propName].mimeType = "audio/mpeg";
}
}
}
}
return $input.all();
```

View File

@@ -0,0 +1,915 @@
{
"name": "WhatsApp Agent",
"nodes": [
{
"parameters": {
"updates": [
"messages"
],
"options": {}
},
"type": "n8n-nodes-base.whatsAppTrigger",
"typeVersion": 1,
"position": [
-1180,
-120
],
"id": "1dcc9ce2-27a4-4308-86d6-311677748af8",
"name": "WhatsApp Trigger",
"webhookId": "d3978cae-2aca-4553-8ac7-ab89068deabc",
"credentials": {
"whatsAppTriggerApi": {
"id": "Om4ztypiSPKA0nMQ",
"name": "WhatsApp OAuth account"
}
}
},
{
"parameters": {
"promptType": "define",
"text": "={{ $json.text }}",
"options": {
"systemMessage": "=You are a helpful assistant called Sam.\n\nYou are currently talking to {{ $('WhatsApp Trigger').item.json.contacts[0].profile.name }}\n\nThe current date and time is {{ $now.toString() }}"
}
},
"type": "@n8n/n8n-nodes-langchain.agent",
"typeVersion": 1.9,
"position": [
-520,
0
],
"id": "98b8830a-20df-4633-a1fc-f8fa959c874e",
"name": "AI Agent"
},
{
"parameters": {
"sessionIdType": "customKey",
"sessionKey": "={{ $('WhatsApp Trigger').item.json.messages[0].from }}",
"contextWindowLength": 20
},
"type": "@n8n/n8n-nodes-langchain.memoryBufferWindow",
"typeVersion": 1.3,
"position": [
-600,
200
],
"id": "2f67e2e7-93c3-4341-954b-72241dc683f3",
"name": "Simple Memory"
},
{
"parameters": {
"rules": {
"values": [
{
"conditions": {
"options": {
"caseSensitive": true,
"leftValue": "",
"typeValidation": "strict",
"version": 2
},
"conditions": [
{
"id": "b7b64446-f1ea-4622-990c-22f3999a8269",
"leftValue": "={{ $json.messages[0].audio }}",
"rightValue": "",
"operator": {
"type": "object",
"operation": "exists",
"singleValue": true
}
}
],
"combinator": "and"
},
"renameOutput": true,
"outputKey": "Voice"
},
{
"conditions": {
"options": {
"caseSensitive": true,
"leftValue": "",
"typeValidation": "strict",
"version": 2
},
"conditions": [
{
"id": "202af928-a324-411a-bf15-68a349e7bf9e",
"leftValue": "={{ $json.messages[0].image }}",
"rightValue": "",
"operator": {
"type": "object",
"operation": "exists",
"singleValue": true
}
}
],
"combinator": "and"
},
"renameOutput": true,
"outputKey": "Image"
},
{
"conditions": {
"options": {
"caseSensitive": true,
"leftValue": "",
"typeValidation": "strict",
"version": 2
},
"conditions": [
{
"leftValue": "={{ $json.messages[0].text.body }}",
"rightValue": "",
"operator": {
"type": "string",
"operation": "exists",
"singleValue": true
},
"id": "08fd0c80-307e-4f45-b1de-35192ee4ec5e"
}
],
"combinator": "and"
},
"renameOutput": true,
"outputKey": "Text"
}
]
},
"options": {}
},
"type": "n8n-nodes-base.switch",
"typeVersion": 3.2,
"position": [
-1000,
-120
],
"id": "e8d8814b-15ea-4398-bae3-ac70311516d5",
"name": "Check Input Type"
},
{
"parameters": {
"resource": "media",
"operation": "mediaUrlGet",
"mediaGetId": "={{ $('WhatsApp Trigger').item.json.messages[0].image.id }}"
},
"type": "n8n-nodes-base.whatsApp",
"typeVersion": 1,
"position": [
-660,
-280
],
"id": "90d9789a-6b22-4580-9571-4469308a2615",
"name": "Get Image URL",
"webhookId": "280bd5de-32d7-4d8f-93d2-e91e3b0bc161",
"credentials": {
"whatsAppApi": {
"id": "EOK2J4IdnuRxPpKl",
"name": "WhatsApp account"
}
}
},
{
"parameters": {
"url": "={{ $json.url }}",
"authentication": "genericCredentialType",
"genericAuthType": "httpHeaderAuth",
"options": {}
},
"type": "n8n-nodes-base.httpRequest",
"typeVersion": 4.2,
"position": [
-500,
-280
],
"id": "849203b5-62ba-4e09-9cde-9ca35d31bea4",
"name": "Download Image",
"credentials": {
"httpHeaderAuth": {
"id": "xhgufBzCq2QJM35A",
"name": "Whatsapp"
}
}
},
{
"parameters": {
"resource": "image",
"operation": "analyze",
"modelId": {
"__rl": true,
"value": "chatgpt-4o-latest",
"mode": "list",
"cachedResultName": "CHATGPT-4O-LATEST"
},
"text": "=Describe the image in detail.",
"inputType": "base64",
"options": {}
},
"type": "@n8n/n8n-nodes-langchain.openAi",
"typeVersion": 1.8,
"position": [
-340,
-280
],
"id": "1d4d684c-0c4d-48b5-bee7-f1079f933bca",
"name": "Analyze Image",
"credentials": {
"openAiApi": {
"id": "NIAubkxDCLCvpVb7",
"name": "OpenAi account"
}
}
},
{
"parameters": {
"assignments": {
"assignments": [
{
"id": "67552183-de2e-494a-878e-c2948e8cb6bb",
"name": "text",
"value": "=# The user provided the following image and text.\n\n## IMAGE CONTENT:\n{{ $json.content }}\n\n## USER MESSAGE:\n{{ $('WhatsApp Trigger').item.json.messages[0].image.caption || \"Describe the image\" }}",
"type": "string"
}
]
},
"options": {}
},
"type": "n8n-nodes-base.set",
"typeVersion": 3.4,
"position": [
-180,
-280
],
"id": "412a08d7-6629-4d76-bfe6-839f420525f7",
"name": "Image + Text Prompt"
},
{
"parameters": {
"assignments": {
"assignments": [
{
"id": "c05a7fbf-309a-407e-9fee-7e0b03f4a5c8",
"name": "text",
"value": "={{ $('WhatsApp Trigger').item.json.messages[0].text.body }}",
"type": "string"
}
]
},
"options": {}
},
"type": "n8n-nodes-base.set",
"typeVersion": 3.4,
"position": [
-740,
0
],
"id": "5ccb6ca6-5001-4fd6-b878-699db60a7f34",
"name": "Text Only Prompt"
},
{
"parameters": {
"resource": "media",
"operation": "mediaUrlGet",
"mediaGetId": "={{ $('WhatsApp Trigger').item.json.messages[0].audio.id }}"
},
"type": "n8n-nodes-base.whatsApp",
"typeVersion": 1,
"position": [
-660,
-540
],
"id": "73c653d2-9f74-4877-9f30-62bbbef8ef7e",
"name": "Get Audio URL",
"webhookId": "87caa300-7204-47b5-959a-94f4a8fbf8cf",
"credentials": {
"whatsAppApi": {
"id": "EOK2J4IdnuRxPpKl",
"name": "WhatsApp account"
}
}
},
{
"parameters": {
"url": "={{ $json.url }}",
"authentication": "genericCredentialType",
"genericAuthType": "httpHeaderAuth",
"options": {}
},
"type": "n8n-nodes-base.httpRequest",
"typeVersion": 4.2,
"position": [
-500,
-540
],
"id": "84844455-d812-4255-a945-8d58c718141a",
"name": "Download Audio",
"credentials": {
"httpHeaderAuth": {
"id": "xhgufBzCq2QJM35A",
"name": "Whatsapp"
}
}
},
{
"parameters": {
"resource": "audio",
"operation": "transcribe",
"options": {}
},
"type": "@n8n/n8n-nodes-langchain.openAi",
"typeVersion": 1.8,
"position": [
-340,
-540
],
"id": "e6ebad0c-18db-42d3-a228-45c90b6d5f22",
"name": "Transcribe Audio",
"credentials": {
"openAiApi": {
"id": "NIAubkxDCLCvpVb7",
"name": "OpenAi account"
}
}
},
{
"parameters": {
"assignments": {
"assignments": [
{
"id": "219577d5-b028-48fc-90be-980f4171ab68",
"name": "text",
"value": "={{ $json.text }}",
"type": "string"
}
]
},
"options": {}
},
"type": "n8n-nodes-base.set",
"typeVersion": 3.4,
"position": [
-180,
-540
],
"id": "e6bf0705-3b67-46f2-9d5c-dd21d1655df8",
"name": "Audio Prompt"
},
{
"parameters": {
"conditions": {
"options": {
"caseSensitive": true,
"leftValue": "",
"typeValidation": "strict",
"version": 2
},
"conditions": [
{
"id": "b9d1d759-f585-4791-a743-b9d72951e77c",
"leftValue": "={{ $('WhatsApp Trigger').item.json.messages[0].audio }}",
"rightValue": "",
"operator": {
"type": "object",
"operation": "exists",
"singleValue": true
}
}
],
"combinator": "and"
},
"options": {}
},
"type": "n8n-nodes-base.if",
"typeVersion": 2.2,
"position": [
-180,
0
],
"id": "b4abd160-64c5-40e6-94ff-73147962d6b2",
"name": "If"
},
{
"parameters": {
"resource": "audio",
"input": "={{ $('AI Agent').item.json.output }}",
"options": {}
},
"type": "@n8n/n8n-nodes-langchain.openAi",
"typeVersion": 1.8,
"position": [
160,
-20
],
"id": "99654efa-c43b-489a-8ea4-ee77a5337fe2",
"name": "Generate Audio",
"credentials": {
"openAiApi": {
"id": "NIAubkxDCLCvpVb7",
"name": "OpenAi account"
}
}
},
{
"parameters": {
"operation": "send",
"phoneNumberId": "175940715605000",
"recipientPhoneNumber": "={{ $('Check Input Type').item.json.contacts[0].wa_id }}",
"messageType": "audio",
"mediaPath": "useMedian8n",
"additionalFields": {}
},
"type": "n8n-nodes-base.whatsApp",
"typeVersion": 1,
"position": [
520,
-20
],
"id": "a13c1450-5909-422c-90cf-58857dc1e72f",
"name": "Respond with Audio",
"webhookId": "d18b2c98-84e4-43cf-a532-0c47d5161684",
"credentials": {
"whatsAppApi": {
"id": "EOK2J4IdnuRxPpKl",
"name": "WhatsApp account"
}
}
},
{
"parameters": {
"operation": "send",
"phoneNumberId": "175940715605000",
"recipientPhoneNumber": "={{ $('WhatsApp Trigger').item.json.messages[0].from }}",
"textBody": "={{ $json.output }}",
"additionalFields": {}
},
"type": "n8n-nodes-base.whatsApp",
"typeVersion": 1,
"position": [
160,
180
],
"id": "c3ece227-dcd9-420b-a3e8-8c59ab16b2b3",
"name": "Respond with Text",
"webhookId": "23834751-5066-48ba-8e19-549680df2b27",
"credentials": {
"whatsAppApi": {
"id": "EOK2J4IdnuRxPpKl",
"name": "WhatsApp account"
}
}
},
{
"parameters": {
"jsCode": "// Loop over input items and change the MIME type of binary data\nfor (const item of $input.all()) {\n // Check if the item has binary data\n if (item.binary) {\n // Find the binary property name (assuming there's at least one)\n const binaryPropertyNames = Object.keys(item.binary);\n \n for (const propName of binaryPropertyNames) {\n // If the MIME type is 'audio/mp3', change it to 'audio/mpeg'\n if (item.binary[propName].mimeType === 'audio/mp3') {\n item.binary[propName].mimeType = 'audio/mpeg';\n }\n }\n }\n}\n\nreturn $input.all();"
},
"type": "n8n-nodes-base.code",
"typeVersion": 2,
"position": [
340,
-20
],
"id": "bbe3ba8d-eaca-4ab5-986d-06ad6f88c270",
"name": "Fix Mime Type"
},
{
"parameters": {
"content": "## Audio",
"height": 240,
"width": 800
},
"type": "n8n-nodes-base.stickyNote",
"typeVersion": 1,
"position": [
-780,
-620
],
"id": "1b1fec09-7118-46a1-93e8-c8b88713d56c",
"name": "Sticky Note"
},
{
"parameters": {
"content": "## Image & Text",
"height": 280,
"width": 800,
"color": 4
},
"type": "n8n-nodes-base.stickyNote",
"typeVersion": 1,
"position": [
-780,
-360
],
"id": "179ab7eb-131e-44e0-978b-47d781c1e3ff",
"name": "Sticky Note1"
},
{
"parameters": {
"content": "## Tools",
"height": 400,
"width": 820
},
"type": "n8n-nodes-base.stickyNote",
"typeVersion": 1,
"position": [
-800,
420
],
"id": "5cf0939c-77fa-48fa-bd12-e3b68cdb06ad",
"name": "Sticky Note2"
},
{
"parameters": {
"options": {}
},
"type": "@n8n/n8n-nodes-langchain.toolSerpApi",
"typeVersion": 1,
"position": [
-760,
520
],
"id": "1d6e2fbe-425d-46db-9664-cb64b16eda10",
"name": "Web Search",
"credentials": {
"serpApi": {
"id": "PbhBiZydA5misPeU",
"name": "SerpAPI account 3"
}
}
},
{
"parameters": {
"model": {
"__rl": true,
"mode": "list",
"value": "gpt-4o-mini"
},
"options": {}
},
"type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
"typeVersion": 1.2,
"position": [
-740,
200
],
"id": "d015e6e7-ed01-45c0-a019-7617db08bbdb",
"name": "OpenAI Chat Model",
"credentials": {
"openAiApi": {
"id": "NIAubkxDCLCvpVb7",
"name": "OpenAi account"
}
}
},
{
"parameters": {},
"type": "@n8n/n8n-nodes-langchain.toolCalculator",
"typeVersion": 1,
"position": [
-660,
520
],
"id": "1ede568b-596d-4124-a4c5-685f3e0189e4",
"name": "Calculator"
},
{
"parameters": {
"options": {}
},
"type": "@n8n/n8n-nodes-langchain.embeddingsOpenAi",
"typeVersion": 1.2,
"position": [
-540,
660
],
"id": "5897d5d2-d067-40e9-9542-5040477383bd",
"name": "Embeddings OpenAI",
"credentials": {
"openAiApi": {
"id": "NIAubkxDCLCvpVb7",
"name": "OpenAi account"
}
}
},
{
"parameters": {
"mode": "retrieve-as-tool",
"toolName": "contacts",
"toolDescription": "Returns contact information like email addresses.",
"pineconeIndex": {
"__rl": true,
"value": "n8n",
"mode": "list",
"cachedResultName": "n8n"
},
"options": {
"pineconeNamespace": "contacts"
}
},
"type": "@n8n/n8n-nodes-langchain.vectorStorePinecone",
"typeVersion": 1.1,
"position": [
-560,
520
],
"id": "cbfb21a7-3bac-45f1-8620-4c22c11a6683",
"name": "Knowledge Base",
"credentials": {
"pineconeApi": {
"id": "r1kObham6zVWrUX9",
"name": "PineconeApi account 2"
}
}
},
{
"parameters": {
"sseEndpoint": "https://n8n.leonvanzyl.com/mcp/mytools/sse"
},
"type": "@n8n/n8n-nodes-langchain.mcpClientTool",
"typeVersion": 1,
"position": [
-260,
520
],
"id": "0c88184b-895a-430b-bc7c-8ab1fac8d431",
"name": "MCP Client"
}
],
"pinData": {},
"connections": {
"WhatsApp Trigger": {
"main": [
[
{
"node": "Check Input Type",
"type": "main",
"index": 0
}
]
]
},
"Simple Memory": {
"ai_memory": [
[
{
"node": "AI Agent",
"type": "ai_memory",
"index": 0
}
]
]
},
"AI Agent": {
"main": [
[
{
"node": "If",
"type": "main",
"index": 0
}
]
]
},
"Check Input Type": {
"main": [
[
{
"node": "Get Audio URL",
"type": "main",
"index": 0
}
],
[
{
"node": "Get Image URL",
"type": "main",
"index": 0
}
],
[
{
"node": "Text Only Prompt",
"type": "main",
"index": 0
}
]
]
},
"Get Image URL": {
"main": [
[
{
"node": "Download Image",
"type": "main",
"index": 0
}
]
]
},
"Download Image": {
"main": [
[
{
"node": "Analyze Image",
"type": "main",
"index": 0
}
]
]
},
"Analyze Image": {
"main": [
[
{
"node": "Image + Text Prompt",
"type": "main",
"index": 0
}
]
]
},
"Text Only Prompt": {
"main": [
[
{
"node": "AI Agent",
"type": "main",
"index": 0
}
]
]
},
"Image + Text Prompt": {
"main": [
[
{
"node": "AI Agent",
"type": "main",
"index": 0
}
]
]
},
"Get Audio URL": {
"main": [
[
{
"node": "Download Audio",
"type": "main",
"index": 0
}
]
]
},
"Download Audio": {
"main": [
[
{
"node": "Transcribe Audio",
"type": "main",
"index": 0
}
]
]
},
"Transcribe Audio": {
"main": [
[
{
"node": "Audio Prompt",
"type": "main",
"index": 0
}
]
]
},
"Audio Prompt": {
"main": [
[
{
"node": "AI Agent",
"type": "main",
"index": 0
}
]
]
},
"If": {
"main": [
[
{
"node": "Generate Audio",
"type": "main",
"index": 0
}
],
[
{
"node": "Respond with Text",
"type": "main",
"index": 0
}
]
]
},
"Generate Audio": {
"main": [
[
{
"node": "Fix Mime Type",
"type": "main",
"index": 0
}
]
]
},
"Fix Mime Type": {
"main": [
[
{
"node": "Respond with Audio",
"type": "main",
"index": 0
}
]
]
},
"Web Search": {
"ai_tool": [
[
{
"node": "AI Agent",
"type": "ai_tool",
"index": 0
}
]
]
},
"OpenAI Chat Model": {
"ai_languageModel": [
[
{
"node": "AI Agent",
"type": "ai_languageModel",
"index": 0
}
]
]
},
"Calculator": {
"ai_tool": [
[
{
"node": "AI Agent",
"type": "ai_tool",
"index": 0
}
]
]
},
"Embeddings OpenAI": {
"ai_embedding": [
[
{
"node": "Knowledge Base",
"type": "ai_embedding",
"index": 0
}
]
]
},
"Knowledge Base": {
"ai_tool": [
[
{
"node": "AI Agent",
"type": "ai_tool",
"index": 0
}
]
]
},
"MCP Client": {
"ai_tool": [
[
{
"node": "AI Agent",
"type": "ai_tool",
"index": 0
}
]
]
}
},
"active": false,
"settings": {
"executionOrder": "v1"
},
"versionId": "bd8880bb-4da6-4ed5-930e-b6e0e4230b99",
"meta": {
"templateCredsSetupCompleted": true,
"instanceId": "c9b5ffc3334450cdf2932407bee77ee116a63706cde141a825b6dab1a723287d"
},
"id": "ufiOA0ay9Ps6sRDb",
"tags": []
}