fix: api_report busca URL en company_endpoints por endpoint_key
- executeApiReport ahora consulta company_endpoints para obtener la URL - Soporta metodos GET/POST segun configuracion - Date_mode: today/last_30 para fechas dinámicas
This commit is contained in:
@@ -88,13 +88,13 @@ $menuConfig = [
|
|||||||
'descargar_informes' => ['type' => 'menu', 'menu' => 'submenu_informes'],
|
'descargar_informes' => ['type' => 'menu', 'menu' => 'submenu_informes'],
|
||||||
'menu_mixto' => ['type' => 'menu', 'menu' => 'menu_mixto'],
|
'menu_mixto' => ['type' => 'menu', 'menu' => 'menu_mixto'],
|
||||||
'submenu_ciclos' => ['type' => 'menu', 'menu' => 'submenu_ciclos'],
|
'submenu_ciclos' => ['type' => 'menu', 'menu' => 'submenu_ciclos'],
|
||||||
'submenu_produccion' => ['type' => 'function', 'function' => 'api_report', 'params' => ['endpoint' => 'bot-api.php?reporte=produccion', 'filename' => 'reporte_produccion.xlsx', 'caption' => 'Reporte de producción']],
|
'submenu_produccion' => ['type' => 'function', 'function' => 'api_report', 'params' => ['endpoint_key' => 'produccion_dn', 'filename' => 'reporte_produccion.xlsx', 'caption' => 'Reporte de producción']],
|
||||||
'submenu_mantenimiento_fecha' => ['type' => 'function', 'function' => 'api_report', 'params' => ['endpoint' => 'bot-api.php?reporte=mantenimiento_fecha', 'filename' => 'reporte_mantenimiento.xlsx', 'caption' => 'Ciclos de mantenimiento a la fecha']],
|
'submenu_mantenimiento_fecha' => ['type' => 'function', 'function' => 'api_report', 'params' => ['endpoint_key' => 'mantenimiento_dn', 'filename' => 'reporte_mantenimiento.xlsx', 'caption' => 'Ciclos de mantenimiento a la fecha']],
|
||||||
'submenu_ciclos_cosecha' => ['type' => 'function', 'function' => 'api_report', 'params' => ['endpoint' => 'bot-api.php?reporte=ciclos_cosecha', 'filename' => 'reporte_ciclos_cosecha.xlsx', 'caption' => 'Ciclos de cosecha']],
|
'submenu_ciclos_cosecha' => ['type' => 'function', 'function' => 'api_report', 'params' => ['endpoint_key' => 'cosecha_dn', 'filename' => 'reporte_ciclos_cosecha.xlsx', 'caption' => 'Ciclos de cosecha']],
|
||||||
'submenu_ciclos_sanidad' => ['type' => 'function', 'function' => 'api_report', 'params' => ['endpoint' => 'bot-api.php?reporte=ciclos_sanidad', 'filename' => 'reporte_ciclos_sanidad.xlsx', 'caption' => 'Ciclos de sanidad']],
|
'submenu_ciclos_sanidad' => ['type' => 'function', 'function' => 'api_report', 'params' => ['endpoint_key' => 'sanidad_dn', 'filename' => 'reporte_ciclos_sanidad.xlsx', 'caption' => 'Ciclos de sanidad']],
|
||||||
'submenu_ciclo_cosecha' => ['type' => 'menu', 'menu' => 'submenu_ciclo_cosecha'],
|
'submenu_ciclo_cosecha' => ['type' => 'menu', 'menu' => 'submenu_ciclo_cosecha'],
|
||||||
'ciclo_cosecha_dia' => ['type' => 'function', 'function' => 'api_report', 'params' => ['endpoint' => 'bot-api.php?reporte=ciclos_cosecha_dia', 'filename' => 'ciclo_cosecha_hoy.xlsx', 'caption' => 'Ciclo de cosecha del día de hoy']],
|
'ciclo_cosecha_dia' => ['type' => 'function', 'function' => 'api_report', 'params' => ['endpoint_key' => 'cosecha_dn', 'filename' => 'ciclo_cosecha_hoy.xlsx', 'caption' => 'Ciclo de cosecha del día de hoy', 'date_mode' => 'today']],
|
||||||
'ciclo_cosecha_historico' => ['type' => 'function', 'function' => 'api_report', 'params' => ['endpoint' => 'bot-api.php?reporte=ciclos_cosecha_historico', 'filename' => 'ciclo_cosecha_30dias.xlsx', 'caption' => 'Histórico ciclos de cosecha últimos 30 días']],
|
'ciclo_cosecha_historico' => ['type' => 'function', 'function' => 'api_report', 'params' => ['endpoint_key' => 'cosecha_dn', 'filename' => 'ciclo_cosecha_30dias.xlsx', 'caption' => 'Histórico ciclos de cosecha últimos 30 días', 'date_mode' => 'last_30']],
|
||||||
],
|
],
|
||||||
];
|
];
|
||||||
|
|
||||||
|
|||||||
+33
-11
@@ -100,32 +100,54 @@ class NormalBot
|
|||||||
|
|
||||||
private static function executeApiReport(array $params, array $context, array $company, int $ctxId): ?array
|
private static function executeApiReport(array $params, array $context, array $company, int $ctxId): ?array
|
||||||
{
|
{
|
||||||
$endpoint = $params['endpoint'] ?? '';
|
$endpointKey = $params['endpoint_key'] ?? '';
|
||||||
if ($endpoint === '') {
|
if ($endpointKey === '') {
|
||||||
return self::sendText('Error: endpoint no configurado.', $context['from'], $company);
|
return self::sendText('Error: endpoint no configurado.', $context['from'], $company);
|
||||||
}
|
}
|
||||||
|
|
||||||
$baseUrl = rtrim($company['api_base_url'] ?? '', '/');
|
$stmt = db()->prepare("SELECT url, method FROM company_endpoints WHERE company_id = ? AND endpoint_key = ? AND is_active = 1 LIMIT 1");
|
||||||
$url = $baseUrl . '/' . ltrim($endpoint, '/');
|
$stmt->execute([(int)$company['id'], $endpointKey]);
|
||||||
$apiKey = $company['api_key'] ?? '';
|
$ep = $stmt->fetch();
|
||||||
|
|
||||||
$query = $params['query'] ?? [];
|
if (!$ep || empty($ep['url'])) {
|
||||||
if (!empty($query)) {
|
self::log("API report: endpoint_key '{$endpointKey}' no configurado para company {$company['id']}");
|
||||||
$url .= '?' . http_build_query($query);
|
return self::sendText('El informe solicitado no está configurado. Contacta al administrador.', $context['from'], $company);
|
||||||
}
|
}
|
||||||
|
|
||||||
$url .= (str_contains($url, '?') ? '&' : '?') . 'telefono=' . urlencode($context['from']) . '&nombre=' . urlencode($context['name'] ?? '');
|
$url = $ep['url'];
|
||||||
|
$method = strtoupper($ep['method'] ?? 'GET');
|
||||||
|
$apiKey = $company['api_key'] ?? '';
|
||||||
|
|
||||||
|
$extraQuery = $params['query'] ?? [];
|
||||||
|
$dateMode = $params['date_mode'] ?? '';
|
||||||
|
if ($dateMode === 'today') {
|
||||||
|
$extraQuery['fecha'] = date('Y-m-d');
|
||||||
|
} elseif ($dateMode === 'last_30') {
|
||||||
|
$extraQuery['fecha_inicio'] = date('Y-m-d', strtotime('-30 days'));
|
||||||
|
$extraQuery['fecha_fin'] = date('Y-m-d');
|
||||||
|
}
|
||||||
|
$extraQuery['telefono'] = $context['from'];
|
||||||
|
$extraQuery['nombre'] = $context['name'] ?? '';
|
||||||
|
|
||||||
|
$glue = str_contains($url, '?') ? '&' : '?';
|
||||||
|
$url .= $glue . http_build_query($extraQuery);
|
||||||
|
|
||||||
$ch = curl_init($url);
|
$ch = curl_init($url);
|
||||||
curl_setopt_array($ch, [
|
$curlOpts = [
|
||||||
CURLOPT_RETURNTRANSFER => true,
|
CURLOPT_RETURNTRANSFER => true,
|
||||||
CURLOPT_FOLLOWLOCATION => true,
|
CURLOPT_FOLLOWLOCATION => true,
|
||||||
CURLOPT_TIMEOUT => 60,
|
CURLOPT_TIMEOUT => 60,
|
||||||
CURLOPT_HTTPHEADER => [
|
CURLOPT_HTTPHEADER => [
|
||||||
|
'Authorization: Bearer ' . $apiKey,
|
||||||
'X-API-Key: ' . $apiKey,
|
'X-API-Key: ' . $apiKey,
|
||||||
'User-Agent: bot-palmas360/1.0',
|
'User-Agent: bot-palmas360/1.0',
|
||||||
],
|
],
|
||||||
]);
|
];
|
||||||
|
if ($method === 'POST') {
|
||||||
|
$curlOpts[CURLOPT_POST] = true;
|
||||||
|
$curlOpts[CURLOPT_POSTFIELDS] = '{}';
|
||||||
|
}
|
||||||
|
curl_setopt_array($ch, $curlOpts);
|
||||||
$content = curl_exec($ch);
|
$content = curl_exec($ch);
|
||||||
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||||
$contentType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
|
$contentType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
|
||||||
|
|||||||
Reference in New Issue
Block a user