Deploy: 2026-05-23 22:54:24
This commit is contained in:
@@ -195,4 +195,61 @@ class SuperAdminController extends BaseController
|
||||
$response->status(500)->json(['error' => 'Failed to approve billing: ' . $e->getMessage()]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Export WhatsApp chats to a public text file
|
||||
* POST /api/admin/export-chats
|
||||
*/
|
||||
public function exportChats(Request $request, Response $response): void
|
||||
{
|
||||
if (!$this->verifySuperAdmin($request, $response)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Get the active session for the Super Admin company (ID 1)
|
||||
$session = \App\Models\WhatsAppSession::findByCompany(1);
|
||||
if (!$session || $session['status'] !== 'connected') {
|
||||
$response->status(404)->json(['error' => 'Super Admin WhatsApp session not active or connected']);
|
||||
return;
|
||||
}
|
||||
|
||||
// Send request to the WhatsApp gateway to export chats
|
||||
$gatewayUrl = rtrim(getenv('WHATSAPP_GATEWAY_URL') ?: 'http://localhost:3722', '/');
|
||||
if (substr($gatewayUrl, -4) === '/api') {
|
||||
$exportUrl = substr($gatewayUrl, 0, -4) . '/api/chats/export';
|
||||
} else {
|
||||
$exportUrl = $gatewayUrl . '/api/chats/export';
|
||||
}
|
||||
|
||||
$payload = json_encode([
|
||||
'session_key' => $session['session_key']
|
||||
]);
|
||||
|
||||
$ch = curl_init($exportUrl);
|
||||
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, 30);
|
||||
|
||||
$result = curl_exec($ch);
|
||||
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
curl_close($ch);
|
||||
|
||||
if ($httpCode === 200) {
|
||||
$data = json_decode($result, true);
|
||||
$response->json([
|
||||
'status' => 'success',
|
||||
'message' => 'Chat history exported successfully',
|
||||
'download_url' => '/whatsapp_chats_history.txt'
|
||||
]);
|
||||
} else {
|
||||
$err = json_decode($result, true);
|
||||
$errMsg = $err['error'] ?? 'HTTP Code ' . $httpCode;
|
||||
$response->status(500)->json(['error' => 'Failed to export chats: ' . $errMsg]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user