Deploy: 2026-05-23 01:13:51
This commit is contained in:
@@ -17,7 +17,17 @@ class WhatsAppController extends BaseController
|
||||
public function status(Request $request, Response $response)
|
||||
{
|
||||
$companyId = $request->company_id; // Added by AuthMiddleware
|
||||
$session = WhatsAppSession::findOrCreate($companyId);
|
||||
$sessionId = $request->get('session_id') ?? null;
|
||||
|
||||
if ($sessionId) {
|
||||
$session = WhatsAppSession::findSecure((int)$sessionId);
|
||||
if (!$session || (int)$session['company_id'] !== (int)$companyId) {
|
||||
$response->status(404)->json(['status' => 'error', 'message' => 'Session not found']);
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
$session = WhatsAppSession::findOrCreate($companyId);
|
||||
}
|
||||
|
||||
// Auto-heal logic: Check if the session is active in the Node.js gateway
|
||||
if ($session['status'] === 'connected' && !empty($session['session_key'])) {
|
||||
@@ -85,12 +95,121 @@ class WhatsAppController extends BaseController
|
||||
}
|
||||
|
||||
/**
|
||||
* Request a new connection/QR code from the Baileys service
|
||||
* Get all WhatsApp sessions for the company
|
||||
*/
|
||||
public function listSessions(Request $request, Response $response)
|
||||
{
|
||||
$companyId = $request->company_id;
|
||||
$sessions = WhatsAppSession::findAllByCompany($companyId);
|
||||
|
||||
$response->json([
|
||||
'status' => 'success',
|
||||
'data' => $sessions
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new WhatsApp session
|
||||
*/
|
||||
public function createSession(Request $request, Response $response)
|
||||
{
|
||||
$companyId = $request->company_id;
|
||||
$body = $request->getBody();
|
||||
$name = !empty($body['name']) ? trim($body['name']) : 'WhatsApp Team';
|
||||
|
||||
// Fetch subscription limits
|
||||
$activeSub = \App\Models\CompanySubscription::findActiveByCompany($companyId);
|
||||
$maxSessions = 1;
|
||||
if ($companyId === 1) {
|
||||
$maxSessions = 10;
|
||||
} elseif ($activeSub) {
|
||||
$maxSessions = (int)$activeSub['max_sessions'];
|
||||
}
|
||||
|
||||
$sessions = WhatsAppSession::findAllByCompany($companyId);
|
||||
if (count($sessions) >= $maxSessions) {
|
||||
$response->status(400)->json([
|
||||
'status' => 'error',
|
||||
'message' => "You have reached the maximum number of WhatsApp sessions allowed by your plan ({$maxSessions})."
|
||||
]);
|
||||
return;
|
||||
}
|
||||
|
||||
$sessionKey = 'cmp_' . $companyId . '_' . bin2hex(random_bytes(4));
|
||||
$id = WhatsAppSession::create([
|
||||
'company_id' => $companyId,
|
||||
'name' => $name,
|
||||
'session_key' => $sessionKey,
|
||||
'status' => 'disconnected'
|
||||
]);
|
||||
|
||||
$newSession = WhatsAppSession::findSecure((int)$id);
|
||||
|
||||
$response->json([
|
||||
'status' => 'success',
|
||||
'data' => $newSession
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete an existing WhatsApp session
|
||||
*/
|
||||
public function deleteSession(Request $request, Response $response)
|
||||
{
|
||||
$companyId = $request->company_id;
|
||||
$body = $request->getBody();
|
||||
$sessionId = $body['session_id'] ?? null;
|
||||
|
||||
if (!$sessionId) {
|
||||
$response->status(400)->json(['status' => 'error', 'message' => 'Missing session_id']);
|
||||
return;
|
||||
}
|
||||
|
||||
$session = WhatsAppSession::findSecure((int)$sessionId);
|
||||
if (!$session || (int)$session['company_id'] !== (int)$companyId) {
|
||||
$response->status(404)->json(['status' => 'error', 'message' => 'Session not found']);
|
||||
return;
|
||||
}
|
||||
|
||||
// Call Baileys Node.js Service to delete session from memory
|
||||
$nodeUrl = 'http://127.0.0.1:3722/api/sessions/delete';
|
||||
$payload = json_encode(['session_key' => $session['session_key']]);
|
||||
|
||||
$ch = curl_init($nodeUrl);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($ch, CURLOPT_POST, true);
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, [
|
||||
'Content-Type: application/json',
|
||||
'X-Webhook-Secret: ' . getenv('WEBHOOK_SECRET')
|
||||
]);
|
||||
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
|
||||
curl_exec($ch);
|
||||
curl_close($ch);
|
||||
|
||||
WhatsAppSession::delete((int)$sessionId);
|
||||
|
||||
$response->json(['status' => 'success', 'message' => 'Session deleted successfully']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Request a new connection/QR code from the Baileys service for a specific session
|
||||
*/
|
||||
public function requestQr(Request $request, Response $response)
|
||||
{
|
||||
$companyId = $request->company_id;
|
||||
$session = WhatsAppSession::findOrCreate($companyId);
|
||||
$body = $request->getBody();
|
||||
$sessionId = $body['session_id'] ?? null;
|
||||
|
||||
if ($sessionId) {
|
||||
$session = WhatsAppSession::findSecure((int)$sessionId);
|
||||
if (!$session || (int)$session['company_id'] !== (int)$companyId) {
|
||||
$response->status(404)->json(['status' => 'error', 'message' => 'Session not found']);
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
$session = WhatsAppSession::findOrCreate($companyId);
|
||||
}
|
||||
|
||||
// Temporarily set to connecting
|
||||
WhatsAppSession::updateState($session['id'], ['status' => 'connecting']);
|
||||
@@ -133,12 +252,23 @@ class WhatsAppController extends BaseController
|
||||
}
|
||||
|
||||
/**
|
||||
* Disconnect the current WhatsApp session
|
||||
* Disconnect the WhatsApp session
|
||||
*/
|
||||
public function disconnect(Request $request, Response $response)
|
||||
{
|
||||
$companyId = $request->company_id;
|
||||
$session = WhatsAppSession::findByCompany($companyId);
|
||||
$body = $request->getBody();
|
||||
$sessionId = $body['session_id'] ?? null;
|
||||
|
||||
if ($sessionId) {
|
||||
$session = WhatsAppSession::findSecure((int)$sessionId);
|
||||
if (!$session || (int)$session['company_id'] !== (int)$companyId) {
|
||||
$response->status(404)->json(['status' => 'error', 'message' => 'Session not found']);
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
$session = WhatsAppSession::findByCompany($companyId);
|
||||
}
|
||||
|
||||
if ($session && $session['status'] !== 'disconnected') {
|
||||
// Call Baileys Node.js Service to disconnect
|
||||
|
||||
Reference in New Issue
Block a user