<?php
require_once __DIR__ . '/config/config.php';

$botToken = $_ENV['TELEGRAM_BOT_TOKEN'] ?? '';
$webhookUrl = rtrim($_ENV['APP_URL'], '/') . '/telegram_bot_webhook.php?auth_token=' . urlencode($_ENV['TELEGRAM_WEBHOOK_TOKEN'] ?? '');

if (empty($botToken)) {
    die("Error: No se ha configurado el TELEGRAM_BOT_TOKEN en el archivo .env");
}

$apiUrl = "https://api.telegram.org/bot{$botToken}/setWebhook?url=" . urlencode($webhookUrl);

$ch = curl_init($apiUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);

header('Content-Type: application/json');
echo $response;