Update: 2026-05-06 02:59:42
This commit is contained in:
76
app/Services/WhatsAppProxyService.php
Normal file
76
app/Services/WhatsAppProxyService.php
Normal file
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
/**
|
||||
* WhatsApp Proxy Service
|
||||
*
|
||||
* Used to send WhatsApp messages (like OTPs) via Intaleq proxy bots.
|
||||
*/
|
||||
class WhatsAppProxyService
|
||||
{
|
||||
/**
|
||||
* قائمة السيرفرات المتاحة
|
||||
*/
|
||||
private array $servers = [
|
||||
//"https://botmasa.intaleq.xyz/send", // mayar
|
||||
//"https://botmasa2.intaleq.xyz/send", // shad
|
||||
//"https://bootride.intaleq.xyz/send", // ramat bus
|
||||
"https://bot3.intaleq.xyz/send", // shahd
|
||||
//"https://whatsapp.tripz-egypt.com/send" // tripz
|
||||
];
|
||||
|
||||
/**
|
||||
* إرسال رسالة واتساب
|
||||
*
|
||||
* @param string $to رقم الهاتف
|
||||
* @param string $message نص الرسالة
|
||||
* @return bool نجاح الإرسال
|
||||
*/
|
||||
public function sendMessage(string $to, string $message): bool
|
||||
{
|
||||
if (empty($this->servers)) {
|
||||
error_log("[WhatsAppProxyService] No servers available.");
|
||||
return false;
|
||||
}
|
||||
|
||||
// اختيار سيرفر عشوائي من القائمة المتاحة لتوزيع الحمل
|
||||
$url = $this->servers[array_rand($this->servers)];
|
||||
|
||||
$payload = [
|
||||
"to" => $to,
|
||||
"message" => $message
|
||||
];
|
||||
|
||||
$curl = curl_init();
|
||||
curl_setopt_array($curl, [
|
||||
CURLOPT_URL => $url,
|
||||
CURLOPT_RETURNTRANSFER => true,
|
||||
CURLOPT_CUSTOMREQUEST => "POST",
|
||||
CURLOPT_POSTFIELDS => json_encode($payload, JSON_UNESCAPED_UNICODE),
|
||||
CURLOPT_HTTPHEADER => [
|
||||
"Content-Type: application/json"
|
||||
],
|
||||
CURLOPT_TIMEOUT => 15, // مهلة 15 ثانية للطلب
|
||||
]);
|
||||
|
||||
$response = curl_exec($curl);
|
||||
$err = curl_error($curl);
|
||||
curl_close($curl);
|
||||
|
||||
if ($err) {
|
||||
error_log("[WhatsAppProxyService] cURL Error on $url: $err");
|
||||
return false;
|
||||
}
|
||||
|
||||
$responseData = json_decode($response, true);
|
||||
|
||||
// التحقق من حالة الرد (يمكنك تخصيصه حسب هيكل رد البوت الخاص بك)
|
||||
if (isset($responseData['error']) && $responseData['error']) {
|
||||
error_log("[WhatsAppProxyService] API Error on $url: " . json_encode($responseData));
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -76,12 +76,13 @@ if ($fp) {
|
||||
fclose($fp);
|
||||
}
|
||||
|
||||
// 5. Send OTP via SMS
|
||||
// TODO: Replace with your actual SMS provider
|
||||
$smsSent = sendOtpSms($phone, $otp);
|
||||
// 5. Send OTP via WhatsApp Proxy
|
||||
$whatsappService = new \App\Services\WhatsAppProxyService();
|
||||
$message = "رمز التحقق لتطبيق مُصادَق:\n*{$otp}*\n\nصالح لمدة 5 دقائق.";
|
||||
$smsSent = $whatsappService->sendMessage($phone, $message);
|
||||
|
||||
if (!$smsSent) {
|
||||
error_log("WARN: Failed to send OTP SMS to phone hash: {$phoneHash}");
|
||||
error_log("WARN: Failed to send OTP WhatsApp to phone hash: {$phoneHash}");
|
||||
// Still return success to not reveal info, but log the issue
|
||||
}
|
||||
|
||||
@@ -90,48 +91,7 @@ if (env('APP_DEBUG', 'false') === 'true') {
|
||||
error_log("DEV OTP for {$phone}: {$otp}");
|
||||
}
|
||||
|
||||
json_success(null, 'إذا كان الرقم مسجلاً، سيتم إرسال رمز التحقق');
|
||||
json_success(null, 'إذا كان الرقم مسجلاً، سيتم إرسال رمز التحقق عبر واتساب');
|
||||
|
||||
|
||||
|
||||
// ─── SMS Helper ──────────────────────────────────────────
|
||||
function sendOtpSms(string $phone, string $otp): bool
|
||||
{
|
||||
$smsProvider = env('SMS_PROVIDER', 'log'); // 'log', 'twilio', 'jordan_sms', 'custom'
|
||||
|
||||
$message = "رمز التحقق لتطبيق مُصادَق: {$otp}\nصالح لمدة 5 دقائق.";
|
||||
|
||||
switch ($smsProvider) {
|
||||
case 'custom':
|
||||
// Custom SMS API (your own provider)
|
||||
$apiUrl = env('SMS_API_URL');
|
||||
$apiKey = env('SMS_API_KEY');
|
||||
if (!$apiUrl || !$apiKey) return false;
|
||||
|
||||
try {
|
||||
$ch = curl_init($apiUrl);
|
||||
curl_setopt_array($ch, [
|
||||
CURLOPT_POST => true,
|
||||
CURLOPT_POSTFIELDS => json_encode([
|
||||
'to' => $phone,
|
||||
'message' => $message,
|
||||
'api_key' => $apiKey,
|
||||
]),
|
||||
CURLOPT_HTTPHEADER => ['Content-Type: application/json'],
|
||||
CURLOPT_RETURNTRANSFER => true,
|
||||
CURLOPT_TIMEOUT => 10,
|
||||
]);
|
||||
$response = curl_exec($ch);
|
||||
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
curl_close($ch);
|
||||
return $httpCode >= 200 && $httpCode < 300;
|
||||
} catch (\Exception $e) {
|
||||
error_log("SMS send error: " . $e->getMessage());
|
||||
return false;
|
||||
}
|
||||
|
||||
case 'log':
|
||||
default:
|
||||
// Development: just log the OTP
|
||||
error_log("SMS OTP [{$phone}]: {$otp}");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user