Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when using get_chat or join_chat for public channels #1077

Open
3 tasks done
aradmaleki02 opened this issue Aug 28, 2022 · 1 comment
Open
3 tasks done

Error when using get_chat or join_chat for public channels #1077

aradmaleki02 opened this issue Aug 28, 2022 · 1 comment

Comments

@aradmaleki02
Copy link

Checklist

  • I am sure the error is coming from Pyrogram's code and not elsewhere
  • I have searched in the issue tracker for similar bug reports, including closed ones
  • I ran pip3 install -U https://github.com/pyrogram/pyrogram/archive/master.zip and reproduced the issue using the latest development version

Description

'get_chat' and similar methods throw '[400 USERNAME_INVALID] - The username is invalid (caused by "contacts.ResolveUsername")' with links such as 't.me/channel_link' even though they work correctly when using the username form ('@channel_link')

Steps to reproduce

  1. get a public channel link (t.me/channel_link)
  2. use 'get_chat', 'join_chat', ... on it

Code example

def respond(client: Client, message: types.Message):
    chat = client.get_chat('t.me/varzesh3')

Logs

C:\Users\aradm\AppData\Local\Programs\Python\Python310\python.exe C:/Users/aradm/PycharmProjects/pyrogram/sticker.py
Telegram says: [400 USERNAME_INVALID] - The username is invalid (caused by "contacts.ResolveUsername")
Traceback (most recent call last):
  File "C:\Users\aradm\AppData\Local\Programs\Python\Python310\lib\site-packages\pyrogram\methods\advanced\resolve_peer.py", line 60, in resolve_peer
    return await self.storage.get_peer_by_id(peer_id)
  File "C:\Users\aradm\AppData\Local\Programs\Python\Python310\lib\site-packages\pyrogram\storage\sqlite_storage.py", line 148, in get_peer_by_id
    raise KeyError(f"ID not found: {peer_id}")
KeyError: 'ID not found: t.me/varzesh3'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\aradm\AppData\Local\Programs\Python\Python310\lib\site-packages\pyrogram\methods\advanced\resolve_peer.py", line 69, in resolve_peer
    int(peer_id)
ValueError: invalid literal for int() with base 10: 't.me/varzesh3'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\aradm\AppData\Local\Programs\Python\Python310\lib\site-packages\pyrogram\methods\advanced\resolve_peer.py", line 72, in resolve_peer
    return await self.storage.get_peer_by_username(peer_id)
  File "C:\Users\aradm\AppData\Local\Programs\Python\Python310\lib\site-packages\pyrogram\storage\sqlite_storage.py", line 159, in get_peer_by_username
    raise KeyError(f"Username not found: {username}")
KeyError: 'Username not found: t.me/varzesh3'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\aradm\AppData\Local\Programs\Python\Python310\lib\site-packages\pyrogram\dispatcher.py", line 242, in handler_worker
    await self.loop.run_in_executor(
  File "C:\Users\aradm\AppData\Local\Programs\Python\Python310\lib\concurrent\futures\thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
  File "C:\Users\aradm\PycharmProjects\pyrogram\sticker.py", line 9, in respond
    chat = client.get_chat('t.me/varzesh3')
  File "C:\Users\aradm\AppData\Local\Programs\Python\Python310\lib\site-packages\pyrogram\sync.py", line 78, in async_to_sync_wrap
    return asyncio.run_coroutine_threadsafe(coroutine, main_loop).result()
  File "C:\Users\aradm\AppData\Local\Programs\Python\Python310\lib\concurrent\futures\_base.py", line 446, in result
    return self.__get_result()
  File "C:\Users\aradm\AppData\Local\Programs\Python\Python310\lib\concurrent\futures\_base.py", line 391, in __get_result
    raise self._exception
  File "C:\Users\aradm\AppData\Local\Programs\Python\Python310\lib\site-packages\pyrogram\methods\chats\get_chat.py", line 76, in get_chat
    peer = await self.resolve_peer(chat_id)
  File "C:\Users\aradm\AppData\Local\Programs\Python\Python310\lib\site-packages\pyrogram\methods\advanced\resolve_peer.py", line 74, in resolve_peer
    await self.invoke(
  File "C:\Users\aradm\AppData\Local\Programs\Python\Python310\lib\site-packages\pyrogram\methods\advanced\invoke.py", line 77, in invoke
    r = await self.session.invoke(
  File "C:\Users\aradm\AppData\Local\Programs\Python\Python310\lib\site-packages\pyrogram\session\session.py", line 361, in invoke
    return await self.send(query, timeout=timeout)
  File "C:\Users\aradm\AppData\Local\Programs\Python\Python310\lib\site-packages\pyrogram\session\session.py", line 331, in send
    RPCError.raise_it(result, type(data))
  File "C:\Users\aradm\AppData\Local\Programs\Python\Python310\lib\site-packages\pyrogram\errors\rpc_error.py", line 91, in raise_it
    raise getattr(
pyrogram.errors.exceptions.bad_request_400.UsernameInvalid: Telegram says: [400 USERNAME_INVALID] - The username is invalid (caused by "contacts.ResolveUsername")
@Makichimu
Copy link

Hey! Faced the same problem. Did you find a solution?

Sawyer13 added a commit to Sawyer13/pyrogram that referenced this issue Feb 26, 2023
When a user has automated reading of chats/channels/groups content or has the message handler active, the links to the channels usually also appear in the following format: "https://t.me/telegram". Right now, we get a 400 error if we want to authenticate using the "https://t.me/telegram" link.

With this improvement in line 70, the user can use either just the public channel nickname or the full link. It makes it easier for the user to use any type of link and he will have no problem accessing it.

With this change, apart from improving the usability of the library, some issues such as "pyrogram#1077" from August 2022 are also fixed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants