[Python] Telethon client .session + socks5 proxy quick deauthorization

Discussão em 'Python' iniciado por Stack, Dezembro 2, 2024.

  1. Stack

    Stack Membro Participativo

    I have a valid .session + .json files pair representing Telegram account session.

    Code below initializes a Telethon client using valid socks5 proxy having same GEO as client telegram account from a .session file.

    from python_socks import ProxyType
    from telethon import TelegramClient
    from telethon.errors import rpcerrorlist
    from telethon.network import ConnectionTcpAbridged
    from telethon.tl.functions.channels import JoinChannelRequest
    import config


    async def post(session_filename, account, chat, post, proxy):
    client = TelegramClient(
    session=session_filename,
    api_id=account.app_id,
    api_hash=account.app_hash,
    proxy={
    'proxy_type': ProxyType.SOCKS5,
    'addr': proxy.host,
    'port': proxy.port,
    'username': proxy.login,
    'password': proxy.password
    },
    device_model=account.device_model,
    system_version=account.system_version,
    app_version=account.app_version,
    lang_code=account.lang_code,
    timeout=15, request_retries=3, connection_retries=3, retry_delay=5, auto_reconnect=False,
    connection=ConnectionTcpAbridged
    )

    if not client.is_connected():
    try:
    await client.connect()
    except ConnectionError:
    return
    if not await client.is_user_authorized():
    return

    try:
    await client(JoinChannelRequest(chat.username))
    except rpcerrorlist.UserDeactivatedBanError:
    return

    try:
    channel = await client.get_entity(chat.username)
    msg = await client.forward_messages(
    entity=channel, messages=post.message_id, from_peer=config['postStorage']['username'],
    drop_author=True)
    except rpcerrorlist.UserDeactivatedBanError:
    return

    return msg


    account.app_id, account.app_hash, account.device_model, account.system_version, account.app_version, account.lang_code fields are taken from .json file.

    Initialized client does join chats and does forward messages dropping author as required, yet in best case after 2-3 joined chats and 7 forwarded messages session gets deauthorized - another call of post function returns on:

    if not await client.is_user_authorized():
    return


    What is the cause? I have tested this with ~4 different proxy providers, I got ~70 different telethon sessions deauthorized, I tried all solutions I could think of, yet with no success.

    How do I solve this issue? What do I do so that my sessions live long and don`t get authorized?

    Continue reading...

Compartilhe esta Página