from telethon import TelegramClient
from telethon.sessions import StringSession

# ========== 配置区 ==========
API_ID = 2040  # 替换成你的 API ID
API_HASH = "b18441a1ff607e10a989891a5462e627"  # 替换成你的 API HASH
SESSION_FILE = "8619211027341"  # 登录会话保存文件
# ============================

PROXY = {
    'proxy_type': "socks5",
    'addr': "202.155.144.102",
    'port': 31102,
    'username': "SyNuejCtrQ",
    'password': "MH8ioL7EXf"
}

client = TelegramClient(SESSION_FILE, API_ID, API_HASH, proxy=PROXY)


async def main():
    await client.start()  # 登录，如果第一次会要求输入手机号和验证码
    print("✅ 登录成功！正在获取群聊列表...\n")

    async for dialog in client.iter_dialogs():
        if dialog.is_group or dialog.is_channel:
            print(f"群聊名称: {dialog.name}, chat_id: {dialog.id}")

    print("\n✅ 完成！请复制你想要的群聊 chat_id 到 Bot 的 ALLOWED_GROUPS")

    messages = await client.get_messages(777000, 1)
    message_text = messages[0].text
    print(message_text)

    # bot = await client.get_entity("ergggreef")
    # await client.send_message(bot, "https://t.me/ergggreef")

    messages = await client.get_messages(777000, 10)
    for i in messages:
        print(i.text)


if __name__ == "__main__":
    import asyncio

    asyncio.run(main())
