V4 - WhatsAppTrigger - Warenga¶
This workflow serves as the main entry point for WhatsApp messages in the Sifa V4 system, handling incoming messages from young entrepreneurs and routing them through various processing stages including sales tracking, message formatting, and response delivery via Twilio.
Purpose¶
No business context provided yet — add a context.md to enrich this documentation.
This workflow appears to be part of a youth entrepreneurship coaching program, processing WhatsApp communications and providing automated responses through AI agents. It handles user onboarding, sales data collection, and provides fallback mechanisms for error scenarios.
How It Works¶
- Message Reception: Receives WhatsApp messages via webhook from Twilio
- User Lookup: Queries the database to find the user's record and current status
- User Routing: Routes new users to an organic handler for onboarding, existing users continue to main flow
- Activity Tracking: Updates the user's last inbound message timestamp
- Message Processing: Extracts user phone number and message content for processing
- AI Agent Processing: Sends the message to a Sales Tracking Agent for analysis and response generation
- Response Validation: Checks if the agent produced valid output
- Message Formatting: Processes the agent's response through a Message Interceptor for proper formatting
- Response Delivery: Determines whether to send a template message or regular text response via Twilio
- Error Handling: Logs errors and sends fallback messages when processing fails
- Chat Logging: Records all interactions for audit and analysis purposes
Workflow Diagram¶
graph TD
A[DevelopmentWebhook] --> B[getUserRecord]
B --> C[ifUserNotFound]
C -->|User Not Found| D[callOrganicHandler]
C -->|User Found| E[updateLastInboundAt]
E --> F[setUserPhoneNumnberAndQuery]
F --> G["Call 'Francis - SalesTrackingAgent'"]
G -->|Success| H[hasAgentOutput]
G -->|Error| I[logError]
H -->|Has Output| J["Call 'V4 - MessageInterceptor'"]
H -->|No Output| I
J --> K[isInteractive?]
K -->|Interactive| L[Variables]
K -->|Regular| M[Send WhatsApp response via Twilio]
L --> N[sendTemplate]
I --> O[setFallbackMessage]
O --> P[sendFallbackWhatsApp]
O --> Q[logChatFallback]
%% Unused daytime message flow
R[isDaytimeMessage] --> S[storeDaytimeMessage]
S --> T[isWeeklyReport]
T --> U[Set WR Input]
U --> V[Call Weekly Report Handler]
Trigger¶
Webhook Trigger: Listens for POST requests at path 98ed6d7d-5af1-4500-878f-56b7f22cad75 from Twilio WhatsApp webhook notifications.
Nodes Used¶
| Node Type | Node Name | Purpose |
|---|---|---|
| Webhook | DevelopmentWebhook | Receives incoming WhatsApp messages from Twilio |
| PostgreSQL | getUserRecord | Retrieves user information from the database |
| If | ifUserNotFound | Routes new vs existing users |
| Execute Workflow | callOrganicHandler | Handles new user onboarding |
| PostgreSQL | updateLastInboundAt | Updates user's last activity timestamp |
| Set | setUserPhoneNumnberAndQuery | Prepares data for agent processing |
| Execute Workflow | Call 'Francis - SalesTrackingAgent' | Processes messages through AI agent |
| If | hasAgentOutput | Validates agent response |
| Execute Workflow | Call 'V4 - MessageInterceptor' | Formats agent responses |
| If | isInteractive? | Determines message type (template vs text) |
| Set | Variables | Prepares template variables |
| HTTP Request | sendTemplate | Sends WhatsApp template messages |
| Twilio | Send WhatsApp response via Twilio | Sends regular text messages |
| PostgreSQL | logError | Records processing errors |
| Set | setFallbackMessage | Creates error fallback message |
| Twilio | sendFallbackWhatsApp | Sends fallback messages |
| PostgreSQL | logChatFallback | Logs fallback interactions |
External Services & Credentials Required¶
Twilio¶
- Credential Name: "Twilio WhatsApp kdp"
- Purpose: Sending WhatsApp messages
- Required Fields: Account SID, Auth Token
Twilio HTTP Basic Auth¶
- Credential Name: "Waringa twilio auth"
- Purpose: Template message API calls
- Required Fields: Username, Password
PostgreSQL¶
- Credential Name: "sifaV4Dev"
- Purpose: Database operations for user records, chat logs, and error tracking
- Required Fields: Host, Database, Username, Password
Environment Variables¶
No explicit environment variables are configured in this workflow. All configuration is handled through n8n credentials and node parameters.
Data Flow¶
Input¶
- WhatsApp webhook payload containing:
WaId: User's WhatsApp IDBody: Message contentTo: Twilio WhatsApp numberFrom: User's WhatsApp numberProfileName: User's display nameInteractiveData: Button/interactive responsesButtonPayload: Button interaction data
Output¶
- WhatsApp messages sent to users via Twilio
- Database records updated in:
v4_youthEntrepreneurs: User activity trackingv4_chatLog: Conversation loggingerrorLog: Error trackingv4_pendingDaytimeMessages: Unused daytime message storage
Error Handling¶
The workflow implements comprehensive error handling:
- Agent Processing Errors: If the Sales Tracking Agent fails, errors are logged to the database and a fallback message is sent
- Database Connection Issues: PostgreSQL nodes are configured to continue on error
- Fallback Messaging: When processing fails, users receive a polite error message in Swahili: "Samahani, kuna tatizo kidogo. Tafadhali jaribu tena baadaye. 🙏"
- Error Logging: All errors are recorded with user phone number, workflow name, error message, and raw input
- Chat Logging: Even fallback interactions are logged for audit purposes
Known Limitations¶
Based on the sticky notes in the workflow: - Daytime message logic is noted as unnecessary for Sifa V4 (active throughout) - Error notifications to Slack channels are planned but not implemented - Call center integration is mentioned as not needed - Weekly report handler is disabled
Related Workflows¶
This workflow calls several other workflows: - V4 - OrganicKeepWarmHandler: Handles new user onboarding - V4 - WIP - Francis - SalesTrackingAgent: Processes messages through AI agent - V4 - MessageInterceptor: Formats agent responses for delivery - V4 - Weekly Report Handler: Disabled weekly report processing
Setup Instructions¶
-
Import the Workflow: Import the JSON into your n8n instance
-
Configure Credentials:
- Set up Twilio credentials with your Account SID and Auth Token
- Configure PostgreSQL connection to your Sifa V4 database
- Set up Twilio HTTP Basic Auth for template messaging
-
Database Setup: Ensure these tables exist:
v4_youthEntrepreneursv4_chatLogerrorLogv4_pendingDaytimeMessages
-
Webhook Configuration:
- Note the webhook URL from the DevelopmentWebhook node
- Configure your Twilio WhatsApp number to send webhooks to this URL
-
Related Workflows: Import and configure the dependent workflows mentioned in the Related Workflows section
-
Testing: Send a test WhatsApp message to verify the complete flow works
-
Monitoring: Monitor the error logs and chat logs to ensure proper operation