Skip to content

SIFA Agent Reply

This workflow enables human agents to send messages to users through either WhatsApp or SMS channels, while tracking agent takeover sessions in a database. When an agent needs to respond to a user inquiry, they can send a message through this webhook endpoint, and the system will route it to the appropriate messaging channel and record the agent interaction.

Purpose

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

How It Works

  1. Webhook Trigger: The workflow starts when an external system (like an agent dashboard) sends a POST request to the webhook endpoint
  2. Variable Processing: Extracts and processes the incoming data including phone number, message content, preferred channel, and agent name
  3. Agent Session Tracking: Records the agent takeover in a PostgreSQL database with a 30-minute expiration time
  4. Channel Detection: Determines whether to send via WhatsApp or SMS based on the channel parameter
  5. Message Delivery: Routes the message through either Twilio (WhatsApp) or Africa's Talking (SMS)
  6. Response: Returns a success confirmation with delivery details

Workflow Diagram

graph TD
    A[Agent Reply Webhook] --> B[Set Variables]
    B --> C[Set Agent Takeover]
    C --> D{Is WhatsApp?}
    D -->|Yes| E[Send WhatsApp via Twilio]
    D -->|No| F[Send SMS via Africa's Talking]
    E --> G[Respond WhatsApp Success]
    F --> H[Respond SMS Success]

Trigger

Webhook: POST request to /sifa-agent-reply

Expected payload:

1
2
3
4
5
6
{
  "phone": "+254712345678",
  "message": "Hello, how can I help you?",
  "channel": "whatsapp",
  "agent": "John Doe"
}

Nodes Used

Node Type Node Name Purpose
Webhook Agent Reply Webhook2 Receives incoming agent reply requests
Set Set Variables1 Processes and formats incoming data
Postgres Set Agent Takeover Records agent session in database
If Is WhatsApp?2 Routes message based on channel type
HTTP Request Send WhatsApp (HTTP)1 Sends WhatsApp messages via Twilio
HTTP Request Send SMS via Africas Talking2 Sends SMS messages via Africa's Talking
Respond to Webhook Respond WhatsApp1 Returns WhatsApp delivery confirmation
Respond to Webhook Respond SMS1 Returns SMS delivery confirmation

External Services & Credentials Required

Twilio (WhatsApp)

  • Credential: twilioApi
  • Purpose: Send WhatsApp messages
  • Required: Account SID, Auth Token
  • From Number: +254203892316

Africa's Talking (SMS)

  • Purpose: Send SMS messages
  • Required: API Key, Username
  • From Number: 24436

PostgreSQL Database

  • Credential: postgres
  • Purpose: Track agent takeover sessions
  • Required: Database connection details

Environment Variables

No environment variables are used in this workflow. All configuration is handled through node parameters and credentials.

Data Flow

Input

1
2
3
4
5
6
{
  "phone": "string",      // Recipient phone number
  "message": "string",    // Message content
  "channel": "string",    // "whatsapp" or "sms" (defaults to "whatsapp")
  "agent": "string"       // Agent name (defaults to "Agent")
}

Output (WhatsApp)

1
2
3
4
5
6
{
  "success": true,
  "channel": "whatsapp",
  "phone": "+254712345678",
  "status": "queued"
}

Output (SMS)

1
2
3
4
5
{
  "success": true,
  "channel": "sms",
  "phone": "+254712345678"
}

Error Handling

The workflow includes basic error handling: - All nodes are configured to continue on error (continueRegularOutput or continueErrorOutput) - If the channel detection fails, it defaults to the SMS path - Database operations continue even if they fail, ensuring message delivery isn't blocked

Known Limitations

  • Agent takeover sessions expire after 30 minutes
  • WhatsApp messages are limited by Twilio's rate limits and policies
  • SMS messages have character encoding limitations (em dashes and arrows are replaced)
  • No validation of phone number formats
  • No duplicate message prevention

No related workflows specified in the context.

Setup Instructions

  1. Import the Workflow

    • Copy the workflow JSON
    • Import into your n8n instance
  2. Configure Credentials

    • Set up Twilio API credentials for WhatsApp
    • Configure Africa's Talking API key
    • Set up PostgreSQL database connection
  3. Database Setup Create the agent_takeover table:

    1
    2
    3
    4
    5
    6
    CREATE TABLE agent_takeover (
      phone_number VARCHAR PRIMARY KEY,
      agent_name VARCHAR NOT NULL,
      taken_at TIMESTAMP NOT NULL,
      expires_at TIMESTAMP NOT NULL
    );
    

  4. Test the Webhook

    • Activate the workflow
    • Send a test POST request to the webhook URL
    • Verify messages are delivered and database is updated
  5. Integration

    • Use the webhook URL in your agent dashboard or CRM
    • Ensure proper payload format is sent
    • Monitor execution logs for any issues