如何获取已加入的Telegram群组名称列表并导出为文件?
Hey there! Let me walk you through a couple of practical ways to retrieve and export the list of Telegram groups you're a member of—whether you prefer a no-code approach or something more flexible for tech-savvy folks.
This is the easiest way if you don't want to mess with code. Note that this works best on the desktop version (mobile doesn't have this direct export feature yet):
- Open the Telegram desktop app and click the hamburger menu (three horizontal lines) in the top-left corner.
- Select Settings from the dropdown menu.
- Navigate to the Advanced tab.
- Scroll down until you find Export Telegram Data and click it.
- In the export window, uncheck all options except Chat list (this keeps the output focused on your groups and chats).
- Choose your preferred export format:
- Plain Text: For a simple, easy-to-read list of group names.
- HTML: If you want a formatted version with additional details like chat types.
- Set your desired save location and click Export.
- Once the export finishes, open the generated file. You'll find all your groups listed—just scan for entries labeled as "Group" or "Supergroup" to filter out private chats.
If you want a more automated, customizable solution, using the Telethon library (a popular Telegram API wrapper for Python) is perfect. It lets you directly fetch only group names and export them cleanly.
Step 1: Prepare your Telegram API credentials
First, you need to get your API ID and API Hash:
- Go to Telegram's official developer platform (search for "Telegram API development tools" to find the page) and log in with your Telegram account.
- Create a new application (fill in the required details—you can use any name/description) and you'll get your unique API ID and API Hash. Keep these safe (don't share them publicly!).
Step 2: Install Telethon
Run this command in your terminal to install the library:
pip install telethon
Step 3: Write the Python script
Create a new file (e.g., export_groups.py) and paste the following code, replacing the placeholder values with your own credentials:
from telethon import TelegramClient # Replace these with your own credentials API_ID = 123456 # Your API ID from the developer platform API_HASH = 'your_unique_api_hash_here' PHONE_NUMBER = '+1234567890' # Your phone number with country code (e.g., +1 for US) async def fetch_and_export_groups(): # Initialize the client (session_name creates a session file to avoid re-logging every time) async with TelegramClient('telegram_session', API_ID, API_HASH) as client: # Log in to your account (first run will prompt for a verification code sent to your Telegram) await client.start(PHONE_NUMBER) group_list = [] # Iterate through all your dialogs (chats/groups/channels) async for dialog in client.iter_dialogs(): # Filter only groups and supergroups (exclude private chats, bots, and channels you're not a member of) if dialog.is_group or dialog.is_channel: group_list.append(dialog.name) # Export the list to a text file with open('my_telegram_groups.txt', 'w', encoding='utf-8') as file: for group in group_list: file.write(f"{group}\n") print(f"Success! Exported {len(group_list)} groups to my_telegram_groups.txt") if __name__ == '__main__': import asyncio asyncio.run(fetch_and_export_groups())
Step 4: Run the script
Execute the script in your terminal:
python export_groups.py
The first time you run it, you'll be prompted to enter the verification code sent to your Telegram app. Once verified, the script will generate a my_telegram_groups.txt file in the same directory with all your group names.
内容的提问来源于stack exchange,提问作者Rudresh Dixit




