Skip to content

AITrainerDraft

An interactive WhatsApp-based AI coaching system that provides personalized educational content through conversational AI, supporting both text and voice interactions with intelligent mentor ID detection and context-aware responses.

Purpose

No business context provided yet — add a context.md to enrich this documentation.

How It Works

  1. Message Reception: The workflow receives WhatsApp messages via Twilio webhook, capturing text messages, voice notes, and media content
  2. Message Processing: Extracts and analyzes incoming messages to detect mentor IDs (6-digit codes), identify voice notes, and maintain session context
  3. AI Conversation: Routes processed messages to an AI agent configured as an interactive course instructor that provides educational content and responds to student questions
  4. Knowledge Retrieval: The AI agent can access a Supabase vector database to retrieve relevant course materials and context
  5. Response Generation: Creates conversational, supportive responses tailored to the educational context
  6. Audio Conversion: Converts AI text responses to natural-sounding audio using OpenAI's text-to-speech
  7. Message Delivery: Sends the audio response back to the student via WhatsApp through Twilio

Workflow Diagram

graph TD
    A[WhatsApp Webhook] --> B[Extract Message Info]
    B --> C[AI Agent]
    C --> D[Generate Audio]
    D --> E[Send WhatsApp Message]

    F[Supabase Vector Store] --> C
    G[Embeddings OpenAI] --> F
    H[OpenAI Chat Model] --> C
    I[Simple Memory] --> C

Trigger

Webhook Trigger: POST endpoint at /coaching-start that receives WhatsApp message data from Twilio webhooks.

Nodes Used

Node Type Purpose
Webhook Receives incoming WhatsApp messages from Twilio
Code (JavaScript) Processes message data, detects mentor IDs, and manages session context
AI Agent Provides interactive educational responses as a course instructor
Supabase Vector Store Retrieves relevant course content and context
OpenAI Embeddings Converts text to vector embeddings for knowledge retrieval
OpenAI Chat Model Powers the conversational AI responses
Buffer Window Memory Maintains conversation context across messages
OpenAI Audio Converts text responses to speech audio
Twilio Sends audio messages back via WhatsApp

External Services & Credentials Required

  • Twilio: WhatsApp Business API integration
    • Account SID and Auth Token
    • WhatsApp-enabled phone number
  • OpenAI:
    • API key for GPT-4.1 chat model
    • API access for text-to-speech (TTS-1-HD)
    • API access for embeddings
  • Supabase:
    • Project URL and API key
    • Vector database with course content

Environment Variables

No specific environment variables are configured in this workflow. All external service credentials are managed through n8n's credential system.

Data Flow

Input: - WhatsApp message data from Twilio webhook - Message body (text or empty for voice notes) - Media URLs for voice notes - Sender phone number and profile information

Processing: - Session management with thread keys - Mentor ID detection and validation - Conversation context maintenance - Knowledge retrieval from vector database

Output: - Audio response files sent via WhatsApp - Updated conversation memory - Session state persistence

Error Handling

The JavaScript code node includes try-catch error handling that returns structured error information when message processing fails, ensuring the workflow can continue even with malformed input data.

Known Limitations

  • Workflow is currently in draft status (not active)
  • Limited to WhatsApp as the communication channel
  • Requires 6-digit mentor ID format for proper identification
  • Audio responses only (no text fallback shown)

No related workflows identified in the current context.

Setup Instructions

  1. Import Workflow: Import the JSON configuration into your n8n instance

  2. Configure Credentials:

    • Set up Twilio API credentials with WhatsApp access
    • Configure OpenAI API credentials with access to GPT-4.1, embeddings, and TTS
    • Set up Supabase credentials with vector database access
  3. Webhook Configuration:

    • Note the webhook URL generated for the "WhatsApp Webhook3" node
    • Configure this URL in your Twilio WhatsApp webhook settings
  4. Vector Database Setup:

    • Ensure your Supabase vector store contains relevant course content
    • Verify the embeddings model matches between storage and retrieval
  5. Test the Flow:

    • Send a test WhatsApp message to your Twilio number
    • Verify the AI responds with audio messages
    • Check that mentor ID detection works with 6-digit codes
  6. Activate Workflow: Change the workflow status from draft to active to begin processing real messages

  7. Monitor Performance: Watch for successful message processing and audio generation in the execution logs