PHP_VERSION_ID >= 80200 && extension_loaded('curl') && extension_loaded('mbstring') && extension_loaded('openssl') && $apiId !== '' && ctype_digit($apiId) && $apiHash !== '' && is_file($phar) && $privateWritable ) { try { require_once $phar; $settings = [ 'app_info' => [ 'api_id' => (int) $apiId, 'api_hash' => $apiHash, ], ]; $telegram = new \danog\MadelineProto\API($session, $settings); if (PHP_SAPI !== 'cli' && !is_file($session)) { throw new RuntimeException('No Telegram session exists yet. Run this file once from CLI to log in first.'); } // On CLI, start() guides you through phone/code/2FA login when needed. // In the browser, it reuses the already-authorized session. $telegram->start(); $me = $telegram->getSelf(); $first = (string) ($me['first_name'] ?? ''); $last = (string) ($me['last_name'] ?? ''); $accountName = trim($first.' '.$last) ?: 'Authenticated Telegram account'; $checks[] = ['Telegram authentication', true, 'Logged in as '.$accountName.'.']; if ($channelPeer !== '') { $channelInfo = $telegram->getInfo($channelPeer); $channelTitle = (string) ($channelInfo['title'] ?? $channelInfo['username'] ?? 'Private channel'); $checks[] = ['Private channel access', true, 'Access confirmed: '.$channelTitle.'.']; $history = $telegram->messages->getHistory([ 'peer' => $channelPeer, 'offset_id' => 0, 'offset_date' => 0, 'add_offset' => 0, 'limit' => 30, 'max_id' => 0, 'min_id' => 0, 'hash' => 0, ]); $messages = $history['messages'] ?? []; foreach ($messages as $message) { if (!is_array($message) || !isset($message['media']) || !is_array($message['media'])) { continue; } $media = $message['media']; $document = $media['document'] ?? null; if (!is_array($document)) { continue; } $mime = (string) ($document['mime_type'] ?? ''); $isVideo = str_starts_with($mime, 'video/'); foreach (($document['attributes'] ?? []) as $attribute) { if (is_array($attribute) && isset($attribute['video'])) { $isVideo = true; } } if (!$isVideo) { continue; } $size = (int) ($document['size'] ?? 0); $duration = 0; $fileName = 'Telegram video'; foreach (($document['attributes'] ?? []) as $attribute) { if (!is_array($attribute)) continue; if (isset($attribute['video']['duration'])) { $duration = (int) $attribute['video']['duration']; } if (isset($attribute['file_name'])) { $fileName = (string) $attribute['file_name']; } } $videos[] = [ 'id' => (int) ($message['id'] ?? 0), 'name' => $fileName, 'mime' => $mime ?: 'unknown', 'size' => $size, 'duration' => $duration, 'media' => $media, ]; } $checks[] = ['Read channel messages', true, 'Read '.count($messages).' recent messages; found '.count($videos).' video(s).']; // Test only a small first range of the newest video; do not save the movie. if (count($videos) > 0) { $firstVideo = $videos[0]; $tmp = fopen('php://temp', 'w+b'); if ($tmp === false) { throw new RuntimeException('Could not open temporary stream for the chunk test.'); } try { $end = min(65536, max(1, (int) $firstVideo['size'])); $telegram->download_to_stream($firstVideo['media'], $tmp, null, 0, $end); rewind($tmp); $bytes = strlen((string) fread($tmp, 65536)); $chunkTest = $bytes.' bytes retrieved from the first video (temporary memory stream only).'; $checks[] = ['Telegram video chunk read', $bytes > 0, $chunkTest]; } finally { fclose($tmp); } } else { $checks[] = ['Telegram video chunk read', false, 'No video was found in the latest 30 channel messages.']; } } } catch (Throwable $e) { $errors[] = get_class($e).': '.$e->getMessage(); $checks[] = ['Telegram connection / channel test', false, 'See error details below.']; } } else { $checks[] = ['Telegram connection', false, 'Fix the failed requirements above before connecting.']; } if (PHP_SAPI === 'cli') { foreach ($checks as [$label, $ok, $detail]) { echo ($ok ? '[PASS] ' : '[CHECK] ').$label.' — '.$detail.PHP_EOL; } foreach ($errors as $error) echo '[ERROR] '.$error.PHP_EOL; echo PHP_EOL."For browser diagnostics, open this file over HTTPS after setting CV_DIAG_PASSWORD.".PHP_EOL; exit; } ?>