WhatsApp Template Implementation Summary
π― Overviewβ
We've successfully updated the Multi-Channel Communication System to support WhatsApp Message Templates, resolving the Error 63016: "Failed to send freeform message because you are outside the allowed window" issue.
β What Was Implementedβ
1. Database Schema Updateβ
- Added
provider_template_idcolumn tocommunication_templatetable - Stores WhatsApp template SIDs from Twilio Content API (format:
HXe835f49d0aa7f1639f7f8e4b8a1e6c1e)
2. TypeScript Types Updatedβ
- Added
providerTemplateIdfield toCommunicationTemplateinterface - Updated all related types and DTOs
3. DTOs Enhancedβ
CreateTemplateDto: AddedproviderTemplateIdfieldSendCommunicationDto: AddedproviderTemplateIdfield
4. WhatsApp Adapter Refactoredβ
- Dual Mode Support:
- Template Messages: Uses Twilio Content API with
contentSid - Freeform Messages: Uses regular Twilio Messages API (24-hour window only)
- Template Messages: Uses Twilio Content API with
- Automatically detects which mode to use based on
providerTemplateIdpresence - Proper variable mapping for Twilio Content API
5. Communications Service Updatedβ
- Fetches template from database to get
providerTemplateId - For WhatsApp + template: Uses provider template instead of rendering
- For other channels: Continues to render templates normally
- Passes
providerTemplateIdthrough the queue to the adapter
6. Documentation Createdβ
whatsapp-templates-setup-guide.md: Complete guide for setting up WhatsApp templates in Twiliowhatsapp-template-implementation-summary.md(this file): Implementation summary
π How It Worksβ
Sending Flow:β
1. User calls /communications/send with:
- channel: "whatsapp"
- templateId: "uuid-of-template"
- templateVariables: {"1": "John", "2": "INV-001", ...}
2. CommunicationsService:
- Fetches template from database
- Checks if template has providerTemplateId
- If yes: Sets providerTemplateId for WhatsApp
- If no: Renders template normally
3. Communication queued with providerTemplateId
4. Queue processes communication
5. WhatsAppAdapterService:
- Detects providerTemplateId is set
- Calls sendTemplateMessage()
- Uses Twilio Content API with contentSid
- Maps templateVariables to Twilio format
6. Message sent via approved WhatsApp template β
π Setup Steps (Quick Reference)β
For Developersβ
-
Run Migration:
cd packages/backend/database
npm run migrate:up -
Rebuild Backend:
cd apps/backend
npm run build -
Restart Services:
# Restart your backend server
For Business Usersβ
-
Create WhatsApp Templates in Twilio:
- Follow guide:
whatsapp-templates-setup-guide.md - Submit templates for approval
- Wait for Meta/WhatsApp approval (1-24 hours)
- Follow guide:
-
Get Content SID:
- Copy from Twilio Console after approval
- Format:
HXe835f49d0aa7f1639f7f8e4b8a1e6c1e
-
Update FlowPOS Templates:
curl -X PATCH 'https://api.flowpos.com/communication-templates/{id}' \
-H 'Authorization: Bearer TOKEN' \
-H 'Content-Type: application/json' \
-d '{
"providerTemplateId": "HXe835f49d0aa7f1639f7f8e4b8a1e6c1e"
}' -
Send WhatsApp Messages:
- Messages automatically use templates
- No code changes needed
- Works outside 24-hour window
π¨ Example Usageβ
Before (Failed with Error 63016):β
# Sending freeform message - FAILS outside 24-hour window
curl -X POST 'https://api.flowpos.com/communications/send' \
-H 'Authorization: Bearer TOKEN' \
-H 'Content-Type: application/json' \
-d '{
"channel": "whatsapp",
"recipientContact": "+50256917111",
"content": "Your invoice is ready" # β Fails!
}'
After (Works with Templates):β
# Sending template message - WORKS anytime
curl -X POST 'https://api.flowpos.com/communications/send' \
-H 'Authorization: Bearer TOKEN' \
-H 'Content-Type: application/json' \
-d '{
"businessId": "uuid",
"createdBy": "uuid",
"channel": "whatsapp",
"type": "invoice",
"recipientType": "customer",
"recipientId": "uuid",
"recipientContact": "+50256917111",
"templateId": "uuid-of-invoice-template",
"templateVariables": {
"1": "John Doe",
"2": "INV-001",
"3": "$150.00",
"4": "2025-11-01"
}
}' # β
Works!
π Database Changesβ
communication_template Table:β
-- New column added
ALTER TABLE communication_template
ADD COLUMN provider_template_id VARCHAR;
-- Example data
UPDATE communication_template
SET provider_template_id = 'HXe835f49d0aa7f1639f7f8e4b8a1e6c1e'
WHERE code = 'invoice_whatsapp';
π Code Changes Summaryβ
Files Modified:β
-
Migration:
packages/backend/database/src/migrations/2025-10-24t00:25:51.900z-communication-system.mjs- Added
provider_template_idcolumn
-
Types:
packages/backend/database/src/types/database.types.ts- Added
providerTemplateId: string | null
-
DTOs:
apps/backend/src/communication-templates/interfaces/dtos/create-template.dto.tsapps/backend/src/communications/interfaces/dtos/send-communication.dto.ts- Added
providerTemplateIdfield
-
Services:
apps/backend/src/communications/application/communications.service.ts- Fetches and passes
providerTemplateId
-
Adapters:
apps/backend/src/communications/application/adapters/whatsapp-adapter.service.ts- Refactored to support both template and freeform messages
π― Benefitsβ
For Users:β
- β Send WhatsApp messages anytime (no 24-hour window restriction)
- β Professional, consistent messaging
- β Higher delivery rates
- β Better customer experience
For Developers:β
- β Clean, maintainable code
- β Clear separation of concerns
- β Backward compatible (freeform still works within 24 hours)
- β Comprehensive logging
For Business:β
- β Compliant with WhatsApp Business policies
- β Scalable messaging solution
- β Better customer engagement
- β Reduced support tickets
β οΈ Important Notesβ
Template Requirements:β
- Must be approved by Meta/WhatsApp
- Approval takes 1-24 hours
- Follow WhatsApp template guidelines
- Test thoroughly before production
Variable Mapping:β
- Variables are numbered:
1,2,3(not{{1}}) - Must be strings
- All required variables must be provided
Backward Compatibility:β
- Freeform messages still work (within 24-hour window)
- Templates are optional (can still send without)
- Existing code continues to work
π Troubleshootingβ
Error 63016 Still Occurring:β
- Template not approved yet
providerTemplateIdnot set in database- Using wrong Content SID
Template Variables Not Working:β
- Check variable numbering (
1,2,3) - Ensure all variables are strings
- Verify template structure matches
Message Not Sending:β
- Check
API_URLenvironment variable is set - Verify Twilio credentials
- Confirm template is active
π Next Stepsβ
-
Create Templates in Twilio:
- Invoice template
- Payment reminder template
- Order confirmation template
-
Update Database:
- Add
provider_template_idto templates - Test with real Content SIDs
- Add
-
Test Thoroughly:
- Send test messages
- Verify delivery
- Monitor logs
-
Production Deployment:
- Run migration
- Deploy updated code
- Monitor performance
π Summaryβ
WhatsApp template support is now fully implemented! You can now send WhatsApp messages at any time without worrying about the 24-hour messaging window restriction.
For detailed setup instructions, see: whatsapp-templates-setup-guide.md
Questions or Issues?
- Check the troubleshooting section above
- Review the setup guide
- Contact the development team