<?php
$token = '100033216:AAFpUx-vJZFa_8cFuw6RBmxGjqcN1spf3bU';

// Define the API endpoint URL
$api_url = "https://api.telegram.org/bot$token/";

// Function to send an API request
function sendTelegramRequest($method, $parameters = []) {
    global $api_url;
    $url = $api_url . $method;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, count($parameters));
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($parameters));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
        'Content-Type: application/json',
    ]);
    $response = curl_exec($ch);
    curl_close($ch);
    return json_decode($response, true);
}

// Function to list groups that the bot has joined
function listGroups() {
    $result = sendTelegramRequest('getUpdates');
    $chat_ids = [];
    if (!empty($result['result'])) {
        foreach ($result['result'] as $update) {
            $chat = $update['message']['chat'];
            $chat_id = $chat['id'];
            $chat_type = $chat['type'];
            if (in_array($chat_type, ['group', 'supergroup', 'channel'])) {
                $chat_ids[] = [
                    'id' => $chat_id,
                    'title' => $chat['title'],
                ];
            }
        }
    }
    return $chat_ids;
}

// List the groups and channels
$groups = listGroups();

echo "Groups and Channels the Bot has Joined:\n";
foreach ($groups as $group) {
    echo "{$group['title']} - ID: {$group['id']}\n";
}
?>
