Search code examples
pythontelethon

Why can't I join an open channel using telethon?


Here is my code:

from telethon.sync import TelegramClient
from telethon.tl.functions.channels import JoinChannelRequest, InviteToChannelRequest

import asyncio

from config import BOT_TOKEN, API_ID, API_HASH

# Ваши API ID и API Hash
api_id = API_ID
api_hash = API_HASH

async def join_channel():
    async with TelegramClient('session_name', api_id, api_hash) as client:
        channel_id = '-1001565.....'
        
        try:
            await client(JoinChannelRequest(channel_id))

        except Exception as e:
            await client(InviteToChannelRequest(channel_id, [await client.get_me()]))

if __name__ == "__main__":
    asyncio.run(join_channel())

Gives an error message: ValueError: Cannot find any entity corresponding to "-1001565...."


Solution

  • Identifiers for most things in Telegram, including channels, are int, not str. You have to remove the quotes around the number.

    The error message you cut should contain a link with additional information and explanation.