updaing whatsapp

This commit is contained in:
Flatlogic Bot 2026-03-13 14:05:01 +00:00
parent 3f3ac7d889
commit 7fefc460c2
2 changed files with 40 additions and 28 deletions

View File

@ -19,16 +19,16 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if ($testPhone === '') {
$testError = 'Phone number is required for testing.';
} else {
// Check if settings are saved first?
// We rely on get_setting() which reads from DB.
// If user just changed input but didn't save, it won't work.
// That's standard behavior.
try {
// We use the new public method in NotificationService
// This method handles the 968 prefix logic internally.
NotificationService::sendWhatsApp($testPhone, $testMessage ?: 'Test message from CargoLink');
$testSuccess = 'Test message sent (check logs/phone).';
$res = NotificationService::sendWhatsApp($testPhone, $testMessage ?: 'Test message from CargoLink');
if ($res['success']) {
$testSuccess = 'Message sent! ' . $res['message'];
} else {
$testError = 'Failed: ' . $res['message'];
}
} catch (Throwable $e) {
$testError = 'Error sending test: ' . $e->getMessage();
}

View File

@ -71,6 +71,7 @@ class NotificationService
if (get_setting('whatsapp_enabled') === '1') {
$phone = self::getPhoneNumber($user);
if ($phone) {
// Return value ignored in async/background context, but logged inside
self::sendWhatsApp($phone, $whatsapp);
} else {
error_log("WHATSAPP Notification skipped for {$user['email']}: Phone number not found.");
@ -107,8 +108,9 @@ class NotificationService
/**
* Send a WhatsApp message via Wablas API.
* @return array ['success' => bool, 'message' => string, 'response' => mixed]
*/
public static function sendWhatsApp(string $phone, string $message)
public static function sendWhatsApp(string $phone, string $message): array
{
// 1. Format Phone Number
// Remove non-numeric characters
@ -125,12 +127,13 @@ class NotificationService
if (!$domain || !$token) {
error_log("WHATSAPP Error: Wablas domain or token not configured.");
return;
return ['success' => false, 'message' => 'Domain or Token missing'];
}
// Ensure domain has no trailing slash and correct scheme
$domain = rtrim($domain, '/');
if (!preg_match('/^https?:\\/', $domain)) {
// If domain doesn't start with http/https, prepend https://
if (!preg_match('/^https?:\/\//', $domain)) {
$domain = 'https://' . $domain;
}
@ -138,40 +141,49 @@ class NotificationService
// Endpoint: /api/send-message (StandardWablas)
$url = $domain . "/api/send-message";
$data = [
$payload = json_encode([
'phone' => $phone,
'message' => $message,
];
]);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
"Authorization: $token",
"Content-Type: application/json" // Wablas often accepts JSON or form-data. Trying JSON first, or form-data if that fails.
// Actually, many Wablas instances use form-data or x-www-form-urlencoded.
// Let's try standard POST fields which curl sends as application/x-www-form-urlencoded by default if passing array.
]);
// Resetting headers to just Authorization for form-urlencoded default
curl_setopt($ch, CURLOPT_HTTPHEADER, [
"Authorization: $token"
"Content-Type: application/json"
]);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // For dev env
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
// curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$error = curl_error($ch);
$curlErr = curl_error($ch);
curl_close($ch);
if ($error) {
error_log("WHATSAPP cURL Error: $error");
return;
if ($curlErr) {
error_log("WHATSAPP cURL Error: $curlErr");
return ['success' => false, 'message' => "Connection error: $curlErr"];
}
$decoded = json_decode($response, true);
$statusMsg = $decoded['message'] ?? $response;
// Log result
error_log("WHATSAPP Sent to $phone. Status: $httpCode. Response: " . substr($response, 0, 100));
// Wablas usually returns {'status': true, ...} or {'status': 'pending'}
// We consider 200 OK and no explicit 'status': false as success for now
$isSuccess = ($httpCode >= 200 && $httpCode < 300);
if (isset($decoded['status']) && $decoded['status'] === false) {
$isSuccess = false;
}
return [
'success' => $isSuccess,
'message' => "API ($httpCode): " . (is_string($statusMsg) ? $statusMsg : json_encode($statusMsg)),
'response' => $decoded
];
}
}
}