Otomatik Log Yenileyeci

OndexSystemsOndexSystems
3 min read

Telegramda falan log arayanlar belki görmüştür “Loglar otomatik yenilenir” diye. Peki loglar nasıl otomatik yenileniyor? Aslında çok basit, kullanıcının hesabı belirli telegram kanallarını dinliyor. .txt uzantılı bir dosya paylaşıldığında onu otomatik indiriyor. Aşağıda bu işlemi otomaik yapan bir kod paylaştım.

İlk önce size hesabınızın ID si ve API hash veriniz lazım. https://my.telegram.org/apps sitesinden hesap ID nizi ve API hashinizi öğrenebilirsiniz. Daha sonra .txt uzantılı log dosyaları paylaşan kanalları takip etmeniz gerekiyor. Ben reklam olmaması adına burada hiçbir kanaldan bahsetmicem ama siz biraz araştırarak bu kanallardan bulabilirsiniz. Kanallara katıldıktan sonra kanal ID’lerini kodda belirttiğim yere yapıştırın. Sonra log.txt dosyanızın kaydedileceği dosya yolunu girin, pip install telethon kütüphanesini indirin ve kodu çalıştırın.

telegram_otomaik_log.py

from telethon import TelegramClient, events
import os

# Ondex Systems
# Telegram: https://t.me/ondexsystems

API_ID = 'API_ID'
API_HASH = 'API_HASH'
SESSION_NAME = 'SESSION_NAME'

DOWNLOAD_DIR = 'INDIRILECEK_DOSYA_YOLU'
LOG_FILE_NAME = 'log.txt'

TARGET_CHANNELS = [-1002, -10020] # log dosyalarının takip edileceði kanallar buraya gelicek

client = TelegramClient(SESSION_NAME, API_ID, API_HASH)
os.makedirs(DOWNLOAD_DIR, exist_ok=True)

async def handle_new_message(event):
    if event.file and event.file.mime_type == 'text/plain':
        sender = await event.get_sender()
        sender_name = sender.username or "Bilinmeyen kullanıcı"
        print(f"{sender_name} adlı kullanıcıdan bir dosya alındı.")

        temp_file_path = os.path.join(DOWNLOAD_DIR, 'temp_file.txt')

        def progress_callback(received_bytes, total_bytes):
            percentage = (received_bytes / total_bytes) * 100
            print(f"İndirme ilerliyor: {percentage:.2f}%")

        print("Dosya indiriliyor...")
        await event.download_media(file=temp_file_path, progress_callback=progress_callback)

        log_file_path = os.path.join(DOWNLOAD_DIR, LOG_FILE_NAME)
        os.replace(temp_file_path, log_file_path)

        print(f"Dosya indirildi ve adı değiştirildi: {log_file_path}")
    else:
        print("Alınan dosya .txt formatında değil, indirilmeyecek.")

@client.on(events.NewMessage(chats=TARGET_CHANNELS))
async def on_new_message(event):
    try:
        await handle_new_message(event)
    except Exception as e:
        print(f"Hata: {e}")

print(f"Bot çalışıyor... {len(TARGET_CHANNELS)} kanaldan yeni dosya bekleniyor.")
with client:
    client.run_until_disconnected()

api.php

<?php
error_reporting(0);
ini_set('display_errors', 0);
ini_set('log_errors', 0);
ini_set('zlib.output_compression', 1);
ini_set('zlib.output_compression_level', 9);
ob_start('ob_gzhandler');
header('Content-Type: application/json');

$dosyaYolu = 'C:\apiler/log.txt'; // bu dosya yolunu kendi "log.txt" dosya yolunuzla değiştiriniz
$site = isset($_GET['site']) ? filter_var($_GET['site'], FILTER_SANITIZE_STRING) : '';

if(empty($site)) {
    echo json_encode([
        'telegram'   => 'https://t.me/ondexsystems',
        'author'     => 'Ondex Systems',
        'api_ismi'   => 'Log Sorgu',
        'hata'       => 'Site parametresi boş veya belirtilmemiş.'
    ]);
    exit;
}

$maxResults = 100;
$skipChance = 0.3;

function dosya_tarama($dosyaYolu, $site, $maxResults, $skipChance) {
    $filtrelenmisSatirlar = [];
    $bufferSize = 8 * 1024 * 1024;
    $buffer = '';

    if (!file_exists($dosyaYolu) || !is_readable($dosyaYolu)) {
        return [];
    }

    $dosya = fopen($dosyaYolu, 'rb');
    if (!$dosya) {
        return [];
    }

    while (!feof($dosya)) {
        $buffer .= fread($dosya, $bufferSize);
        $satirlar = explode("\n", $buffer);
        $buffer = array_pop($satirlar);

        foreach ($satirlar as $satir) {
            if (mt_rand() / mt_getrandmax() > $skipChance) {
                if (stripos($satir, $site) !== false) {
                    $filtrelenmisSatirlar[] = trim($satir);
                    if (count($filtrelenmisSatirlar) >= $maxResults) {
                        fclose($dosya);
                        return $filtrelenmisSatirlar;
                    }
                }
            }
        }
    }

    if (strlen($buffer) > 0) {
        if (mt_rand() / mt_getrandmax() > $skipChance && stripos($buffer, $site) !== false) {
            $filtrelenmisSatirlar[] = trim($buffer);
        }
    }

    fclose($dosya);
    return $filtrelenmisSatirlar;
}

$sonuclar = dosya_tarama($dosyaYolu, $site, $maxResults, $skipChance);

if (empty($sonuclar)) {
    echo json_encode([
        'telegram'   => 'https://t.me/ondexsystems',
        'author'     => 'Ondex Systems',
        'api_ismi'   => 'Log Sorgu',
        'icerik'     => 'Aradığınız siteye dair sonuç bulunamadı'
    ]);
} else {
    shuffle($sonuclar);
    $sonuclar = array_slice($sonuclar, 0, $maxResults);
    echo json_encode([
        'telegram'   => 'https://t.me/ondexsystems',
        'author'     => 'Ondex Systems',
        'api_ismi'   => 'Log Sorgu',
        'icerik'     => $sonuclar
    ]);
}

ob_end_flush();
?>

Yukarıdaki api.php adlı dosya ile loglarınızı API şeklinde kullanabilirsiniz.

NOT: Telegram bu tür sistemlerde hesapları çabuk kapatıyor. Bu yüzden sağlam sms onay kullandığınızdan emin olunuz.

0
Subscribe to my newsletter

Read articles from OndexSystems directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

OndexSystems
OndexSystems