This project sets up a WhatsApp AI Agent (Customer Support Assistant) using n8n, OpenAI, PostgreSQL, and Twilio. The assistant automatically handles customer queries, provides responses based on logic and memory, and can escalate issues when needed.
- n8n: Workflow automation tool
- Twilio: WhatsApp messaging API
- OpenAI: Natural language processing for intelligent responses
- Receives and reads incoming WhatsApp messages via Twilio
- Processes messages using OpenAI to generate context-aware replies
- Automatically removes irrelevant annotations (e.g., 【text】) using a custom JavaScript function
- Responds back via WhatsApp through Twilio
- Memory expressions for user identification and contextual conversations
- Logs message content with timestamps for tracking
This function removes surrounding annotation blocks often found in AI-generated or formatted messages:
function removeAnnotations(input) {
return input.replace(/【[^】]*】/g, '').trim();
}
// Example usage
const output = removeAnnotations(query);
return output;To:
From:
Use these expressions in the Twilio - Send an SMS node to direct replies to the right user.
-
Clone this repository
-
Configure Environment Variables (Twilio SID, Auth Token, OpenAI API Key, etc.)
-
Create a Workflow in n8n
- Set up a Twilio Trigger node to listen to incoming WhatsApp messages.
- Add a Code node with the annotation removal logic.
- Use the OpenAI node to generate a reply.
- Send the response using Twilio - Send an SMS node.
-
Deploy the Workflow
- You can use n8n Cloud or self-host using Docker.
- Ensure your Twilio account is WhatsApp-enabled.
- Customize your OpenAI prompt to match your support tone (e.g., friendly, concise, professional).
- Memory expressions can be expanded to store chat history for better continuity.
Feel free to open issues or submit PRs to improve functionality, edge case handling, or integrations!
MIT License