# WhatsApp Campaign API Call & Template Variables --- ## CRED-6166 — 2026-04-23 ### Overview Resolved a silent failure in the WhatsApp bulk campaign feature where messages were not triggering the Narad WhatsApp endpoint correctly. The API required a structured array with `templateId` and `args` instead of just a raw string message. --- ### Logic Changed - `CampaignController::runCampDataJob($campaign, $data)`: Updated the `sendWhatsAppMessage` method call. Instead of passing the raw string `$message`, it now passes an array `$whatsappData` containing: - `templateId`: Fetched from `$campaign->template_id`. - `messageString`: The actual message text. - `args`: Array (initially populated with the message) to satisfy the Narad API requirements. --- ### Impact — Files Changed | File | Type | Change | |------|------|--------| | `app/Http/Controllers/Admin/CampaignController.php` | Modified | Updated `sendWhatsAppMessage()` call to pass a structured array instead of a string. | --- ## Dynamic WhatsApp Template Variables — 2026-04-24 ### Overview Resolved a flow gap in the bulk WhatsApp campaign feature where template placeholders (e.g., `{{1}}`, `{{2}}`) were not mapping to dynamic data correctly. Implemented a new "WhatsApp Variables" input field in the campaign creation UI, updated the database schema to store these variables, and modified the backend logic to parse and map query parameters to the template's `args` array for the Narad WhatsApp API. --- ### Logic Added & Changed #### Campaign Variables & Database Schema - A new column `whatsapp_variables` was added to the `campaign` table to store a comma-separated list of query columns intended to replace placeholders in the WhatsApp template. #### Campaign Creation UI - **Run Campaign View** (`run-campaign.blade.php`): Added a new input field **WhatsApp Variables** below the Template ID in the campaign scheduling form. - The Javascript was updated to extract the `whatsapp_variables` input and include it in the AJAX request payload. #### WhatsApp API Call & Arguments Mapping - `CampaignController::runCampDataJob($campaign, $data)`: - Added logic to parse `whatsapp_variables`. If present, it splits the string by comma and iteratively maps each variable name to its corresponding value from the `$data` object (from the campaign's query result). - The dynamically constructed `args` array is then included in the `$whatsappData` structure passed to `sendWhatsAppMessage()`. - `WhatsApp::setProvider()`: - For the `NARAD` provider configuration, added logic to fetch and assign `NARAD_API_KEY` to the `$this->key` property. - Fixed a property declaration issue by removing a duplicate `private $key;` definition, ensuring it relies on the properly inherited property. --- ### Impact — Files Changed / Added | File | Type | Change | |------|------|--------| | `app/Campaigns.php` | Modified | Added `'whatsapp_variables'` to the `$fillable` array. | | `app/Http/Controllers/Admin/CampaignController.php` | Modified | Added logic to save `whatsapp_variables` during campaign creation and dynamically parse them into the `args` array during job execution. | | `app/WhatsApp.php` | Modified | Added `NARAD_API_KEY` binding inside `setProvider()` and removed a duplicate `$key` property definition. | | `resources/views/admin/reports/run-campaign.blade.php` | Modified | Added the `whatsapp_variables` input field in the UI and updated the AJAX request payload. | | `database/migrations/2026_04_24_135548_add_whatsapp_variables_to_campaign_table.php` | Added | Migration to add the `whatsapp_variables` column to the `campaign` table. |