71 lines
2.6 KiB
PHP
71 lines
2.6 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
class CompanyApiClient
|
|
{
|
|
public static function forwardMessage(array $company, array $messageData): array
|
|
{
|
|
$endpoint = rtrim($company['api_base_url'], '/') . '/webhook/incoming';
|
|
|
|
$payload = [
|
|
'company_id' => (int)$company['id'],
|
|
'from' => $messageData['from'] ?? '',
|
|
'name' => $messageData['name'] ?? '',
|
|
'message_id' => $messageData['message_id'] ?? '',
|
|
'type' => $messageData['type'] ?? 'unknown',
|
|
'content' => $messageData['content'] ?? '',
|
|
'media_id' => $messageData['media_id'] ?? null,
|
|
'timestamp' => $messageData['timestamp'] ?? time(),
|
|
'phone_number_id' => $messageData['phone_number_id'] ?? '',
|
|
'display_phone' => $messageData['display_phone'] ?? '',
|
|
'raw_payload' => $messageData['raw_payload'] ?? null,
|
|
];
|
|
|
|
return self::post($endpoint, $company['api_key'] ?? '', $payload);
|
|
}
|
|
|
|
public static function forwardStatus(array $company, array $statusData): array
|
|
{
|
|
$endpoint = rtrim($company['api_base_url'], '/') . '/webhook/status';
|
|
|
|
$payload = [
|
|
'company_id' => (int)$company['id'],
|
|
'message_id' => $statusData['message_id'] ?? '',
|
|
'status' => $statusData['status'] ?? '',
|
|
'recipient' => $statusData['recipient'] ?? '',
|
|
'timestamp' => $statusData['timestamp'] ?? time(),
|
|
'errors' => $statusData['errors'] ?? null,
|
|
];
|
|
|
|
return self::post($endpoint, $company['api_key'] ?? '', $payload);
|
|
}
|
|
|
|
private static function post(string $url, string $apiKey, array $payload): array
|
|
{
|
|
$ch = curl_init($url);
|
|
curl_setopt_array($ch, [
|
|
CURLOPT_POST => true,
|
|
CURLOPT_POSTFIELDS => json_encode($payload),
|
|
CURLOPT_HTTPHEADER => [
|
|
'Content-Type: application/json',
|
|
'X-API-Key: ' . $apiKey,
|
|
'User-Agent: bot-palmas360/1.0',
|
|
],
|
|
CURLOPT_RETURNTRANSFER => true,
|
|
CURLOPT_TIMEOUT => 15,
|
|
]);
|
|
|
|
$response = curl_exec($ch);
|
|
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
$error = curl_error($ch);
|
|
curl_close($ch);
|
|
|
|
return [
|
|
'http_code' => $httpCode,
|
|
'response' => $response !== false ? json_decode($response, true) : null,
|
|
'error' => $error ?: null,
|
|
'success' => $httpCode >= 200 && $httpCode < 300,
|
|
];
|
|
}
|
|
}
|