Skip to content

Check Nudge Channel and Send Daily Sales Tracking Messages

This workflow sends daily nudge messages to youth entrepreneurs to track their sales performance, intelligently choosing between WhatsApp and SMS based on user preferences and interaction history. It runs at scheduled times (7 PM and 8 PM) to encourage consistent business tracking and engagement with the SIFA coaching system.

Purpose

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

Based on the workflow structure, this appears to serve youth entrepreneurs in a business coaching program by: - Sending daily reminders to track sales and profits - Maintaining engagement through personalized messages in Swahili - Adapting communication channels based on user preferences and activity - Logging all interactions for program tracking and analysis

How It Works

The workflow operates through several parallel paths depending on timing and user characteristics:

  1. User Selection: Retrieves active youth entrepreneurs from the database, filtering out internal team members and ensuring valid phone numbers
  2. Channel Detection: For each user, checks their preferred communication channel (WhatsApp vs SMS) and recent interaction history
  3. Message Personalization: Creates personalized messages in Swahili using the user's first name, asking about daily sales performance
  4. Smart Delivery:
    • For WhatsApp users: Checks if they've interacted within the last 24 hours, then sends via WhatsApp or falls back to SMS
    • For SMS users: Sends direct SMS messages through Africa's Talking
  5. Activity Logging: Records all sent messages in the chat history database for tracking and analysis
  6. Batch Processing: Processes users in batches to manage system load and API rate limits

The workflow includes special handling for onboarding new users and updating their status to "active" once they receive their first nudge.

Workflow Diagram

graph TD
    A[7PM Nudge Trigger] --> B[Get Active Users]
    C[8PM Nudge Trigger] --> D[Get Active Users 2]
    E[Manual Trigger] --> F[Get Inactive Users]

    B --> G[Remove Duplicates]
    D --> H[Remove Duplicates 2]
    F --> I[Check WhatsApp History]

    G --> J{Channel Check}
    H --> K[Loop Over Items]

    J -->|WhatsApp| L[Loop Over Items 4]
    J -->|SMS| M[Loop Over Items 3]

    L --> N[Check Chat Log]
    M --> O[Set SMS Message]

    N --> P{Chat Record Exists?}
    P -->|Yes| Q{Less than 24hrs?}
    P -->|No| R[Set WhatsApp Redirect Message]

    Q -->|Yes| S[Send WhatsApp Message]
    Q -->|No| R

    S --> T[Log WhatsApp Message]
    R --> U[Send SMS with WhatsApp Link]
    O --> V[Send Direct SMS]

    U --> W[Log SMS Message]
    V --> X[Log SMS Message 2]

    K --> Y[Get Chat History]
    Y --> Z[Filter Recent Activity]
    Z --> AA[Check for Recent Interactions]
    AA -->|No Recent Activity| BB{Channel Type}
    BB -->|WhatsApp| CC[Send WhatsApp]
    BB -->|SMS| DD[Send SMS]

    I --> EE[Update to Active Status]

Triggers

  • 7PM Nudge: Schedule trigger that runs daily at 7:00 PM (Africa/Nairobi timezone)
  • 8PM Nudge: Schedule trigger that runs daily at 8:00 PM (Africa/Nairobi timezone)
  • Manual Trigger: For testing and handling inactive users who need onboarding

Nodes Used

Node Type Purpose
Schedule Trigger Automated daily execution at 7 PM and 8 PM
Manual Trigger Manual workflow execution for testing
Postgres Database operations for user data and chat history
HTTP Request Sending SMS via Africa's Talking API
Twilio Sending WhatsApp messages
If/Filter Conditional logic and data filtering
Split in Batches Processing users in manageable batches
Set Creating and formatting message content
Remove Duplicates Ensuring unique user processing
Aggregate Combining filtered data
No Op Placeholder for chat history processing

External Services & Credentials Required

Africa's Talking SMS API

  • Purpose: Sending SMS messages
  • Credentials: API key (currently hardcoded - should be moved to environment variables)
  • Endpoint: https://api.africastalking.com/version1/messaging

Twilio WhatsApp API

  • Purpose: Sending WhatsApp messages
  • Credentials: Twilio API credentials (stored as "Twilio WhatsApp kdp")
  • From Number: +254203892316

PostgreSQL Database

  • Purpose: User data storage and chat history logging
  • Credentials: PostgreSQL connection (stored as "PostgresOnSupabase")
  • Tables: youthEntrepreneursReal, n8n_chat_histories, chatLog

Environment Variables

The workflow currently has hardcoded values that should be moved to environment variables: - Africa's Talking API key - Africa's Talking username - SMS sender IDs ("24436", "Educate") - WhatsApp sender number

Data Flow

Input Data

  • User records from youthEntrepreneursReal table containing:
    • Phone numbers, names, onboarding status
    • Channel preferences, interaction history
    • Business and demographic information

Processing

  • Filters active users with valid phone numbers
  • Checks recent interaction patterns
  • Personalizes messages with user names
  • Determines optimal communication channel

Output Data

  • SMS messages sent via Africa's Talking
  • WhatsApp messages sent via Twilio
  • Chat history records in database
  • Updated user onboarding status

Error Handling

The workflow includes several error handling mechanisms: - alwaysOutputData flags on database queries to continue processing even with empty results - onError: continueErrorOutput on chat history logging to prevent workflow failure - Conditional checks for data existence before processing - Fallback from WhatsApp to SMS when interaction history is insufficient

Known Limitations

  • API keys are hardcoded in the workflow rather than using environment variables
  • Limited to Kenyan phone numbers (+254 prefix)
  • Messages are currently only in Swahili
  • No retry mechanism for failed message deliveries
  • Batch processing may cause delays for large user bases

No related workflows specified in the context.

Setup Instructions

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

  2. Configure Database Connection:

    • Set up PostgreSQL credentials named "PostgresOnSupabase"
    • Ensure access to tables: youthEntrepreneursReal, n8n_chat_histories, chatLog
  3. Configure Twilio:

    • Create Twilio credentials named "Twilio WhatsApp kdp"
    • Verify WhatsApp Business API setup for +254203892316
  4. Configure Africa's Talking:

    • Replace hardcoded API key with environment variable
    • Verify SMS sender IDs ("24436", "Educate") are registered
  5. Set Schedule:

    • Activate the 7 PM and 8 PM schedule triggers
    • Adjust timezone if needed (currently set to Africa/Nairobi)
  6. Test Execution:

    • Use manual trigger to test with inactive users first
    • Monitor database logs for successful message delivery
    • Verify message formatting and personalization
  7. Security Hardening:

    • Move all API keys to environment variables
    • Review database permissions
    • Set up monitoring for failed executions