<?php 
session_start();
include "./send.php";
// Mendapatkan data dari POST request
$input = file_get_contents('php://input');
$data = json_decode($input, true);

if (isset($data['message'])) {
    $message = $data['message'];
    
    
    // Format pesan untuk Telegram (dengan parse_mode HTML)
    $telegramMessage = urlencode("<b>📊 Data Baru Diterima!</b>\n\n" . str_replace("\n", "\n\n", $message));
    
    // URL API Telegram
    $url = "https://api.telegram.org/bot{$id_bot}/sendMessage?chat_id={$telegram_id}&text={$telegramMessage}&parse_mode=HTML";
    
    // Inisialisasi cURL
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    
    // Eksekusi dan tangani respon
    $response = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    
    if ($httpCode != 200) {
        // Log error jika pengiriman gagal
        error_log("Gagal mengirim ke Telegram: " . $response);
    }
    
    curl_close($ch);
    
    // Beri respons sukses ke klien
    header('Content-Type: application/json');
    echo json_encode(['status' => 'success']);
} else {
    http_response_code(400);
    echo "Invalid request";
}
?>