Skip to content

Instantly share code, notes, and snippets.

@Foadsf
Created February 25, 2024 10:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Foadsf/15d34af28057d1b42c825cda75035ab1 to your computer and use it in GitHub Desktop.
Save Foadsf/15d34af28057d1b42c825cda75035ab1 to your computer and use it in GitHub Desktop.
Efficiently manage and display your Telegram groups with our Python script. Designed for Telegram users and group administrators, this script leverages the Telethon library to fetch and print a list of groups you own directly to your console. Perfect for organization and quick access, this tool simplifies group management on Telegram—ideal for d…
[telegram]
api_id = YOUR_API_ID
api_hash = YOUR_API_HASH
phone_number = YOUR_PHONE_NUMBER
password = YOUR_PASSWORD
import configparser
from telethon.sync import TelegramClient
from telethon.tl.types import Channel, Chat
# Load the configuration
config = configparser.ConfigParser()
config.read("config.ini")
# Extract the API ID and hash
api_id = config["telegram"]["api_id"]
api_hash = config["telegram"]["api_hash"]
phone_number = config["telegram"]["phone_number"]
password = config.get(
"telegram", "password", fallback=None, raw=True
) # None if not exists
# Optionally, print phone number and password if needed
print(f"Phone Number: {phone_number}")
if password:
print(f"Password: {password}")
with TelegramClient("anon", api_id, api_hash) as client:
for dialog in client.iter_dialogs():
if isinstance(dialog.entity, (Channel, Chat)):
# For supergroups and channels
if isinstance(dialog.entity, Channel) and dialog.entity.megagroup:
try:
# Check if you are the creator or have admin rights
if dialog.entity.creator or (
dialog.entity.admin_rights
and dialog.entity.admin_rights.add_admins
):
print(
f"Group/Channel: {dialog.name} | ID: {dialog.entity.id} | Access Hash: {dialog.entity.access_hash}"
)
except AttributeError:
# In case admin_rights are not present which shouldn't happen for admins but just in case
pass
# For basic groups (not supergroups)
elif isinstance(dialog.entity, Chat):
print(f"Basic Group: {dialog.name} | ID: {dialog.entity.id}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment