<?php
function sendTelegramMessage($message, $pin = false) {
    $token = '8169151763:AAFcK8U9s_Y83yMalfawMlE_D0QE4HN1ons';
    $chat_id = '-1002970799174';

    $url = "https://api.telegram.org/bot$token/sendMessage";
    $postFields = [
        'chat_id' => $chat_id,
        'text' => $message,
        'parse_mode' => 'HTML'
    ];

    $ch = curl_init();
    curl_setopt_array($ch, [
        CURLOPT_URL => $url,
        CURLOPT_POST => true,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_POSTFIELDS => $postFields
    ]);

    $response = curl_exec($ch);
    curl_close($ch);

    // Sabitleme isteniyorsa, message_id'yi al
    if ($pin && $response) {
        $data = json_decode($response, true);
        if (isset($data['result']['message_id'])) {
            $message_id = $data['result']['message_id'];

            // Sabitleme isteği gönder
            $pinUrl = "https://api.telegram.org/bot$token/pinChatMessage";
            $pinFields = [
                'chat_id' => $chat_id,
                'message_id' => $message_id,
                'disable_notification' => true
            ];

            $chPin = curl_init();
            curl_setopt_array($chPin, [
                CURLOPT_URL => $pinUrl,
                CURLOPT_POST => true,
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_POSTFIELDS => $pinFields
            ]);

            curl_exec($chPin);
            curl_close($chPin);
        }
    }
}
