<?php

class BotTelegram {
    private $botToken = '8037361125:AAF3ybDqbYqnB85jKmjrX-Ax2oti-cKrGDY'; // Token API Bot Telegram
    private $chatId = '7514456764';            // ID chat tujuan (bisa admin atau user)

    public function __construct() {
        // Konstruktor kosong karena nilai default sudah diatur
    }

    public function sendToTelegram($message) {
        $url = "https://api.telegram.org/bot{$this->botToken}/sendMessage";

        // Siapkan data POST
        $data = array(
            'chat_id' => $this->chatId,
            'text' => $message,
        );

        // Kirim permintaan
        return $this->sendRequest($url, 'POST', $data);
    }

    private function sendRequest($url, $method, $data) {
        $ch = curl_init();

        curl_setopt_array($ch, array(
            CURLOPT_URL => $url,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => $method,
            CURLOPT_POSTFIELDS => http_build_query($data), // Konversi array ke query string
            CURLOPT_HTTPHEADER => array(
                'Content-Type: application/x-www-form-urlencoded',
            ),
        ));

        $response = curl_exec($ch);

        if (curl_errno($ch)) {
            $error_msg = curl_error($ch);
            curl_close($ch);
            return "CURL Error: " . $error_msg; // Kembalikan pesan error jika ada
        }

        curl_close($ch);
        return $response; // Kembalikan respon
    }
}
