TG-getlist

import os
import csv
import datetime
from telethon import TelegramClient
from dotenv import load_dotenv

# === 1. 读取配置 ===
load_dotenv()

API_ID = os.getenv("API_ID")
API_HASH = os.getenv("API_HASH")
PHONE_NUMBER = os.getenv("PHONE_NUMBER")
TWO_STEP_PASSWORD = os.getenv("TWO_STEP_PASSWORD")

if not API_ID or not API_HASH:
    print("❌ 错误: 请确保 .env 文件中配置了 API_ID 和 API_HASH")
    exit(1)

client = TelegramClient('user_session', int(API_ID), API_HASH)

def get_bot_api_id(entity, entity_type):
    """
    根据实体类型将 Telethon ID 转换为 Bot API ID
    Bot API 规则:
    - 频道/超级群: -100 + ID
    - 普通小群: - + ID
    - 用户: ID (不变)
    """
    raw_id = entity.id
    if entity_type in ["频道", "超级群", "频道(未知)"]:
        return int(f"-100{raw_id}")
    elif entity_type == "普通群":
        return int(f"-{raw_id}")
    else:
        return raw_id

async def list_and_export_chats():
    """列出并导出账号加入的所有频道和群组"""
    print("📃 正在获取对话列表,请稍候...")
    
    chat_data_list = []
    
    async for dialog in client.iter_dialogs():
        entity = dialog.entity
        entity_type = "未知"
        
        # --- 分类逻辑 ---
        if dialog.is_user:
            entity_type = "私聊"
        elif dialog.is_channel:
            if getattr(entity, 'broadcast', False):
                entity_type = "频道"
            elif getattr(entity, 'megagroup', False):
                entity_type = "超级群"
            else:
                entity_type = "频道(未知)"
        elif dialog.is_group:
            entity_type = "普通群"
        
        # 过滤掉私聊 (如果需要私聊ID,注释掉下面这行)
        if entity_type != "私聊":
            # 获取两种格式的 ID
            raw_id = entity.id
            bot_api_id = get_bot_api_id(entity, entity_type)

            chat_info = {
                "类型": entity_type,
                "名称": dialog.name,
                "Bot_API_ID": bot_api_id,  # ✅ 新增:可以直接给 Bot 用的 ID
                "原始_ID": raw_id,         # Telethon 用的原始 ID
                "用户名": getattr(entity, 'username', '无') or '无',
                "成员数": getattr(entity, 'participants_count', '未知')
            }
            chat_data_list.append(chat_info)
            
            print(f"[{entity_type}] {dialog.name} | Bot_ID: {bot_api_id}")

    # === 导出到 CSV 文件 ===
    if chat_data_list:
        timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
        filename = f"telegram_chats_{timestamp}.csv"
        
        # 更新表头
        headers = ["类型", "名称", "Bot_API_ID", "原始_ID", "用户名", "成员数"]
        
        try:
            with open(filename, mode='w', encoding='utf-8-sig', newline='') as f:
                writer = csv.DictWriter(f, fieldnames=headers)
                writer.writeheader()
                writer.writerows(chat_data_list)
            
            print("-" * 50)
            print(f"✅ 成功!共导出 {len(chat_data_list)} 个群组/频道。")
            print(f"📁 文件已保存为: {os.path.abspath(filename)}")
            
        except Exception as e:
            print(f"❌ 导出文件失败: {e}")
    else:
        print("⚠️ 未找到任何群组或频道。")

async def main():
    await client.start(phone=PHONE_NUMBER, password=TWO_STEP_PASSWORD)
    print("✅ 登录成功")
    await list_and_export_chats()

if __name__ == "__main__":
    with client:
        client.loop.run_until_complete(main())
vless://a2a396c3-f40e-4dee-bdbb-b1201f12dc1c@www.shopify.com:2096?encryption=none&security=tls&sni=danfeng.sylu.cc&fp=random&alpn=http%2F1.1&insecure=1&allowInsecure=1&type=ws&host=danfeng.sylu.cc&path=%2Fdanfeng%3Fed%3D2560#%E7%99%BD%E8%99%8E%C2%B7%E8%A5%BF
vless://a2a396c3-f40e-4dee-bdbb-b1201f12dc1c@store.ubi.com:2083?encryption=none&security=tls&sni=danfeng.sylu.cc&fp=random&alpn=http%2F1.1&insecure=1&allowInsecure=1&type=ws&host=danfeng.sylu.cc&path=%2Fdanfeng%3Fed%3D2560#%E9%9D%92%E9%BE%99%C2%B7%E4%B8%9C
vless://a2a396c3-f40e-4dee-bdbb-b1201f12dc1c@cloudflare-dl.cnae.top:2087?encryption=none&security=tls&sni=danfeng.sylu.cc&fp=random&alpn=http%2F1.1&insecure=1&allowInsecure=1&type=ws&host=danfeng.sylu.cc&path=%2Fdanfeng%3Fed%3D2560#%E6%89%B6%E6%A1%91%C2%B7%E6%97%A5
vless://a2a396c3-f40e-4dee-bdbb-b1201f12dc1c@cfsaas.080112.xyz:2096?encryption=none&security=tls&sni=danfeng.sylu.cc&fp=random&alpn=http%2F1.1&insecure=1&allowInsecure=1&type=ws&host=danfeng.sylu.cc&path=%2Fdanfeng%3Fed%3D2560#%E8%93%AC%E8%8E%B1%C2%B7%E7%BB%88
vless://a2a396c3-f40e-4dee-bdbb-b1201f12dc1c@cmcmcm.cf.090227.xyz:2053?encryption=none&security=tls&sni=danfeng.sylu.cc&fp=random&alpn=http%2F1.1&insecure=1&allowInsecure=1&type=ws&host=danfeng.sylu.cc&path=%2Fdanfeng%3Fed%3D2560#%E5%A4%AA%E7%99%BD%C2%B7%E5%AE%88
vless://a2a396c3-f40e-4dee-bdbb-b1201f12dc1c@cf-cname.xingpingcn.top:443?encryption=none&security=tls&sni=danfeng.sylu.cc&fp=random&alpn=http%2F1.1&insecure=1&allowInsecure=1&type=ws&host=danfeng.sylu.cc&path=%2Fdanfeng%3Fed%3D2560#%E6%98%86%E4%BB%91%C2%B7%E5%90%AF
vless://a2a396c3-f40e-4dee-bdbb-b1201f12dc1c@staticdelivery.nexusmods.com:2083?encryption=none&security=tls&sni=danfeng.sylu.cc&fp=random&alpn=http%2F1.1&insecure=1&allowInsecure=1&type=ws&host=danfeng.sylu.cc&path=%2Fdanfeng%3Fed%3D2560#%E7%8E%84%E6%AD%A6%C2%B7%E5%8C%97
vless://a2a396c3-f40e-4dee-bdbb-b1201f12dc1c@saas.sin.fan:2087?encryption=none&security=tls&sni=danfeng.sylu.cc&fp=random&alpn=http%2F1.1&insecure=1&allowInsecure=1&type=ws&host=danfeng.sylu.cc&path=%2Fdanfeng%3Fed%3D2560#%E7%90%85%E7%90%8A%C2%B7%E6%B5%B7
vless://a2a396c3-f40e-4dee-bdbb-b1201f12dc1c@openai.com:8443?encryption=none&security=tls&sni=danfeng.sylu.cc&fp=random&alpn=http%2F1.1&insecure=1&allowInsecure=1&type=ws&host=danfeng.sylu.cc&path=%2Fdanfeng%3Fed%3D2560#%E4%B8%8D%E5%91%A8%C2%B7%E5%A4%A9
vless://a2a396c3-f40e-4dee-bdbb-b1201f12dc1c@203.170.125.48:33414?encryption=none&security=tls&sni=danfeng.sylu.cc&fp=random&alpn=http%2F1.1&insecure=1&allowInsecure=1&type=ws&host=danfeng.sylu.cc&path=%2Fdanfeng%3Fed%3D2560#KR%E2%9A%A1TG%40danfeng_chat
vless://a2a396c3-f40e-4dee-bdbb-b1201f12dc1c@80.251.215.244:443?encryption=none&security=tls&sni=danfeng.sylu.cc&fp=random&alpn=http%2F1.1&insecure=1&allowInsecure=1&type=ws&host=danfeng.sylu.cc&path=%2Fdanfeng%3Fed%3D2560#US%E2%9A%A1TG%40danfeng_chat
vless://a2a396c3-f40e-4dee-bdbb-b1201f12dc1c@172.104.239.152:20205?encryption=none&security=tls&sni=danfeng.sylu.cc&fp=random&alpn=http%2F1.1&insecure=1&allowInsecure=1&type=ws&host=danfeng.sylu.cc&path=%2Fdanfeng%3Fed%3D2560#DE%E2%9A%A1TG%40danfeng_chat
vless://a2a396c3-f40e-4dee-bdbb-b1201f12dc1c@89.208.249.191:31337?encryption=none&security=tls&sni=danfeng.sylu.cc&fp=random&alpn=http%2F1.1&insecure=1&allowInsecure=1&type=ws&host=danfeng.sylu.cc&path=%2Fdanfeng%3Fed%3D2560#US%E2%9A%A1TG%40danfeng_chat
vless://a2a396c3-f40e-4dee-bdbb-b1201f12dc1c@57.129.137.77:8443?encryption=none&security=tls&sni=danfeng.sylu.cc&fp=random&alpn=http%2F1.1&insecure=1&allowInsecure=1&type=ws&host=danfeng.sylu.cc&path=%2Fdanfeng%3Fed%3D2560#GB%E2%9A%A1TG%40danfeng_chat
vless://a2a396c3-f40e-4dee-bdbb-b1201f12dc1c@123.214.155.140:32773?encryption=none&security=tls&sni=danfeng.sylu.cc&fp=random&alpn=http%2F1.1&insecure=1&allowInsecure=1&type=ws&host=danfeng.sylu.cc&path=%2Fdanfeng%3Fed%3D2560#KR%E2%9A%A1TG%40danfeng_chat
vless://a2a396c3-f40e-4dee-bdbb-b1201f12dc1c@141.95.159.211:2053?encryption=none&security=tls&sni=danfeng.sylu.cc&fp=random&alpn=http%2F1.1&insecure=1&allowInsecure=1&type=ws&host=danfeng.sylu.cc&path=%2Fdanfeng%3Fed%3D2560#FR%E2%9A%A1TG%40danfeng_chat
vless://a2a396c3-f40e-4dee-bdbb-b1201f12dc1c@152.228.134.80:2053?encryption=none&security=tls&sni=danfeng.sylu.cc&fp=random&alpn=http%2F1.1&insecure=1&allowInsecure=1&type=ws&host=danfeng.sylu.cc&path=%2Fdanfeng%3Fed%3D2560#DE%E2%9A%A1TG%40danfeng_chat
vless://a2a396c3-f40e-4dee-bdbb-b1201f12dc1c@3.67.25.193:443?encryption=none&security=tls&sni=danfeng.sylu.cc&fp=random&alpn=http%2F1.1&insecure=1&allowInsecure=1&type=ws&host=danfeng.sylu.cc&path=%2Fdanfeng%3Fed%3D2560#DE%E2%9A%A1TG%40danfeng_chat
vless://a2a396c3-f40e-4dee-bdbb-b1201f12dc1c@152.53.200.54:8443?encryption=none&security=tls&sni=danfeng.sylu.cc&fp=random&alpn=http%2F1.1&insecure=1&allowInsecure=1&type=ws&host=danfeng.sylu.cc&path=%2Fdanfeng%3Fed%3D2560#DE%E2%9A%A1TG%40danfeng_chat
vless://a2a396c3-f40e-4dee-bdbb-b1201f12dc1c@mfa.gov.ua:2096?encryption=none&security=tls&sni=danfeng.sylu.cc&fp=random&alpn=http%2F1.1&insecure=1&allowInsecure=1&type=ws&host=danfeng.sylu.cc&path=%2Fdanfeng%3Fed%3D2560#%E6%9C%B1%E9%9B%80%C2%B7%E5%8D%97
vless://a2a396c3-f40e-4dee-bdbb-b1201f12dc1c@45.202.113.68:2053?encryption=none&security=tls&sni=danfeng.sylu.cc&fp=random&alpn=http%2F1.1&insecure=1&allowInsecure=1&type=ws&host=danfeng.sylu.cc&path=%2Fdanfeng%3Fed%3D2560#US%E2%9A%A1TG%40danfeng_chat
vless://a2a396c3-f40e-4dee-bdbb-b1201f12dc1c@37.0.6.68:443?encryption=none&security=tls&sni=danfeng.sylu.cc&fp=random&alpn=http%2F1.1&insecure=1&allowInsecure=1&type=ws&host=danfeng.sylu.cc&path=%2Fdanfeng%3Fed%3D2560#GB%E2%9A%A1TG%40danfeng_chat
vless://a2a396c3-f40e-4dee-bdbb-b1201f12dc1c@38.179.85.233:39738?encryption=none&security=tls&sni=danfeng.sylu.cc&fp=random&alpn=http%2F1.1&insecure=1&allowInsecure=1&type=ws&host=danfeng.sylu.cc&path=%2Fdanfeng%3Fed%3D2560#US%E2%9A%A1TG%40danfeng_chat
vless://a2a396c3-f40e-4dee-bdbb-b1201f12dc1c@20.207.237.5:443?encryption=none&security=tls&sni=danfeng.sylu.cc&fp=random&alpn=http%2F1.1&insecure=1&allowInsecure=1&type=ws&host=danfeng.sylu.cc&path=%2Fdanfeng%3Fed%3D2560#IN%E2%9A%A1TG%40danfeng_chat
vless://a2a396c3-f40e-4dee-bdbb-b1201f12dc1c@185.17.2.103:8443?encryption=none&security=tls&sni=danfeng.sylu.cc&fp=random&alpn=http%2F1.1&insecure=1&allowInsecure=1&type=ws&host=danfeng.sylu.cc&path=%2Fdanfeng%3Fed%3D2560#SE%E2%9A%A1TG%40danfeng_chat
vless://a2a396c3-f40e-4dee-bdbb-b1201f12dc1c@95.216.215.126:8443?encryption=none&security=tls&sni=danfeng.sylu.cc&fp=random&alpn=http%2F1.1&insecure=1&allowInsecure=1&type=ws&host=danfeng.sylu.cc&path=%2Fdanfeng%3Fed%3D2560#SE%E2%9A%A1TG%40danfeng_chat
vless://a2a396c3-f40e-4dee-bdbb-b1201f12dc1c@103.45.245.208:2053?encryption=none&security=tls&sni=danfeng.sylu.cc&fp=random&alpn=http%2F1.1&insecure=1&allowInsecure=1&type=ws&host=danfeng.sylu.cc&path=%2Fdanfeng%3Fed%3D2560#ES%E2%9A%A1TG%40danfeng_chat
vless://a2a396c3-f40e-4dee-bdbb-b1201f12dc1c@178.62.21.181:443?encryption=none&security=tls&sni=danfeng.sylu.cc&fp=random&alpn=http%2F1.1&insecure=1&allowInsecure=1&type=ws&host=danfeng.sylu.cc&path=%2Fdanfeng%3Fed%3D2560#GB%E2%9A%A1TG%40danfeng_chat
vless://a2a396c3-f40e-4dee-bdbb-b1201f12dc1c@45.12.53.232:443?encryption=none&security=tls&sni=danfeng.sylu.cc&fp=random&alpn=http%2F1.1&insecure=1&allowInsecure=1&type=ws&host=danfeng.sylu.cc&path=%2Fdanfeng%3Fed%3D2560#US%E2%9A%A1TG%40danfeng_chat
vless://a2a396c3-f40e-4dee-bdbb-b1201f12dc1c@141.94.205.201:8443?encryption=none&security=tls&sni=danfeng.sylu.cc&fp=random&alpn=http%2F1.1&insecure=1&allowInsecure=1&type=ws&host=danfeng.sylu.cc&path=%2Fdanfeng%3Fed%3D2560#FR%E2%9A%A1TG%40danfeng_chat

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注