Templates Module Specification
1. Purpose & Overview
The module handles WhatsApp Highly Structured Message (HSM) templates, variable placeholders, and approval workflows with Meta.
templates2. Responsibilities
- Maintain and
Templatedomain models.TemplateComponent - Track Meta approval states (,
APPROVED,PENDING).REJECTED - Provide variable binding services for campaign message generation.
3. Directory Structure
text
services/api/templates/
├── admin.py
├── apps.py
├── migrations/
├── models.py
├── serializers.py
└── views.py
4. Models & Database Schemas
python
class TemplateStatus(models.TextChoices):
PENDING = 'PENDING', 'Pending'
APPROVED = 'APPROVED', 'Approved'
REJECTED = 'REJECTED', 'Rejected'
class Template(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4)
tenant = models.ForeignKey('tenants.Tenant', on_delete=models.CASCADE)
name = models.CharField(max_length=255)
category = models.CharField(max_length=50) # 'MARKETING', 'UTILITY'
language = models.CharField(max_length=10, default='en_US')
components = models.JSONField(default=list) # HEADER, BODY, FOOTER, BUTTONS
status = models.CharField(max_length=20, choices=TemplateStatus.choices, default=TemplateStatus.PENDING)
5. Services & Business Logic
- : Replaces
Template.render_body(variables_dict),{{1}}placeholders with customer variable data.{{2}}
6. Serializers & Data Transfer Objects
- : Validates structure of header, body, footer, and button components.
TemplateSerializer
7. Views & API Endpoints
- : List approved templates.
GET /api/templates/ - : Submit new template for Meta approval.
POST /api/templates/
8. Permissions & Role Rules
- &
TENANT_ADMIN: Access templates within active tenant.TENANT_USER
9. Signals & Event Listeners
- : Notifies team when a pending template is approved by Meta.
post_save
10. Background Tasks & Celery Jobs
- Periodic task syncs template statuses from Meta Graph API.
11. Data Flow & External Dependencies
- Integrates with Meta WhatsApp Graph API () for template creation & status sync.
v22.0
12. Business Rules & Validations
- Template names must contain only lowercase alphanumeric characters and underscores.
- Body components must contain valid numbered placeholders ().
{{1}}
13. Sequence Diagram (Mermaid)
Rendering diagram...
14. Known Limitations & Technical Debt
- Local edits to templates must be synced back with Meta's developer portal.
15. Future Improvements
- Add interactive Drag-and-Drop WhatsApp Template Builder in Next.js UI.