"Reborn" in groups on Telegram (
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import sys | |
import json | |
from functools import partial | |
from pyrogram import Client, Error | |
from pyrogram.client.types.user_and_chats.dialog import Dialog | |
from pyrogram.client.types.user_and_chats.chat import Chat | |
from pyrogram.api.functions.channels import InviteToChannel | |
eprint = partial(print, file=sys.stderr) | |
app = Client("dummy", workers=1) | |
app.start() | |
def print_running_info(app): | |
me = app.get_me() | |
name = me.first_name + " " + me.last_name | |
username = me.username | |
print("REBORN is up and running...", | |
f"for {name} (@{username})", sep="\n") | |
def get_chats(app: Client, types=["supergroup", "group", "channel"]) -> Chat: | |
last_dialogs = None | |
count = 0 | |
total_count = 1 | |
while count < total_count: | |
dialogs = app.get_dialogs(offset_dialogs=last_dialogs, limit=100) | |
total_count = dialogs.total_count | |
for dialog in dialogs.dialogs: | |
count += 1 | |
if dialog.chat.type in types: | |
yield dialog.chat | |
last_dialogs = dialogs | |
def render_chat(chat: Chat): | |
return f'{chat.title}{" (@" + chat.username + ")" if chat.username is not None else ""}'\ | |
f"\nType: {chat.type}" | |
app.chats = partial(get_chats, app=app) | |
def main(): | |
print_running_info(app) | |
try: | |
incarnation = app.resolve_peer(input("New account identity: ")) | |
except Error as e: | |
exit("Failed to locate the new account: {}".format(e)) | |
print("Reborning as: {}".format(incarnation)) | |
failures = [] | |
try: | |
for chat in app.chats(): | |
print(render_chat(chat)) | |
while True: | |
inp = input("Reborn in this group? (y/n): ") | |
if inp.lower() == "y": | |
to_reborn = True | |
break | |
elif inp.lower() == "n": | |
to_reborn = False | |
break | |
else: | |
eprint("Invalid choice") | |
if to_reborn: | |
try: | |
print("Reborning...") | |
app.send(InviteToChannel(app.resolve_peer(chat.id), [incarnation])) | |
except Error as e: | |
eprint("Failed to reborn: {}".format(e)) | |
failures.append(chat) | |
input("Enter to continue") | |
print("") | |
except Error as e: | |
eprint("Failed to get chat lists: {}".format(e)) | |
finally: | |
if failures: | |
print("\n----------Failed for------------") | |
for failure in failures: | |
print(failure) | |
if __name__ == "__main__": | |
main() | |
# Note: | |
# Mutual contacts should be met in case of PEER_FLOOD. | |
# For some groups and all channels, admin privileges are required to invite others. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment