Files
api_arrahn/app/Console/Commands/WhatsappTestSend.php
T
2026-09-20 16:20:36 +08:00

56 lines
1.6 KiB
PHP

<?php
namespace App\Console\Commands;
use App\Services\Whatsapp\WhatsappCloudClient;
use Illuminate\Console\Command;
class WhatsappTestSend extends Command
{
protected $signature = 'whatsapp:test {--to= : Recipient MSISDN, default ADMIN_PHONE_NUMBER}';
protected $description = 'Send the configured WhatsApp Cloud template to a test number';
public function handle()
{
$client = new WhatsappCloudClient();
if (!$client->isConfigured()) {
$this->error('Set WHATSAPP_TOKEN and WHATSAPP_PHONE_NUMBER_ID in .env first.');
return 1;
}
$to = $client->normalizeMsisdn($this->option('to') ?: env('ADMIN_PHONE_NUMBER'));
if (!$to) {
$this->error('Pass --to=60193450165 or set ADMIN_PHONE_NUMBER.');
return 1;
}
$template = config('whatsapp.template', 'hello_world');
$lang = config('whatsapp.template_lang', 'en_US');
$bodyParams = [];
if (strtolower($template) !== 'hello_world') {
$bodyParams = [
'Tampoi',
'TEST-SAG',
'100.00',
'Maybank ****1234',
date('d/m/Y'),
'TEST-REF',
];
}
$this->info("Sending {$template} ({$lang}) to {$to} …");
$response = $client->sendTemplate($to, $template, $lang, $bodyParams);
if ($response->successful()) {
$this->info('OK ' . $response->status() . ' ' . $response->body());
return 0;
}
$this->error('FAIL ' . $response->status() . ' ' . $response->body());
return 1;
}
}