首页 > 解决方案 > Telethon freezes when joining group

问题描述

I want to connect to a Telegram group using the Telethon package:

from telethon import TelegramClient
from telethon import functions, types
from telethon.tl.functions.channels import JoinChannelRequest
import asyncio

api_id = 1234
api_hash = 'abcde1234'

async def connect_helper(api_id: int, api_hash: str, session_file: str) -> TelegramClient:
        client = TelegramClient(session_file, api_id, api_hash)
        await client.connect()
        authorized = await client.is_user_authorized()
        if authorized:
            print(f"Connection to {session_file} established, Captain!")
            return client

async def join_group(client: TelegramClient, group_name: str):
    result = await client(functions.channels.JoinChannelRequest(group_name))
    return result

client = asyncio.run(connect_helper(api_id, api_hash, '+123456789'))
result = asyncio.run(join_group(client, 'TelethonChat'))

The client loads well, but when it's time to run asyncio.run(join_group(client, 'TelethonChat')), the console "freezes" and does seem to get out of the async loop.

I think this is the cause of error, as I've tried with telethon.sync and it seems to work fine, but I'd like to know if there's another way of solving it, like in this example from the doc.

Thanks for your time!

标签: pythonpython-asynciotelegramtelethon

解决方案


推荐阅读