updaing whatsapp
This commit is contained in:
parent
3f3ac7d889
commit
7fefc460c2
@ -19,16 +19,16 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|||||||
if ($testPhone === '') {
|
if ($testPhone === '') {
|
||||||
$testError = 'Phone number is required for testing.';
|
$testError = 'Phone number is required for testing.';
|
||||||
} else {
|
} 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 {
|
try {
|
||||||
// We use the new public method in NotificationService
|
// We use the new public method in NotificationService
|
||||||
// This method handles the 968 prefix logic internally.
|
// This method handles the 968 prefix logic internally.
|
||||||
NotificationService::sendWhatsApp($testPhone, $testMessage ?: 'Test message from CargoLink');
|
$res = NotificationService::sendWhatsApp($testPhone, $testMessage ?: 'Test message from CargoLink');
|
||||||
$testSuccess = 'Test message sent (check logs/phone).';
|
|
||||||
|
if ($res['success']) {
|
||||||
|
$testSuccess = 'Message sent! ' . $res['message'];
|
||||||
|
} else {
|
||||||
|
$testError = 'Failed: ' . $res['message'];
|
||||||
|
}
|
||||||
} catch (Throwable $e) {
|
} catch (Throwable $e) {
|
||||||
$testError = 'Error sending test: ' . $e->getMessage();
|
$testError = 'Error sending test: ' . $e->getMessage();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -71,6 +71,7 @@ class NotificationService
|
|||||||
if (get_setting('whatsapp_enabled') === '1') {
|
if (get_setting('whatsapp_enabled') === '1') {
|
||||||
$phone = self::getPhoneNumber($user);
|
$phone = self::getPhoneNumber($user);
|
||||||
if ($phone) {
|
if ($phone) {
|
||||||
|
// Return value ignored in async/background context, but logged inside
|
||||||
self::sendWhatsApp($phone, $whatsapp);
|
self::sendWhatsApp($phone, $whatsapp);
|
||||||
} else {
|
} else {
|
||||||
error_log("WHATSAPP Notification skipped for {$user['email']}: Phone number not found.");
|
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.
|
* 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
|
// 1. Format Phone Number
|
||||||
// Remove non-numeric characters
|
// Remove non-numeric characters
|
||||||
@ -125,12 +127,13 @@ class NotificationService
|
|||||||
|
|
||||||
if (!$domain || !$token) {
|
if (!$domain || !$token) {
|
||||||
error_log("WHATSAPP Error: Wablas domain or token not configured.");
|
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
|
// Ensure domain has no trailing slash and correct scheme
|
||||||
$domain = rtrim($domain, '/');
|
$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;
|
$domain = 'https://' . $domain;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -138,40 +141,49 @@ class NotificationService
|
|||||||
// Endpoint: /api/send-message (StandardWablas)
|
// Endpoint: /api/send-message (StandardWablas)
|
||||||
$url = $domain . "/api/send-message";
|
$url = $domain . "/api/send-message";
|
||||||
|
|
||||||
$data = [
|
$payload = json_encode([
|
||||||
'phone' => $phone,
|
'phone' => $phone,
|
||||||
'message' => $message,
|
'message' => $message,
|
||||||
];
|
]);
|
||||||
|
|
||||||
$ch = curl_init($url);
|
$ch = curl_init($url);
|
||||||
curl_setopt($ch, CURLOPT_HTTPHEADER, [
|
curl_setopt($ch, CURLOPT_HTTPHEADER, [
|
||||||
"Authorization: $token",
|
"Authorization: $token",
|
||||||
"Content-Type: application/json" // Wablas often accepts JSON or form-data. Trying JSON first, or form-data if that fails.
|
"Content-Type: application/json"
|
||||||
// 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"
|
|
||||||
]);
|
]);
|
||||||
curl_setopt($ch, CURLOPT_POST, true);
|
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_RETURNTRANSFER, true);
|
||||||
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
|
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
|
||||||
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // For dev env
|
// curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
||||||
|
|
||||||
$response = curl_exec($ch);
|
$response = curl_exec($ch);
|
||||||
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||||
$error = curl_error($ch);
|
$curlErr = curl_error($ch);
|
||||||
curl_close($ch);
|
curl_close($ch);
|
||||||
|
|
||||||
if ($error) {
|
if ($curlErr) {
|
||||||
error_log("WHATSAPP cURL Error: $error");
|
error_log("WHATSAPP cURL Error: $curlErr");
|
||||||
return;
|
return ['success' => false, 'message' => "Connection error: $curlErr"];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$decoded = json_decode($response, true);
|
||||||
|
$statusMsg = $decoded['message'] ?? $response;
|
||||||
|
|
||||||
// Log result
|
// Log result
|
||||||
error_log("WHATSAPP Sent to $phone. Status: $httpCode. Response: " . substr($response, 0, 100));
|
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
|
||||||
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user