<?php
// ═══════════════════════════════════════════════════════════════
// config/telegram_notify.php — pakai cURL (bukan file_get_contents)
// ═══════════════════════════════════════════════════════════════

if (!defined('TELEGRAM_BOT_TOKEN')) {
    $__token = '';
    if (isset($pdo)) {
        try {
            $__r = $pdo->query("SELECT nilai FROM pengaturan WHERE kunci='telegram_bot_token' LIMIT 1")->fetch();
            $__token = $__r['nilai'] ?? '';
        } catch (Exception $e) {}
    }
    define('TELEGRAM_BOT_TOKEN', $__token);
}

/**
 * Kirim pesan ke Telegram via cURL
 */
function kirimTelegram(string $chatId, string $pesan): bool {
    $token = TELEGRAM_BOT_TOKEN;
    if (empty($token) || empty($chatId)) return false;

    $url  = "https://api.telegram.org/bot{$token}/sendMessage";
    $data = [
        'chat_id'    => $chatId,
        'text'       => $pesan,
        'parse_mode' => 'Markdown',
    ];

    $ch = curl_init($url);
    curl_setopt_array($ch, [
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_POST           => true,
        CURLOPT_POSTFIELDS     => http_build_query($data),
        CURLOPT_TIMEOUT        => 10,
        CURLOPT_SSL_VERIFYPEER => false,
        CURLOPT_HTTPHEADER     => ['Content-Type: application/x-www-form-urlencoded'],
    ]);
    $result = curl_exec($ch);
    curl_close($ch);

    if (!$result) return false;
    $json = json_decode($result, true);
    return $json['ok'] ?? false;
}