V4 - MessageStatusPoller¶
This workflow acts as a safety net for SMS message delivery tracking by periodically checking the status of messages that may have missed their Twilio status callbacks. It runs every 15 minutes to ensure message delivery statuses are accurately recorded in the database.
Purpose¶
No business context provided yet — add a context.md to enrich this documentation.
This workflow serves as a backup mechanism for message status tracking in the V4 system. When Twilio sends SMS messages, it normally provides status updates via webhooks (status callbacks). However, these callbacks can sometimes fail or be missed due to network issues, server downtime, or other technical problems. This poller ensures that message statuses are eventually updated by actively querying Twilio's API for messages that appear to be stuck in intermediate states.
How It Works¶
- Schedule Trigger: Every 15 minutes, the workflow automatically starts
- Find Pending Messages: Queries the database for messages that are still in intermediate states (queued, sent, sending, accepted, scheduled) and were sent more than 3 minutes ago but less than 2 days ago
- Check Status with Twilio: For each pending message, makes an API call to Twilio to get the current status
- Parse Response: Extracts the message SID, status, and any error codes from Twilio's response
- Validate Data: Checks if the response contains valid status information
- Update Database: If valid status data is found, updates the message log with the new status and sets appropriate timestamps for delivery, read, or failure events
Mermaid Diagram¶
graph TD
A[Every 15 min] --> B[SelectPending]
B --> C[GetStatus]
C --> D[Parse]
D --> E[IF status]
E -->|Valid Status| F[UpdateLog]
E -->|No Status| G[End]
Trigger¶
- Type: Schedule Trigger
- Frequency: Every 15 minutes
- Purpose: Automated polling to catch missed status updates
Nodes Used¶
| Node | Type | Purpose |
|---|---|---|
| Every 15 min | Schedule Trigger | Starts the workflow every 15 minutes |
| SelectPending | PostgreSQL | Finds messages with pending statuses older than 3 minutes |
| GetStatus | HTTP Request | Queries Twilio API for current message status |
| Parse | Code | Extracts SID, status, and error code from Twilio response |
| IF status | If | Validates that response contains usable status data |
| UpdateLog | PostgreSQL | Updates message status and timestamps in database |
External Services & Credentials Required¶
Twilio API¶
- Service: Twilio Messages API
- Endpoint:
https://api.twilio.com/2010-04-01/Accounts/{AccountSid}/Messages/{MessageSid}.json - Authentication: HTTP Basic Auth
- Required Credential: "Waringa twilio auth" (Account SID and Auth Token)
Database¶
- Service: PostgreSQL
- Required Credential: "sifaV4Dev" (database connection details)
- Tables Used:
v4_message_log
Environment Variables¶
No environment variables are explicitly used in this workflow. All configuration is handled through: - Hardcoded Twilio Account SID in the API URL - Database credentials stored in n8n credential manager - Fixed polling interval (15 minutes)
Data Flow¶
Input¶
- Trigger: Time-based (every 15 minutes)
- Database Query: Message SIDs from
v4_message_logtable where status is pending
Processing¶
- API Calls: Individual Twilio API requests for each pending message
- Data Transformation: Raw Twilio responses parsed into structured status data
Output¶
- Database Updates: Updated message statuses, timestamps, and error codes in
v4_message_log - Batch Processing: Handles up to 100 messages per execution
Error Handling¶
- HTTP Request: Configured with
neverError: trueto handle API failures gracefully - Response Validation: IF node filters out invalid or empty responses before database updates
- Database Safety: Uses NULLIF and CASE statements to prevent invalid data insertion
- Batch Limiting: LIMIT 100 prevents overwhelming the system with too many API calls
Known Limitations¶
- Batch Size: Limited to 100 messages per execution to avoid API rate limits
- Time Window: Only processes messages between 3 minutes and 2 days old
- No Retry Logic: Failed API calls are not retried within the same execution
- Hardcoded Account: Twilio Account SID is hardcoded in the URL
Related Workflows¶
No related workflows specified in the context.
Setup Instructions¶
-
Import Workflow: Import the JSON into your n8n instance
-
Configure Database Credential:
- Create PostgreSQL credential named "sifaV4Dev"
- Configure connection to database containing
v4_message_logtable
-
Configure Twilio Credential:
- Create HTTP Basic Auth credential named "Waringa twilio auth"
- Set username to your Twilio Account SID
- Set password to your Twilio Auth Token
-
Update Account SID:
- Edit the GetStatus node
- Replace
AC906ca9524b640bdad7d029fd565b6a02with your actual Twilio Account SID
-
Verify Database Schema:
- Ensure
v4_message_logtable exists with required columns:message_sid,status,error_code,sent_at,delivered_at,read_at,failed_at,updated_at
- Ensure
-
Test Execution:
- Run manually to verify database connectivity and API access
- Check that status updates are properly recorded
-
Activate Workflow:
- Enable the workflow to start automatic polling every 15 minutes