54 lines
1.7 KiB
PHP
54 lines
1.7 KiB
PHP
<?php
|
|
require_once __DIR__ . '/../db/config.php';
|
|
|
|
function send_whatsapp_message($phone, $message) {
|
|
if (get_setting('whatsapp_enabled') !== '1') {
|
|
return ['success' => false, 'error' => 'WhatsApp is disabled'];
|
|
}
|
|
|
|
$token = get_setting('wablas_token');
|
|
$server = get_setting('wablas_server', 'https://console.wablas.com');
|
|
$security_key = get_setting('wablas_security_key');
|
|
|
|
if (empty($token) || empty($server)) {
|
|
return ['success' => false, 'error' => 'Wablas configuration is incomplete'];
|
|
}
|
|
|
|
// Clean phone number
|
|
$phone = preg_replace('/[^0-9]/', '', $phone);
|
|
|
|
$curl = curl_init();
|
|
$data = [
|
|
'phone' => $phone,
|
|
'message' => $message,
|
|
];
|
|
|
|
if (!empty($security_key)) {
|
|
$data['security_key'] = $security_key;
|
|
}
|
|
|
|
curl_setopt($curl, CURLOPT_HTTPHEADER, [
|
|
"Authorization: $token",
|
|
]);
|
|
curl_setopt($curl, CURLOPT_URL, rtrim($server, '/') . "/api/send-message");
|
|
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
|
|
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
|
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
|
|
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
|
|
curl_setopt($curl, CURLOPT_TIMEOUT, 10);
|
|
|
|
$result = curl_exec($curl);
|
|
$error = curl_error($curl);
|
|
curl_close($curl);
|
|
|
|
if ($error) {
|
|
return ['success' => false, 'error' => $error];
|
|
}
|
|
|
|
$response = json_decode($result, true);
|
|
if (isset($response['status']) && $response['status'] == true) {
|
|
return ['success' => true, 'response' => $response];
|
|
}
|
|
|
|
return ['success' => false, 'error' => $response['message'] ?? 'Unknown error from Wablas'];
|
|
} |