How to Export Telegram Groups and Channels via Telethon

Preface

A group friend said that after creating a new Telegram account, they didn't know how to add the groups from the old account back

https://t.me/duangvpsfs/3769416

Approach

First, ask GPT whether it's possible
telegram bot 或 telethon 可以 查询 某个 telegram 账号已加入的 group 和 channel 吗?
The conclusion is that telethon can do it, and GPT provided some code
from telethon import TelegramClient

client = TelegramClient('session_name', api_id, api_hash)

async def get_my_dialogs():
    await client.start()
    # 获取所有对话(包括群组、频道、私聊)
    async for dialog in client.iter_dialogs():
        if dialog.is_group:
            print(f"群组: {dialog.name}")
        elif dialog.is_channel:
            print(f"频道: {dialog.name}")

with client:
    client.loop.run_until_complete(get_my_dialogs())
I have some programming ability myself, so I can tell that this code only outputs the names of groups and channels. More accurately, it should also output the username of groups and channels
But, suppose I am just a beginner who doesn't know how to program, then I would first try what effect this code produces.

Debugging

Found a VPS (you can also use your own computer, as long as you install python and solve the proxy problem)
Save the code as a .py file and upload it. For example, the filename tg-get-group-channel.py
Then run it on the VPS, and there is an error
Copy and paste the error to GPT, and the reply is
Need to install the telethon module
Need to apply for api
Need to modify the code
from telethon import TelegramClient

# 填入你从 my.telegram.org 获取的信息
api_id = 12345  # 你的 api_id
api_hash = 'your_api_hash_here'  # 你的 api_hash

client = TelegramClient('session_name', api_id, api_hash)

async def get_my_dialogs():
    await client.start()
    async for dialog in client.iter_dialogs():
        if dialog.is_group:
            print(f"群组: {dialog.name}")
        elif dialog.is_channel:
            print(f"频道: {dialog.name}")

with client:
    client.loop.run_until_complete(get_my_dialogs())
An error occurred when installing telethon
Thanks to myself for having pre-installing command-not-found
I followed the instructions to install python3-pip first, and then I could install telethon
There was a new error
Since we are just running this experimentally, just add the  --break-system-packages parameter to install.
pip install telethon --break-system-packages
Next, go to apply for the API 
* Applying for the API has certain requirements for the visiting IP. You may succeed or fail. This is omitted here.

Put api_id and api_hash into the code, and run the code again
It worked.
Need to enter the phone number, enter the telegram verification code, enter the password (if you set one)
The result is like this

In this way, there are only the names of groups and channels, not the username 
So I made a request to GPT
输出 的结果 是这样的
群组: test公开群
频道: 翻墙新手求助现场
群组: Joey Huang Blog
群组: ja-netfilter
群组: 折腾啥
群组: Trojan-Go使用与反馈

我需要增加输出 群组和频道的 username
As a result, the code was updated
from telethon import TelegramClient

api_id = 12345
api_hash = 'your_api_hash_here'

client = TelegramClient('session_name', api_id, api_hash)

async def get_my_dialogs():
    await client.start()
    async for dialog in client.iter_dialogs():
        entity = dialog.entity
        username = f"@{entity.username}" if getattr(entity, 'username', None) else "私有"
        
        type_name = "群组" if dialog.is_group else "频道" if dialog.is_channel else "其他"
        
        if dialog.is_group or dialog.is_channel:
            print(f"{type_name}: {dialog.name} | {username}")

with client:
    client.loop.run_until_complete(get_my_dialogs())
Running result 

Done.

It is already in a usable state. 
If you think copying text from the terminal interface is too troublesome, let GPT add a feature to output to a file.

========

Github


========

Postscript

The GPT used this time (2026-1-24) is

Claude has a feature to share the entire conversation process
https://claude.ai/share/5ab8d166-974f-44bc-af97-3d21be266fc8

========
Related recommendations
Comments