Skip to content

KDP Entry Original

A multi-channel messaging system that processes incoming messages from both SMS (Africa's Talking) and WhatsApp (Twilio), validates users against a database, and routes messages to an AI sales tracking agent for intelligent responses.

Purpose

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

This workflow appears to be designed for youth entrepreneurs to track their daily business metrics through conversational messaging. The system accepts queries via SMS or WhatsApp, validates the sender against a database of registered users, and provides intelligent responses through an AI agent focused on sales tracking.

How It Works

  1. Message Reception: The workflow receives incoming messages from two sources - SMS messages via Africa's Talking webhook and WhatsApp messages via Twilio webhook
  2. Message Routing: A switch node determines the message source and standardizes the data format
  3. User Validation: The system queries a PostgreSQL database to verify if the sender's phone number exists in the youth entrepreneurs table
  4. Response Routing: If the user is not found, an error message is sent back; if found, the message is processed by an AI sales tracking agent
  5. Response Delivery: The AI agent's response is sent back through the appropriate channel (SMS or WhatsApp)
  6. Scheduled Nudges: A disabled 7:00 PM trigger can send daily prompts to users asking for their business metrics

Mermaid Diagram

graph TD
    A[Africa's Talking Webhook] --> D[Switch]
    B[Twilio WhatsApp Webhook] --> D
    D -->|SMS| E[setSMSfields]
    D -->|WhatsApp| F[setSMSfields1]
    E --> G[Execute SQL Query]
    F --> G
    G --> H[ifUserNotFound]
    H -->|Not Found| I[userNotFound WhatsApp Response]
    H -->|Found| J[setUserPhoneNumberAndQuery]
    J --> K[Call SalesTrackingAgent]
    K --> L[Send WhatsApp Response via Twilio]

    M[7:00 PM Trigger] --> N[Get Youth Number]
    N --> O[Send 7:00pm WhatsApp Nudge]
    N --> P[Send 7:00 PM SMS Nudge]

Trigger

  • Webhook Triggers: Two webhook endpoints receive incoming messages
    • Africa's Talking SMS webhook (currently disabled)
    • Twilio WhatsApp webhook (currently disabled)
  • Schedule Trigger: Daily at 7:00 PM for sending nudge messages (currently disabled)

Nodes Used

Node Type Purpose
Webhook Receives incoming SMS and WhatsApp messages
Switch Routes messages based on source (SMS vs WhatsApp)
Set Standardizes message data format
PostgreSQL Validates user phone numbers against database
If Conditional logic for user validation and message routing
Execute Workflow Calls the SalesTrackingAgent workflow
Twilio Sends WhatsApp responses
HTTP Request Sends SMS responses via Africa's Talking API
Google Sheets Retrieves user data for scheduled nudges
Schedule Trigger Triggers daily nudge messages
Sticky Note Documentation and comments

External Services & Credentials Required

Africa's Talking

  • API Key: Required for sending SMS messages
  • Username: toll_free_sms_ke
  • Short Code: 24436

Twilio

  • Credential: twilioApi (ID: KIdaZWKXNQtH7wvH)
  • WhatsApp Number: +254203892316

PostgreSQL Database

  • Credential: postgres (ID: Hw9XWerQ5RNHaVTk)
  • Database: Contains youthEntrepreneurs table with phone numbers

Google Sheets

  • Document: "Prototype Itinerary" spreadsheet
  • Sheet: "Nudge test numbers" for scheduled messaging

Environment Variables

No explicit environment variables are defined in this workflow. Configuration is handled through: - Hard-coded API keys (should be moved to credentials) - Database connection strings in credentials - Webhook URLs generated by n8n

Data Flow

Input

  • SMS Messages: Phone number, message text, metadata from Africa's Talking
  • WhatsApp Messages: WhatsApp ID, message body, profile information from Twilio

Processing

  • Standardizes message format regardless of source
  • Validates sender against PostgreSQL database
  • Passes user query to SalesTrackingAgent workflow

Output

  • Success: AI-generated response sent via original channel
  • User Not Found: Error message sent back to sender
  • Scheduled Nudges: Daily prompts for business metrics

Error Handling

  • User Validation: If phone number not found in database, sends "Sorry, your number was not found in our database" message
  • Retry Logic: The SalesTrackingAgent call has retry enabled
  • Channel-Specific Responses: Error messages are sent through the same channel as the incoming message

Known Limitations

Based on the sticky notes in the workflow: - API keys are hard-coded instead of using proper credential management - Uncertainty about A/B testing approach for SMS vs WhatsApp nudges - Data flow issues when connecting multiple webhooks to edit fields nodes - Missing source tagging to provide context to the AI agent about message format requirements

  • Mark - SalesTrackingAgent (ID: SQuf6XRV3xERKdsY): AI agent that processes user queries and generates responses

Setup Instructions

  1. Import Workflow: Import the JSON into your n8n instance
  2. Configure Credentials:
    • Set up Africa's Talking API credentials
    • Configure Twilio WhatsApp credentials
    • Set up PostgreSQL database connection
    • Configure Google Sheets access
  3. Database Setup: Ensure PostgreSQL database has youthEntrepreneurs table with phoneNumber field
  4. Webhook Configuration:
    • Enable the webhook nodes
    • Configure Africa's Talking to send SMS webhooks to the provided endpoint
    • Configure Twilio to send WhatsApp webhooks to the provided endpoint
  5. Dependencies: Ensure the "Mark - SalesTrackingAgent" workflow is imported and configured
  6. Security: Move hard-coded API keys to proper credential storage
  7. Testing: Use the pinned data to test message processing flows
  8. Enable Triggers: Enable webhook and schedule triggers when ready for production