{"meta":{"instanceId":"8f351f8f18f69574a46094a253f36ef0804528a7fc6d1eee81ee60e356860359","templateCredsSetupCompleted":true},"nodes":[{"id":"5e4c80ac-d9e2-4e0c-93ea-0ece73aec329","name":"Send an SMS/MMS/WhatsApp message","type":"n8n-nodes-base.twilio","position":[1200,-432],"parameters":{"to":"={{ $('Twilio (whatsapp) Trigger').item.json.data.from.replaceAll('whatsapp:', '') }}","from":"={{ $('Twilio (whatsapp) Trigger').item.json.data.to.replaceAll('whatsapp:', '') }}","message":"={{ $json.output }}","options":{},"toWhatsapp":true},"credentials":{"twilioApi":{"id":"dfkafMHOFDwjShIU","name":"Twilio account"}},"typeVersion":1},{"id":"fa661ad7-76c1-4108-8893-242b5ef38650","name":"Google Drive Trigger","type":"n8n-nodes-base.googleDriveTrigger","position":[-784,368],"parameters":{"event":"fileCreated","options":{},"pollTimes":{"item":[{"mode":"everyMinute"}]},"triggerOn":"specificFolder","folderToWatch":{"__rl":true,"mode":"list","value":"1-IwC1Zsn5MB0-EjsRYkZ5EpfvWrOtpV-","cachedResultUrl":"https://drive.google.com/drive/folders/1-IwC1Zsn5MB0-EjsRYkZ5EpfvWrOtpV-","cachedResultName":"CustomCX knowledgebase folder"}},"credentials":{"googleDriveOAuth2Api":{"id":"eDpP3P4PjLffHA9W","name":"Google Drive account 2"}},"typeVersion":1},{"id":"f9bc2868-08ad-4b30-8556-2d1fad6d51d9","name":"Google Drive Trigger1","type":"n8n-nodes-base.googleDriveTrigger","position":[-800,560],"parameters":{"event":"fileUpdated","options":{},"pollTimes":{"item":[{"mode":"everyMinute"}]},"triggerOn":"specificFolder","folderToWatch":{"__rl":true,"mode":"list","value":"1-IwC1Zsn5MB0-EjsRYkZ5EpfvWrOtpV-","cachedResultUrl":"https://drive.google.com/drive/folders/1-IwC1Zsn5MB0-EjsRYkZ5EpfvWrOtpV-","cachedResultName":"CustomCX knowledgebase folder"}},"credentials":{"googleDriveOAuth2Api":{"id":"eDpP3P4PjLffHA9W","name":"Google Drive account 2"}},"typeVersion":1},{"id":"502829da-1fdd-4c8d-8830-9d93e2a7b9a5","name":"Download file","type":"n8n-nodes-base.googleDrive","position":[448,336],"parameters":{"fileId":{"__rl":true,"mode":"id","value":"={{ $('Set the fields for mapping').item.json.file_id }}"},"options":{},"operation":"download"},"credentials":{"googleDriveOAuth2Api":{"id":"eDpP3P4PjLffHA9W","name":"Google Drive account 2"}},"executeOnce":true,"typeVersion":3},{"id":"e71617ba-dfba-4999-8924-330a6b1d12a4","name":"Supabase Vector Store","type":"@n8n/n8n-nodes-langchain.vectorStoreSupabase","position":[688,304],"parameters":{"mode":"insert","options":{"queryName":"match_documents"},"tableName":{"__rl":true,"mode":"list","value":"documents","cachedResultName":"documents"}},"credentials":{"supabaseApi":{"id":"CVoH7Yln6wHo5Xdy","name":"Meshackago a/c"}},"typeVersion":1.3},{"id":"fa3c2242-be9b-4d16-b6fa-2eb8ee66b46d","name":"Default Data Loader","type":"@n8n/n8n-nodes-langchain.documentDefaultDataLoader","position":[624,480],"parameters":{"loader":"docxLoader","options":{"metadata":{"metadataValues":[{"name":"file_id","value":"={{ $('Set the fields for mapping').item.json.file_id }}"}]}},"dataType":"binary","textSplittingMode":"custom"},"typeVersion":1.1},{"id":"09361643-9d63-4bda-986b-43e00a5d5a3a","name":"Recursive Character Text Splitter","type":"@n8n/n8n-nodes-langchain.textSplitterRecursiveCharacterTextSplitter","position":[656,656],"parameters":{"options":{}},"typeVersion":1},{"id":"59e42362-a9a8-40b3-9e80-7363585dbbf8","name":"Sticky Note","type":"n8n-nodes-base.stickyNote","position":[160,208],"parameters":{"color":7,"width":864,"height":592,"content":"## RAG pipeline\nSet the embedding Dimension to 1536"},"typeVersion":1},{"id":"a887d7fc-c8b7-4ffd-8947-8ee22c5e451b","name":"Knowledgebase","type":"@n8n/n8n-nodes-langchain.vectorStoreSupabase","position":[480,-160],"parameters":{"mode":"retrieve-as-tool","options":{"queryName":"match_documents"},"tableName":{"__rl":true,"mode":"list","value":"documents","cachedResultName":"documents"},"toolDescription":"Use this tool to retrieve the knowledge about customCX"},"credentials":{"supabaseApi":{"id":"CVoH7Yln6wHo5Xdy","name":"Meshackago a/c"}},"typeVersion":1.3},{"id":"2b451ff8-e3ce-4c8d-8834-bce41a047887","name":"Loop Over Items","type":"n8n-nodes-base.splitInBatches","position":[224,336],"parameters":{"options":{}},"typeVersion":3},{"id":"0dd6c592-a304-4c96-8a80-2d63708bfa66","name":"Embeddings OpenAI","type":"@n8n/n8n-nodes-langchain.embeddingsOpenAi","position":[880,480],"parameters":{"options":{"dimensions":1536}},"credentials":{"openAiApi":{"id":"BRBnRcpPneH4paLG","name":"OpenAi account verified"}},"typeVersion":1.2},{"id":"f5288b8d-7462-48c2-ab6d-818c65dfb82f","name":"Think","type":"@n8n/n8n-nodes-langchain.toolThink","position":[992,-176],"parameters":{},"typeVersion":1.1},{"id":"874f68cd-f655-439e-ae76-e6978b71a4e9","name":"Calculator","type":"@n8n/n8n-nodes-langchain.toolCalculator","position":[896,-176],"parameters":{},"typeVersion":1},{"id":"ec7b074f-53ef-4b61-8932-f757ba398126","name":"conversation buffer","type":"@n8n/n8n-nodes-langchain.memoryBufferWindow","position":[352,-192],"parameters":{"sessionKey":"={{ $json.phoneNumber }}","sessionIdType":"customKey","contextWindowLength":20},"typeVersion":1.3},{"id":"b7c9f3d6-8ac2-485b-8a44-a50fd3bbde27","name":"Deletes old records","type":"n8n-nodes-base.supabase","position":[-272,416],"parameters":{"tableId":"documents","operation":"delete","filterType":"string","filterString":"=metadata->>file_id=eq.{{ $json.file_id }}"},"credentials":{"supabaseApi":{"id":"CVoH7Yln6wHo5Xdy","name":"Meshackago a/c"}},"executeOnce":true,"retryOnFail":false,"typeVersion":1,"alwaysOutputData":true},{"id":"b309126e-d07f-483b-90c3-d87114bc50ba","name":"Set the fields for mapping","type":"n8n-nodes-base.set","position":[-496,416],"parameters":{"options":{},"assignments":{"assignments":[{"id":"32591319-9705-4dea-aacc-c6b8b8fb38e4","name":"file_id","type":"string","value":"={{ $json.id }}"},{"id":"87d30657-a58a-4f3c-b59a-135511261000","name":"file_name","type":"string","value":"={{ $json.originalFilename }}"}]}},"typeVersion":3.4},{"id":"6118157c-87ac-4609-a097-785e1ec2c8e2","name":"human in the loop","type":"n8n-nodes-base.httpRequest","position":[-608,-416],"parameters":{"url":"=https://customcx-whatsap.netlify.app/api/check-human?phone={{$json.data.from.trim()}}","options":{},"jsonHeaders":"{\n  \"Content-Type\": \"application/json\"\n}","sendHeaders":true,"specifyHeaders":"json"},"typeVersion":4.3,"alwaysOutputData":true},{"id":"2fb32db8-6ba7-494e-92de-c07cc6b85593","name":"Sticky Note7","type":"n8n-nodes-base.stickyNote","position":[-1744,-752],"parameters":{"width":608,"height":768,"content":"## How it works\nThis is a unique whatsapp automation, that allows AI and human collaboration. The AI agent respond to enquiries and queries but once a human agent takes over the AI agent steps down. The AI agent can carry on with the session once 2hours window expires without a human response. \n\nIt is important to note that whatsapp only allows businesses to respond to clients who texted/initiated a conversation in the past 24hours window. Also, only verified tamplates are allowed.\n\n## Setup steps\nAfter pasting this workflow in n8n;\nCreate a twilio account, and then buy a twilio number roughly $20\nYou'll need these parameters to establish the connection;\nTWILIO_ACCOUNT_SID=AC2e547b4fcb89af363be29085****\nTWILIO_AUTH_TOKEN=5ff6d9f54793780031e3411378****\nTWILIO_PHONE_NUMBER=+1323577****\n\nTo set up  Human-in-the-loop, use this GitHub code [Full guide](https://github.com/shadrack-ago/whatsapp-dashboard.git), it has a ready dashboard with analytics of AI and Human responses. The responses are clearly labled as AI or Human for easier tracking \n\nSupabase account for knowledgebase, follow this [Tutorial](https://youtu.be/5uw1wE6niGc?si=hLd8m_PdcH4tts5m)\n\nGemini [Free API](https://aistudio.google.com/api-keys)\n\n## Customization\nThis workflow also works fine with whatsapp APIs. However, I used twilio for faster business verification by Meta/facebook Business Suite\n"},"typeVersion":1},{"id":"85a4cb58-6364-4756-bc89-82cb35cfe278","name":"Validate text input","type":"n8n-nodes-base.if","position":[-192,-416],"parameters":{"options":{},"conditions":{"options":{"version":2,"leftValue":"","caseSensitive":true,"typeValidation":"strict"},"combinator":"and","conditions":[{"id":"70562678-2637-416d-bfa3-b5517e9215d2","operator":{"name":"filter.operator.equals","type":"string","operation":"equals"},"leftValue":"","rightValue":""}]}},"typeVersion":2.2},{"id":"70fdb973-206b-4808-a21d-6083ccd034c9","name":"Sticky Note1","type":"n8n-nodes-base.stickyNote","position":[-640,-608],"parameters":{"color":7,"width":304,"height":336,"content":"## Human + AI \nHuman in the loop node checks the past conversation history.\n\nThe Filter node checks if 2hours has elapsed since the last time of human response"},"typeVersion":1},{"id":"9dace532-619e-4f9e-a870-f9cfb57e9414","name":"2hour Filter","type":"n8n-nodes-base.filter","position":[-448,-416],"parameters":{"options":{},"conditions":{"options":{"version":2,"leftValue":"","caseSensitive":true,"typeValidation":"loose"},"combinator":"or","conditions":[{"id":"852e0abf-9764-4812-a99b-67bec3f9190a","operator":{"type":"boolean","operation":"false","singleValue":true},"leftValue":"={{ $json.humanActive }}","rightValue":"false"},{"id":"97d66ffb-38ba-48d7-a5d0-8f3caf991c6d","operator":{"type":"dateTime","operation":"after"},"leftValue":"={{ $json.lastHumanResponseTime }}","rightValue":"={{ $now.minus({ hours: 2 }) }}"}]},"looseTypeValidation":true},"typeVersion":2.2},{"id":"707c81bb-fd3c-4851-b37d-de005f9691eb","name":"Twilio (whatsapp) Trigger","type":"n8n-nodes-base.twilioTrigger","position":[-976,-416],"webhookId":"e59492bf-99aa-45d9-9112-01e3326a247a","parameters":{"updates":["com.twilio.messaging.inbound-message.received"]},"credentials":{"twilioApi":{"id":"dfkafMHOFDwjShIU","name":"Twilio account"}},"typeVersion":1},{"id":"0e1bbc66-a380-4019-80fe-6bfcb8592844","name":"Airtable CRM","type":"n8n-nodes-base.airtableTool","position":[784,-176],"parameters":{"id":"={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('Record_ID', ``, 'string') }}","base":{"__rl":true,"mode":"list","value":"appWhV8pFDfV5WSEr","cachedResultUrl":"https://airtable.com/appWhV8pFDfV5WSEr","cachedResultName":"ShambaTours"},"table":{"__rl":true,"mode":"list","value":"tblIjzRKeAiFcB4Jl","cachedResultUrl":"https://airtable.com/appWhV8pFDfV5WSEr/tblIjzRKeAiFcB4Jl","cachedResultName":"Table 1"},"options":{},"authentication":"airtableOAuth2Api"},"credentials":{"airtableOAuth2Api":{"id":"3XmiynaAhYRAkqoV","name":"Airtable Personal Access Token account"}},"typeVersion":2.1},{"id":"870fd5bc-8594-449d-9f72-0236a97dcbf4","name":"Sticky Note2","type":"n8n-nodes-base.stickyNote","position":[-912,272],"parameters":{"color":7,"width":752,"height":416,"content":"## Knowledgebase source\nGoogle drive folder makes it easier to update or add fresh data from time to time without the hustle of executing the workflow manually everytime you need an update"},"typeVersion":1},{"id":"9fbe5ced-0a8a-4356-a934-207e5766587e","name":"Whatsapp Support Agent","type":"@n8n/n8n-nodes-langchain.agent","position":[480,-432],"parameters":{"text":"={{ $('Twilio (whatsapp) Trigger').item.json.data.body }}","options":{"systemMessage":"You are a CustomCX virtual Assistant. Your role is to help users, answer their concerns and direct them to the right place. you should be able to answer majority of there questions.\nCustomCX help businesses to automate their daily repeated workflows by Leveraging AI.\nWe implement AI solutions for Businesses that help the deliver more while spending less \n\nUse Knowledgebase tool to retrieve answers\n\nThey can also Visit our website CustomCX.com\nor contact human support at +254728411182\nWe located in Nairobi,Kenya (we work remotely but we do physical meeting at Nation Centre 9th Floor)\nBook free discovery call  https://cal.com/shadrack-customcx/30min \n\nTry to be helpful before refering our clients to human support.\n\nYou are allowed to be creative.\nAlways try to be brief and clear\n\nstrictly stick to Customcx solution context. no discussing political issues nor medical nor relationship or personal views\n\n#Rule\nYour output must never exceed 14000characters. stay in whatsapp twilio supported character limit. you may create a summary\n\n#Rule2\nYou are strictly CustomCX virtual assistant, you can Never answer questions that do not relate to CustomCX, stick to CustomCX role of workflow automation and building AI solutions.\n\nUse think tool for steps that demands logical thinking and Calculator for calculation\n"},"promptType":"define"},"typeVersion":2.2},{"id":"27a39faa-3889-46e5-b270-9aa22b337c45","name":"Google Gemini (the brain)","type":"@n8n/n8n-nodes-langchain.lmChatGoogleGemini","position":[160,-192],"parameters":{"options":{}},"credentials":{"googlePalmApi":{"id":"q1mDATlhD26qgYRs","name":"Google Gemini(PaLM) Mikes Api account 2"}},"typeVersion":1},{"id":"55959d51-2029-44f3-800f-3863dcdcfe89","name":"Sticky Note3","type":"n8n-nodes-base.stickyNote","position":[112,-528],"parameters":{"color":7,"width":960,"height":496,"content":"## Whatsapp Agent\nProvide the agent with necessary tools based on your business requirements/needs\n"},"typeVersion":1}],"pinData":{},"connections":{"Think":{"ai_tool":[[{"node":"Whatsapp Support Agent","type":"ai_tool","index":0}]]},"Calculator":{"ai_tool":[[{"node":"Whatsapp Support Agent","type":"ai_tool","index":0}]]},"2hour Filter":{"main":[[{"node":"Validate text input","type":"main","index":0}]]},"Airtable CRM":{"ai_tool":[[{"node":"Whatsapp Support Agent","type":"ai_tool","index":0}]]},"Download file":{"main":[[{"node":"Supabase Vector Store","type":"main","index":0}]]},"Knowledgebase":{"ai_tool":[[{"node":"Whatsapp Support Agent","type":"ai_tool","index":0}]]},"Loop Over Items":{"main":[[{"node":"Download file","type":"main","index":0}],[{"node":"Deletes old records","type":"main","index":0}]]},"Embeddings OpenAI":{"ai_embedding":[[{"node":"Supabase Vector Store","type":"ai_embedding","index":0},{"node":"Knowledgebase","type":"ai_embedding","index":0}]]},"human in the loop":{"main":[[{"node":"2hour Filter","type":"main","index":0}]]},"Default Data Loader":{"ai_document":[[{"node":"Supabase Vector Store","type":"ai_document","index":0}]]},"Deletes old records":{"main":[[{"node":"Loop Over Items","type":"main","index":0}]]},"Validate text input":{"main":[[{"node":"Whatsapp Support Agent","type":"main","index":0}]]},"conversation buffer":{"ai_memory":[[{"node":"Whatsapp Support Agent","type":"ai_memory","index":0}]]},"Google Drive Trigger":{"main":[[{"node":"Set the fields for mapping","type":"main","index":0}]]},"Google Drive Trigger1":{"main":[[{"node":"Set the fields for mapping","type":"main","index":0}]]},"Whatsapp Support Agent":{"main":[[{"node":"Send an SMS/MMS/WhatsApp message","type":"main","index":0}]]},"Google Gemini (the brain)":{"ai_languageModel":[[{"node":"Whatsapp Support Agent","type":"ai_languageModel","index":0}]]},"Twilio (whatsapp) Trigger":{"main":[[{"node":"human in the loop","type":"main","index":0}]]},"Set the fields for mapping":{"main":[[{"node":"Deletes old records","type":"main","index":0}]]},"Recursive Character Text Splitter":{"ai_textSplitter":[[{"node":"Default Data Loader","type":"ai_textSplitter","index":0}]]}}}