Search code examples
pythonhyperlinktelegramtelegram-botpyrogram

cant join chat from a file (pyrogram)


Im trying to join in telegram group chats from a txt file that contains lots of group links using Pyrogram framework. this is my code:

links = open('.../Desktop/Tel_Links/Links.txt')

app = Client(
    "My_Account",
    api_id = API,
    api_hash ="API_Hash"
)

with app:
    for line in links.readline():
            app.join_chat()

but when i do that i get this error :

Exception has occurred: UsernameInvalid
[400 USERNAME_INVALID]: The username is invalid (caused by "contacts.ResolveUsername")

after this i tried to type the link in app.join_chat("link") instead of app.join_chat(links.readline())

like this :

app = Client(
    "My_Account",
    api_id = API,
    api_hash ="API_Hash"
)

with app:
    app.join_chat('django')

and that worked perfectly but i dont want to add all of them by hand cuz there is lots of links and i need to import them from the file. please help me Thank you


Solution

  • You have forgotten to pass line to join_chat:

    with app:
        for line in links.readlines():
                app.join_chat(line.rstrip()) # pass line to method
    

    You have to remove the trailing newline with rstrip before passing it.

    Edit

    Btw: It should be readlines() instead of readline() in your case.