E-Lab Script Generator with Airtable Loop¶
This workflow generates WhatsApp-based mentor preparation scripts for Educate!'s training programs. It accepts lesson details via webhook, uses AI to generate formatted scripts following specific guidelines, validates the output, and logs everything to Airtable with retry logic for failed validations.
Purpose¶
No business context provided yet — add a context.md to enrich this documentation.
How It Works¶
- Input Reception: Webhook receives lesson details including name, objectives, duration, outline, characters, and special requirements
- Input Validation: Validates required fields and ensures proper character selection (Guide, Practitioner, Challenger)
- Guidelines Retrieval: Fetches formatting guidelines from Google Docs
- AI Script Generation: Uses Claude AI to generate a 15-segment WhatsApp script following exact format specifications
- Script Validation: Comprehensive validation checks format compliance, segment count, character usage, and structure
- Airtable Logging: Records generation attempt with all details and validation results
- Success/Retry Logic: If validation passes, returns final script; if fails and under 3 attempts, fetches previous attempt and tries rewrite
- Error Handling: After 3 failed attempts, returns error with last validation issues
Mermaid Diagram¶
graph TD
A[Webhook] --> B[Validate Input]
B --> C[Get Guidelines from Google Docs]
C --> D[Prepare Agent Input]
D --> E[Script Writer Agent]
E --> F[Evaluation]
F --> G[Create/Update Airtable Record]
G --> H{Check If Passed}
H -->|Yes| I[Calculate Final Metrics]
I --> J[Respond Success]
H -->|No| K{Check Max Attempts}
K -->|< 3| L[Fetch From Airtable]
L --> M[Build Rewrite Prompt]
M --> N[Update Attempt Context]
N --> E
K -->|>= 3| O[Respond Max Attempts Error]
P[OpenRouter Model] --> E
Q[Extract from File] --> C
Trigger¶
Webhook: POST endpoint at /elab-generator that accepts lesson generation requests with the following payload structure:
- lessonName: Name of the lesson
- objectives: Learning objectives
- duration: Target duration in minutes
- outline: Content outline
- characters: Array of character names (must include Guide, Practitioner, Challenger)
- special: Special requirements (optional)
- tone: Tone preference (optional, defaults to "conversational")
- sourceFiles: Uploaded files for additional context (optional)
Nodes Used¶
| Node Type | Purpose |
|---|---|
| Webhook | Receives lesson generation requests |
| Code (JavaScript) | Validates input, calculates metrics, builds prompts |
| Google Docs | Retrieves formatting guidelines |
| AI Agent (LangChain) | Generates scripts using Claude AI |
| OpenRouter Chat Model | Provides Claude 3.7 Sonnet AI model |
| Airtable | Logs generation attempts and results |
| Set | Prepares data for agent input |
| If | Conditional logic for validation and retry flow |
| Respond to Webhook | Returns results to client |
| Extract from File | Processes uploaded lesson content |
External Services & Credentials Required¶
OpenRouter¶
- Purpose: AI model access (Claude 3.7 Sonnet)
- Credential: OpenRouter API key
- Setup: Configure "E-LAB" credential in n8n
Google Docs¶
- Purpose: Retrieve script formatting guidelines
- Credential: Google OAuth2 for Docs API
- Setup: Configure "Google Docs Waringa" credential
Airtable¶
- Purpose: Log generation attempts and results
- Credential: Airtable API token
- Setup: Configure "EXP Training Bot" credential
- Base: E-Lab Script Generator Logs
- Table: Script Generation Logs
Environment Variables¶
No specific environment variables are required. All configuration is handled through n8n credentials.
Data Flow¶
Input¶
1 2 3 4 5 6 7 8 9 10 | |
Output (Success)¶
1 2 3 4 5 6 7 8 9 10 11 | |
Output (Error)¶
1 2 3 4 5 | |
Error Handling¶
The workflow includes comprehensive error handling:
- Input Validation: Checks for required fields and proper character selection
- Retry Logic: Up to 3 attempts with AI-guided rewrites based on specific validation errors
- Validation System: 9 different validation checks including:
- Segment count (14-16 segments)
- Character presence in dialogues
- Proper INTRO AUDIO/NO INTRO AUDIO flags
- Correct response patterns (CR:)
- Word count targets (2,500-4,000 words)
- Graceful Degradation: Returns detailed error information after max attempts
Known Limitations¶
- Maximum 3 generation attempts per request
- Requires specific character selection (Guide, Practitioner, Challenger)
- Dependent on external AI service availability
- File processing limited to basic text extraction
Related Workflows¶
No related workflows mentioned in the provided context.
Setup Instructions¶
-
Import Workflow: Import the JSON workflow into your n8n instance
-
Configure Credentials:
- Set up OpenRouter API credential named "E-LAB"
- Configure Google Docs OAuth2 credential named "Google Docs Waringa"
- Set up Airtable API credential named "EXP Training Bot"
-
Airtable Setup:
- Create base "E-Lab Script Generator Logs"
- Create table "Script Generation Logs" with required fields
- Update base and table IDs in Airtable nodes
-
Google Docs Setup:
- Ensure guidelines document is accessible
- Update document URL in Google Docs nodes
-
Webhook Configuration:
- Note the webhook URL for client integration
- Configure CORS headers if needed
-
Test the Workflow:
- Send a test request to the webhook endpoint
- Verify Airtable logging works correctly
- Check AI model responses and validation
-
Production Deployment:
- Activate the workflow
- Monitor Airtable logs for performance
- Set up alerts for failed generations