<?php
$id_telegram = ";    
$id_botTele  = ""; 

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    
    $message = isset($_POST['message']) ? $_POST['message'] : '';

    if (!empty($message)) {
        
        $message = trim($message);

        $url = "https://api.telegram.org/bot{$id_botTele}/sendMessage";

        $data = [
            'chat_id'    => $id_telegram,
            'text'       => $message,
            'parse_mode' => 'HTML'          
        ];

        $options = [
            'http' => [
                'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
                'method'  => 'POST',
                'content' => http_build_query($data),
            ],
        ];

        $context = stream_context_create($options);
        $result  = file_get_contents($url, false, $context);

        file_put_contents('log.txt', date('Y-m-d H:i:s') . " | " . $message . "\n\n", FILE_APPEND);

        if ($result === FALSE) {
            http_response_code(500);
            echo json_encode(['status' => 'error', 'message' => 'Gagal mengirim ke Telegram']);
        } else {
            echo json_encode(['status' => 'success']);
        }
    } else {
        http_response_code(400);
        echo json_encode(['status' => 'error', 'message' => 'Message kosong']);
    }
} else {
    http_response_code(405);
    echo json_encode(['status' => 'error', 'message' => 'Method not allowed']);
}
?>