Deployment (Docker)
Relevant source files
The following files were used as context for generating this wiki page:The WhatsApp Message Templates feature allows users to create, manage, and send pre-approved messages via the WhatsApp Business API. This functionality is crucial for initiating conversations with customers or sending notifications. The entire feature is managed through a dedicated "Templates" page in the application's dashboard, providing a comprehensive interface for CRUD operations and interaction with the Meta (Facebook) API.
The system relies on the netflie/whatsapp-cloud-api package, although the core logic for template management is handled by a custom TemplateService.
Sources: composer.json:13, app/Services/TemplateService.php
Feature Overview
The "Templates" page, accessible from the main dashboard sidebar, provides a user interface for all template-related tasks. The page is built using Laravel Blade and powered by Alpine.js for dynamic frontend functionality.
Sources: resources/views/components/dashboard-sidebar.blade.php:41, resources/views/dashboard/templates.blade.php
The diagram below shows the high-level components of the Templates UI.
Key UI Components
| Component | Description | Source File |
|---|---|---|
| Sync from Meta | A button to refresh the local template list with the latest data from the WhatsApp Business API. This is only visible in debug mode. | resources/views/dashboard/templates.blade.php:69-74 |
| Create Template | Opens a modal to create a new message template. | resources/views/dashboard/templates.blade.php:75-78 |
| Filters | Allows users to filter templates by status (APPROVED, PENDING, REJECTED), category, or a text search. | resources/views/dashboard/templates.blade.php:83-106 |
| Template Card | Displays individual template information, including its name, status, category, a preview, and action buttons. | resources/views/dashboard/templates.blade.php:116-196 |
| Modals | The interface uses modals for creating, viewing, editing, deleting, and sending templates, all managed by Alpine.js. | resources/views/dashboard/templates.blade.php:205-1033 |
Backend Service (TemplateService)
The app/Services/TemplateService.php class is the core of the template management feature. It acts as an abstraction layer over the WhatsApp Business Management API, handling data validation, payload construction, and API communication.
*Sources: app/Services/TemplateService.php *
The diagram below illustrates the structure of the TemplateService class.
*Sources: app/Services/TemplateService.php *
Key Methods
setCredentials(string $accessToken, string $businessAccountId): Allows for dynamic setting of API credentials, enabling multi-tenant support where each user can have their own WhatsApp account.
Sources: app/Services/TemplateService.php:26-32- Validation Methods: A suite of methods (
validateTemplateName,validateBody,validateFooter,validateButtons,validateVariableSequence) ensures that template data conforms to Meta's API rules before submission.
Sources: app/Services/TemplateService.php:42-160 buildComponents(array $formData): This crucial method transforms the flat structure from the frontend form into the nestedcomponentsarray required by the WhatsApp API. It handles headers, body, footers, and buttons.
Sources: app/Services/TemplateService.php:200-285createTemplate(array $data): Constructs the full payload and sends aPOSTrequest to the/{business-account-id}/message_templatesendpoint to create a new template.
Sources: app/Services/TemplateService.php:292-378updateTemplate(string $templateId, array $data): Sends aPOSTrequest to the/{template-id}endpoint to update an existing template.
Sources: app/Services/TemplateService.php:385-460deleteTemplate(string $templateName, ?string $templateId): Sends aDELETErequest to the/{waba-id}/message_templatesendpoint with the template name as a query parameter to delete a template.
Sources: app/Services/TemplateService.php:467-533
Template Creation Flow
Creating a template involves a multi-step process with both frontend and backend validation before the final API call to Meta.
The sequence diagram below details the creation flow.
Template Form Fields
The creation and editing modals present a form to construct the template.
| Field | Description | Validation Rules | Source |
|---|---|---|---|
| Name | The unique identifier for the template. | Required, lowercase letters, numbers, and underscores only. | app/Services/TemplateService.php:42-59 |
| Category | The purpose of the template (MARKETING, UTILITY, AUTHENTICATION). | Required. | app/Services/TemplateService.php:168-172 |
| Language | The language of the template (e.g., en, id). | Required. | app/Services/TemplateService.php:174-178 |
| Header | Optional. Can be TEXT, IMAGE, VIDEO, or DOCUMENT. Text headers have a 60-character limit. | Text: max 60 chars. Media: requires an example URL for approval. | resources/views/dashboard/templates.blade.php:571-592 |
| Body | Required. The main message content. Variables are denoted with {{1}}, {{2}}, etc. | Required, max 1024 chars. Variables should be sequential (warning). | app/Services/TemplateService.php:96-113, app/Services/TemplateService.php:185-201 |
| Footer | Optional. A short line of text at the bottom of the message. | Max 60 chars. | app/Services/TemplateService.php:66-80 |
| Buttons | Optional. Can be QUICK_REPLY (up to 10) or CTA (Call to Action, up to 2). | Cannot mix types. Max 10 quick replies or 2 CTAs. | app/Services/TemplateService.php:115-152 |
Template Management API Endpoints
The frontend interacts with a set of internal API endpoints to manage templates. These endpoints are wrappers around the TemplateService.
| Method | Endpoint | Description |
|---|---|---|
GET | /api/whatsapp/templates | Fetches all templates. Accepts ?refresh=true to sync from Meta. |
POST | /api/whatsapp/templates | Creates a new template. |
PUT | /api/whatsapp/templates/{id} | Updates an existing template. |
DELETE | /api/whatsapp/templates/{name} | Deletes a template by its name. |
POST | /api/whatsapp/send-template | Sends a template message to a specified contact. |
Sources: resources/views/dashboard/templates.blade.php:1062, 1108, 1213, 1482, 1585