import json import datetime import time from telethon import client from telethon.sync import TelegramClient from telethon.tl.types import UserStatusRecently, UserStatusOnline, User # self.client.get_participants(group_name, aggressive=True) class PmBot(): def __init__(self, api_id: str, api_hash: str) -> None: self.api_id = api_id self.api_hash = api_hash self.config = json.load(open("config.json", "r", encoding="utf-8")) self.client = "" def change_client(self, session_name: str) -> None: self.client = "" self.client = TelegramClient( "sessions\\" + session_name, self.api_id, self.api_hash) self.client.connect() print(f"[ - ] {session_name} ADLI HESABA GİRİŞ YAPILDI") def get_users(self, group_name :str, eklenecek: str): self.users = [] users_will_add = self.client.get_participants(group_name, aggressive=True) users_added = self.client.get_participants(eklenecek, aggressive=True) usersa = [] white_list = [za.replace("\n", "").lower() for za in open("white_list.txt", "r", encoding="utf-8").readlines()] for user in users_will_add: now = datetime.datetime.now() - datetime.timedelta(hours=24) if user not in users_added and True in [sa.lower() in white_list for sa in f"{user.first_name} {user.last_name}".split(" ")] or "**" in white_list and user not in users_added: print([sa.lower() for sa in f"{user.first_name} {user.last_name}".split(" ")]) if user.status: print(user.status, type(user.status), type(UserStatusRecently())) if type(user.status) == type(UserStatusRecently()): usersa.append( { "id": user.id, "name": user.first_name, "last_name": user.last_name } ) else: print("geçmiş değil") try: if datetime.datetime( user.status.was_online.year, user.status.was_online.month, user.status.was_online.day, user.status.was_online.hour, user.status.was_online.minute, user.status.was_online.second, user.status.was_online.microsecond) > now: # if user not in users_added: usersa.append( { "id": user.id, "name": user.first_name, "last_name": user.last_name } ) except: print("geçmişi yok ") pass else: if type(UserStatusOnline(expires="")) == type(user.status): # if user not in users_added: usersa.append( { "id": user.id, "name": user.first_name, "last_name": user.last_name } ) else: print("online değilü") with open('data.json', 'w', encoding="utf-8") as outfile: json.dump(usersa, outfile) print( f"[ - ] {len(usersa)} adet üye çekildi ;)" ) def send_messages(self, group_name: str, count: int, message: str, sleept: int): users = json.load(open("data.json", "r", encoding="utf-8")) self.client.get_participants(group_name, aggressive=True) for i in users[:count]: try: self.client.send_message( i['id'], message ) print("bir adet mesaj yollandı, toplam", self.counter) self.counter += 1 time.sleep(self.config['send_sleep']) users.pop(0) except: print("mesaj gönderilemedi") time.sleep(self.config['send_sleep']) with open('data.json', 'w', encoding="utf-8") as outfile: json.dump(users, outfile) def delete_messages(self): for i in self.client.iter_dialogs(): if type(i.entity) == User: print("silindi") self.client.delete_dialog(i, revoke=False) time.sleep(self.config['delete_sleep']) bot = PmBot("3226017", "8702e11ddbf002c90b373da426332147") import os all_sess = os.listdir("sessions") print( """ 1- Üye kaydet 2- Üyelere mesaj at 3- Mesajları temizle """ ) tip = input(">> ") if tip == "1": group_name = input("Çekilecek: ") eklenecek = input("Eklenecek: ") bot.change_client(all_sess[0]) bot.get_users(group_name=group_name, eklenecek=eklenecek) if tip == "2": cnt = input("Kaçar tane mesaj atılsın?: ") group_name = input("Çekilecek: ") bot.counter = 1 for i in all_sess: if i.endswith(".session"): bot.change_client(i) bot.send_messages(group_name, int(cnt), open("message.txt", "r", encoding="utf-8").read(), 1) if tip == "3": for i in all_sess: if i.endswith(".session"): bot.change_client(i) bot.delete_messages()